This question already has answers here:
scanf not working as expected
(4 answers)
C loop prints string twice? (using scanf("%c"))
(2 answers)
why does this c program print twice [closed]
(1 answer)
Closed 2 years ago.
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int main()
{
char op;
float a, b;
printf("\n Operators are ( +,-,*,/,^) \n");
printf("Enter 0 to quit \n");
while(1)
{
printf("Enter your choice: \n");
scanf("%c", &op);
switch(op)
{
case '+':
printf(" Enter your two numbers: \n");
scanf("%f %f", &a, &b);
printf("%f + %f = %f", a, b, a+b);
break;
case '-':
printf(" Enter your two numbers: \n");
scanf("%f %f", &a, &b);
printf("%f - %f = %f", a, b, a-b);
break;
case '*':
printf(" Enter your two numbers: \n");
scanf("%f %f", &a, &b);
printf("%f x %f = %f", a, b, a*b);
break;
case '/':
printf(" Enter your two numbers: \n");
scanf("%f %f", &a, &b);
printf("%f / %f = %f", a, b, a/b);
break;
case '^':
printf(" Enter your two numbers: \n");
scanf("%f %f", &a, &b);
printf("%f ^ %f = %f", a, b, pow(a, b));
break;
case '0':
exit(0);
}
}
return 0;
}
This program is basically a switch case basic calculator.
After the while loop start, the program prints "Enter your choice" twice instead of once, can anyone tell how to fix this. Apart from that it works fine. The while loop is for menu driven program.
Related
#include <stdio.h>
void main() {
int a, b, c, d, e, f, avg;
printf("Enter marks of six subjects: ");
scanf("%d %d %d %d %d %d %d", &a, &b, &c, &d, &e, &f);
avg = (a + b + c + d + e + f) / 6;
printf("Average is %d", avg);
switch (avg) {
case 10:
printf("Your grade is 10.");
break;
case 9:
printf("Your grade is 9.");
break;
case 8:
printf("Your grade is 8.");
break;
case 7:
printf("Your grade is 7.");
break;
case 6:
printf("Your grade is 6.");
break;
case 5:
printf("Your grade is 5.");
default:
printf("You are fail.");
}
}
After I enter the marks of six subjects, the code is not running, it's neither showing any error message as well.
Problems include
Not enabling all compiler warnings
Mis-match
"%d %d %d %d %d %d %d", &a, &b, &c, &d, &e, &f: 7 specifiers and 6 following arguments.
Not checking return value of scanf()
// scanf("%d %d %d %d %d %d %d", &a, &b, &c, &d, &e, &f);
if (scanf("%d %d %d %d %d %d", &a, &b, &c, &d, &e, &f) != 6) {
; // TBD: Handle error, maybe print some error message and exit
}
No break
case 5:
printf("Your grade is 5.");
break; // add
default:
printf("You are fail.");
This question already has answers here:
pow() isn't defined [closed]
(4 answers)
Closed 3 years ago.
I have reviewed multiple questions and answers for powers in C, or 4^2. I have wrote it out in different ways and still can't it right. As of now I get the message:
/tmp/cc9yBiPo.o: In function `main':
Functions2.c:(.text+0x2b8): undefined reference to `pow'
collect2: error: ld returned 1 exit status.
Here is my code so far, like I said I have tried different peoples answers but I am more confused more than anything.
#include <stdio.h>
#include <math.h>
int main()
{
int choice;
int a, b, c;
float d;
do{
printf("\t Menu \n");
printf("1. Addition \n");
printf("2. Subtraction \n");
printf("3. Multiplication \n");
printf("4. Division \n");
printf("5. Modulo \n");
printf("6. Eponents \n");
printf("7. Exit \n");
printf("Please choose a menu selection: \n");
scanf("%d", &choice);
switch(choice)
{
case 1:
{
printf("Enter two numbers to add: \n");
scanf("%d%d", &a, &b);
c = a + b;
printf("The sum of the two numbers = %d \n", c);
}break;
case 2:
{
printf("Enter two numbers to subtract: \n");
scanf("%d&d", &a, &b);
c = a - b;
printf("The difference between the two number = %d \n", c);
}break;
case 3:
{
printf("Enter two numbers to multiply: \n");
scanf("%d%d", &a, &b);
c = a * b;
printf("The product of the two numbers = %d \n", c);
}break;
case 4:
{
printf("Enter two numbers to divide: \n");
scanf("%d%d", &a, &b);
d = (float) a/b;
printf("The quotient of the two numbers = %f \n", d);
}break;
case 5:
{
printf("Enter two numbers to get a modulo: \n");
scanf("%d%d", &a, &b);
c = a % b;
printf("The modulus of the two numbers = %d \n", c);
}break;
case 6:
{
printf("Enter a base: \n");
scanf("%d", &a);
printf("Enter an exponent: \n");
scanf("%d", &b);
c = pow(a, b);
printf("The exponent of the two numbers = %d", c);
}break;
case 7:
{
printf("Thank you, you will now exit.");
}break;
default:
printf("error \n");
}
} while (choice != 7);
return 0;
}
You have to link with the math library(libm).
Add -lm flag to your complication command to link with it.
Check this question.
You have to change the data types of base & exponent to get correct result.
The syntax for the pow function in the C Language is:
double pow(double x, double y);
Again, change the data type from int to float in case of 4.
#include <stdio.h>
#include <math.h>
int main()
{
double g,h,i;
int choice;
int a, b, c;
float d,e,f;
do{
printf("\t Menu \n");
printf("1. Addition \n");
printf("2. Subtraction \n");
printf("3. Multiplication \n");
printf("4. Division \n");
printf("5. Modulo \n");
printf("6. Eponents \n");
printf("7. Exit \n");
printf("Please choose a menu selection: \n");
scanf("%d", &choice);
switch(choice)
{
case 1:
{
printf("Enter two numbers to add: \n");
scanf("%d%d", &a, &b);
c = a + b;
printf("The sum of the two numbers = %d \n", c);
}break;
case 2:
{
printf("Enter two numbers to subtract: \n");
scanf("%d&d", &a, &b);
c = a - b;
printf("The difference between the two number = %d \n", c);
}break;
case 3:
{
printf("Enter two numbers to multiply: \n");
scanf("%d%d", &a, &b);
c = a * b;
printf("The product of the two numbers = %d \n", c);
}break;
case 4:
{
printf("Enter two numbers to divide: \n");
scanf("%f%f", &e, &f);
d = e/f;
printf("The quotient of the two numbers = %f \n", d);
}break;
case 5:
{
printf("Enter two numbers to get a modulo: \n");
scanf("%d%d", &a, &b);
c = a % b;
printf("The modulus of the two numbers = %d \n", c);
}break;
case 6:
{
printf("Enter a base: \n");
scanf("%lf", &h);
printf("Enter an exponent: \n");
scanf("%lf", &i);
g = pow(h, i);
printf("The exponent of the two numbers = %lf", g);
}break;
case 7:
{
printf("Thank you, you will now exit.");
}break;
default:
printf("error \n");
}
} while (choice != 7);
return 0;
}
Hope this will help you.
This question already has answers here:
C: printf a float value
(7 answers)
Closed 3 years ago.
I can't disable scientific notation... I'd like to print on screen the exact value of the numbers instead of the scientific notation. It's my first project in C. It's a simple calculator it's just a prototype, I'd like to create a very complex program starting from this. Any help or tips will be really appreciated. Thanks for your support.
#include <math.h>
#include <stdio.h>
#define ADD(a, b) (a += b)
#define SOT(a, b) (a -= b)
#define PER(a, b) (a *= b)
#define DIV(a, b) (a /= b)
#define POT(a, b) a = pow(a, b)
#define LOG(a, b) a = (log10(a) / log10(b))
#define PERC(a) a = (a / 100)
{
float a, b, d;
static float e;
char c, g;
printf("Calcola \n");
scanf("%g", &a);
while (1) {
e = a;
scanf(" %c", &c);
switch (c) {
case '+':
scanf("%g", &b);
system("clear");
d = ADD(a, b);
printf(" \n Risultato \n \n");
printf("(%g+%g)=", e, b);
printf("%g", d);
break;
case '-':
scanf("%g", &b);
system("clear");
d = SOT(a, b);
printf(" \n Risultato \n \n");
printf("(%g-%g)=", e, b);
printf("%g", d);
break;
case '*':
scanf("%g", &b);
system("clear");
d = PER(a, b);
printf(" \n Risultato \n \n");
printf("(%g*%g)=", e, b);
printf("%g ", d);
break;
case '/':
scanf("%g", &b);
system("clear");
d = DIV(a, b);
printf(" \n Risultato \n \n");
printf("(%g/%g)=", e, b);
printf("%g", d);
break;
case '^':
scanf("%g", &b);
system("clear");
d = POT(a, b);
printf(" \n Risultato \n \n");
printf("(%g^%g)=", e, b);
printf("%g", d);
break;
case 'l':
scanf("%g", &b);
system("clear");
d = LOG(a, b);
printf(" \n Risultato \n \n");
printf("(%glog%g)=", e, b);
printf("%g", d);
break;
case '%':
system("clear");
d = PERC(a);
printf(" \n Risultato \n \n");
printf("%g%=", e);
printf("%g", d);
break;
}
}
return 0;
}
Use the f format specifier, not g:
converts floating-point number to the decimal notation in the style [-]ddd.ddd.
Example:
printf ("%f", d);
instead of:
printf ("%g", d);
Here, i am writing a c programming code i need help form you guys! I want to modify my program and want to use this in a loop i.e if a user enters "y" & if "n" will be entered it must end the program, I am trying this with switch case, I tried my best to end the program, it is returning to end but i don't know how to use "y" value for running the program again!
#include<conio.h>
#include<stdio.h>
int main()
{
char n, A, B;
float kmTOm = 0.621371;
float inchesTOfoot = 0.0833333;
float cmTOinches = 0.393701;
float poundtokg = 0.453592;
float inchesTOmeter = 0.0254;
float a, b;
while (1)
{
printf("Enter the input character. q to quit\n1. kms to miles\n2. inches to foot\n3. cms to inches\n4. pound to kgs\n5. inches to meters\n");
scanf(" %c", &n);
switch (n)
{
case 'q':
printf("Quitting the program...");
goto end;
break;
case '1':
printf("Enter quantity in terms of a unit\n");
scanf("%f", &a);
b = a * kmTOm;
printf("%.2f Kms is equal to %.2f Miles\n\n\n", a, b);
break;
case '2':
printf("Enter quantity in terms of a unit\n");
scanf("%f", &a);
b = a * inchesTOfoot;
printf("%f Inches is equal to %f Foot\n", a, b);
break;
case '3':
printf("Enter quantity in terms of a unit\n");
scanf("%f", &a);
b = a * cmTOinches;
printf("%f Cms is equal to %f Inches\n", a, b);
break;
case '4':
printf("Enter quantity in terms of a unit\n");
scanf("%f", &a);
b = a * poundtokg;
printf("%f Pounds is equal to Kgs %f \n", a, b);
break;
case '5':
printf("Enter quantity in terms of a unit\n");
scanf("%f", &a);
b = a * inchesTOmeter;
printf("%f inches is equal to %f meters \n", a, b);
break;
default:
printf("In default now");
break;
}
}
end:
return 0;
}
I want a kindful modification from you developers to help me as I am a beginner and learing from a month, I except that the actual output must come like this ----o/p- do you want to quit press "q" ,do you want to run again press "y"
If you change the while(1) to a variable, then you can test after every loop whether it's time to exit.
The changes I made to your code all have comments below:
#include<conio.h>
#include<stdio.h>
int main()
{
char n, A, B;
float kmTOm = 0.621371;
float inchesTOfoot = 0.0833333;
float cmTOinches = 0.393701;
float poundtokg = 0.453592;
float inchesTOmeter = 0.0254;
float a, b;
n = 'y'; //set n to make sure while repeats
while (n != 'n') //change while to test "n" instead of 1, this way the value of n can be changed and the loop can be exited
{
printf("Enter the input character. q to quit\n1. kms to miles\n2. inches to foot\n3. cms to inches\n4. po
und to kgs\n5. inches to meters\n");
scanf(" %c", &n);
switch (n)
{
case 'q':
printf("Quitting the program...");
return(0); //exit entire program
break;
case '1':
printf("Enter quantity in terms of a unit\n");
scanf("%f", &a);
b = a * kmTOm;
printf("%.2f Kms is equal to %.2f Miles\n\n\n", a, b);
break;
case '2':
printf("Enter quantity in terms of a unit\n");
scanf("%f", &a);
b = a * inchesTOfoot;
printf("%f Inches is equal to %f Foot\n", a, b);
break;
case '3':
printf("Enter quantity in terms of a unit\n");
scanf("%f", &a);
b = a * cmTOinches;
printf("%f Cms is equal to %f Inches\n", a, b);
break;
case '4':
printf("Enter quantity in terms of a unit\n");
scanf("%f", &a);
b = a * poundtokg;
printf("%f Pounds is equal to Kgs %f \n", a, b);
break;
case '5':
printf("Enter quantity in terms of a unit\n");
scanf("%f", &a);
b = a * inchesTOmeter;
printf("%f inches is equal to %f meters \n", a, b);
break;
default:
printf("In default now");
break;
}
//Ask the user if they want to run again. The code will exit if the user types 'n', it will repeat if they type anything else (including 'y').
printf("\nRun again? y/n:");
scanf(" %c",&n);
}
return 0;
}
The below C program gives me output only for Addition(+), difference(-), multiplication(*).
But when I try to use division(/) and modulus(%) the program just close itself without giving any error. Help me I'm a newbie to C programming.
//A simple calculator.
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, sum, diff, rem, multi;
float div;
char character;
clrscr();
printf("Choose the character you want to use(+, -, *, /, %): ");
scanf("%c", &character);
switch(character)
{
case '+': //will be used for addition.
printf("Enter the first and second number: ");
scanf("%d %d", &a, &b);
sum = a+b;
printf("The sum of the %d and %d is %d", a, b, sum);
break;
case '-': //will be used for difference.
printf("Enter the first and second number: ");
scanf("%d %d", &a, &b);
diff = a-b;
printf("The difference between %d and %d is %d", a, b, diff);
break;
case '%': //will be used for modulus.
printf("Enter the first and second number: ");
scanf("%f %f", &a, &b);
rem = a%b;
printf("The remainder of %f and %f is %f", a, b, rem);
break;
case '*': //will be used for product of 2 no.
printf("Enter the first and second number: ");
scanf("%d %d", &a, &b);
multi = a*b;
printf("The multiplication of %d and %d is %d", a, b, multi);
break;
case '/': //will be used for the division.
printf("Enter the first and second number: ");
scanf("%f %f", &a, &b);
div = a/b;
printf("The division of %f and %f is %f", a, b, div);
break;
default:
printf("Error! character please retry");
}
getch();
}
You are using %f format specifier for int variables in / and % cases.
scanf("%f %f", &a, &b);
Thus invoking undefined behavior.
Change it to as below.
scanf("%d %d", &a, &b);
%f is used to readin the float variables.
If you want float result for division, you need to cast the one of the argument to float instead reading them as float.
div = (float)a/b;
When using the / and % use
scanf("%d %d", &a, &b);
instead of
scanf("%f %f", &a, &b);
because %f is used for float vars whereas in case of / and % %d is used
You are using wrong formats. After enabling -Wall in gcc and fixing the warnings I get a working program. You also missing the \n in your answer printf()'s
#include <stdio.h>
int main(int argc, char **argv)
{
int a, b, sum, diff, rem, multi;
float div;
char character;
printf("Choose the character you want to use(+, -, *, /, %%): ");
scanf("%c", &character);
switch(character)
{
case '+': //will be used for addition.
printf("Enter the first and second number: ");
scanf("%d %d", &a, &b);
sum = a+b;
printf("The sum of the %d and %d is %d\n", a, b, sum);
break;
case '-': //will be used for difference.
printf("Enter the first and second number: ");
scanf("%d %d", &a, &b);
diff = a-b;
printf("The difference between %d and %d is %d\n", a, b, diff);
break;
case '%': //will be used for modulus.
printf("Enter the first and second number: ");
scanf("%d %d", &a, &b);
rem = a%b;
printf("The remainder of %d and %d is %d\n", a, b, rem);
break;
case '*': //will be used for product of 2 no.
printf("Enter the first and second number: ");
scanf("%d %d", &a, &b);
multi = a*b;
printf("The multiplication of %d and %d is %d\n", a, b, multi);
break;
case '/': //will be used for the division.
printf("Enter the first and second number: ");
scanf("%d %d", &a, &b);
div = a/b;
printf("The division of %d and %d is %f\n", a, b, div);
break;
default:
printf("Error! character please retry");
}
}
Test output:
$ ./dummy
Choose the character you want to use(+, -, *, /, %): %
Enter the first and second number: 5 2
The remainder of 5 and 2 is 1
$