I am trying to execute following program in eclipse
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
int a, base, power;
printf("\nBase:");
fflush(stdout);
scanf("%d", &base);
fflush(stdin);
printf("\nExponent:");
fflush(stdout);
scanf("%d", &power);
fflush(stdin);
a = pow(base, power);
printf("\nAnswer: %d", a);
fflush(stdout);
return 0;
getch();
}
the output to the above program should be as follows
Base :
Exponent :
Answer:
but I am getting different output
Base: 2
Exponent: 2
Enter base value: 2
Enter exponent value: 2
The exponent value of 2 is 4
Answer: 0
as you can see the output is asking me to enter the base and exponent values two times where's it should ask only once.
How to disable this?
A couple of things you should fix in your code:
Remove the getch(); line after the return. It won't be executed and you should be getting a compiler warning about this line. Now you can also remove #include<conio.h> as you don't need it.
You fflush() stdin or stdout (as pointed out in the comments).
The resulting code runs just fine and produces the expected output. You can check it out here: https://onlinegdb.com/S1LLp9GoU
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int
main ()
{
int a, base, power;
printf ("\nBase:");
scanf ("%d", &base);
printf ("\nExponent:");
scanf ("%d", &power);
a = pow (base, power);
printf ("\nAnswer: %d", a);
return 0;
}
Related
i just started studying programming at school, and i am so lost already. Here's the task:
Answer all 3 tasks in a separate file and return it.
What is the result if the format string (control character) of the printf function is %.3f and the number to be printed is
456.87654321
0.17023
443.14159
How do i even do this? My code is this but it's obviously wrong.
#include <stdio.h>
int main() {
int num1, num2, num3;
printf("Give a number 1\n");
scanf("%i", &num1);
printf("Answer is on %.3f", &num1);
return 0;
}
It gave me 0 as answers or 0.000 what ever. Only 0's.
I don't know what to do really, my teacher is already on another subject and has no time helping me much.
This source code:
#include <stdio.h>
int main(void)
{
printf("%.3f\n", 456.87654321);
printf("%.3f\n", 0.17023);
printf("%.3f\n", 443.14159);
}
produces this output:
456.877
0.170
443.142
You declared num1 to be an int (integer... whole numbers only).
Then you read that number from the keyboard.
I'm guessing you're entering 456.87654321.
(hint, that is not a whole number. it will not fit in an int)
Then you try to print it out with:
printf("Answer is on %.3f", &num1);
This has several problems.
%.3f is for printing out doubles, not ints.
You passed the "address" (& sign) of the number you wanted to print. Just pass the variable directly
Fixing up your code, I get:
#include <stdio.h>
int main() {
double num1; // Declare DOUBLE, not INT
printf("Give a number 1\n");
scanf("%f", &num1); // Get a DOUBLE from input, not an INT
printf("Answer is on %.3f", num1); // Remove the & (address)
return 0;
}
I am a beginner in C and would like to know what's the problem about my code here :
#include "stdio.h"
int main(void)
{
int a;
printf("Please input an integer value: ");
scanf("%d", &a);
printf("You entered: %d\n", a);
return 0;
}
My problem is that I have to type a value before having any consol output, for example if I type 7, I get this console output : Please input an integer value: You entered: 7
I tried the exact same code in another computer and it worked pretty well, I guess it's a buffer problem ? but I have no idea how to fix it.. Any ideas please ?
As other already mentioned, to guarantee that that line is going to be printed at that point in your code you can flush the standard output like this,
#include "stdio.h"
int main(void)
{
int a;
printf("Please input an integer value: ");
fflush(stdout);
scanf("%d", &a);
printf("You entered: %d\n", a);
return 0;
}
you can read this for more details, Why does printf not flush after the call unless a newline is in the format string?
updated thanks to #Osiris comments
so I'm currently running my C program in NetBeans IDE 8.1, but as soon as I tried using the scanf function, I began running into issues. I have MinGW download and have added C:\MinGW\bin; to my path environment variable. I looked up and found that I should be running external terminal to use scanf but I am receiving this error. Does anyone have any idea how to fix this. I'm pretty new to C and this IDE so simpler instructions would be appreciated.
Here is the code:
#include <stdio.h>
int main()
{
int int1, sum, int2;
printf("Enter\n");
scanf("%d", int1);
printf("Enter\n");
scanf("%d", int2);
sum = int1 + int2;
printf("sum is %d", sum);
return 0;
}
You need to pass an int *, not an int into scanf. This is because scanf must fill in each argument in the variable argument list. Your code should be
int main()
{
int a, b;
printf("Enter first number\n");
scanf("%d", &a);
printf("Enter second number\n");
scanf("%d", &b);
printf("sum is %d\n", a + b);
return 0;
}
I was writing a C Program to find if a number is prime or not. Everytime I run it and enter a number, the value of the input changes. PLease point out the loopholes.
#include<stdio.h>
#include<conio.h>
int main()
{
int x;
int y;
y=getchar();
for(x=2;x<y;++x){
if(y%x != 0 && y!=x)
printf(" THE NUMBER %d is A PRIME \n", y);
else
printf(" \r THE NUMBER %d IS NOT A PRIME", y);
break;
}
}
I use the Code::Blocks IDE with GCC Compiler
As the name implies, getchar() gets a single character from standard input. For example, if you enter 3, the y gets the ASCII code of the character '3', which is obviously not what you want.
Try scanf:
scanf("%d", &y);
getchar returns the ASCII code of a single character. Consequently, your program picks up the ASCII code of the first character of the number you input and checks if it is prime.
Instead, you need to read an integer:
scanf("%d", &y);
The complete program:
#include<stdio.h>
#include<conio.h>
int main()
{
int x;
int y;
scanf("%d", &y);
for(x=2;x<y;++x){
if(y%x != 0 && y!=x)
printf(" THE NUMBER %d is A PRIME \n", y);
else {
printf(" \r THE NUMBER %d IS NOT A PRIME", y);
break;
}
}
}
Note: You can stop when x >= sqrt(y)
Well, you are calling getchar() which is used to input a single character and this is what happens in your case:
getchar() returns a character.
Character is then converted into integer when you store it in variable of type int.
Hence that integer contains the ASCII of input character i.e. 3 will be stored as 51 that is the reason input changes.
What you need to do is to input an integer instead of character. Try this:
scanf("%d", &y);
Hope this helps.
First answers are correct about input for y:
scanf("%d", &y);
Also, please note that you should loop until square root of x, and not more if your want to optimize your algorithm (I won't demonstrate here why, it's a mathematical property).
#include <stdio.h>
#include <math.h>
// ...
int x;
int x_max;
int y;
scanf("%d", &y);
x_max = (int)floor(sqrt(y));
for(x=2;x<=x_max;++x){
// ...
The program is very simple , it gives the greatest common divisor as the output.I have verified my algorithm.The compiler issues no error ,but still it wont produce any output.
#include<conio.h>
#include <stdio.h>
int gcd(int ,int );
int main()
{
int a,b,j;
printf("enter two numbers");
scanf("%d\n",&a);
scanf("%d\n",&b);
j=gcd(a,b);
printf("gcd is %d",j);
getch();
return 0;
}
int gcd(int x, int y)
{
int temp,c;
if(x<y)
{
temp=x;
x=y;
y=temp;
}
if(y<=x&&(x%y==0))
return y;
else
{ temp=x%y;
c=gcd(y,temp);
return c;
}
}
This could be due to buffering of the output. Add \n to your printfs and see if it fixes it:
printf("enter two numbers\n");
printf("gcd is %d\n",j);
Alternatively, you can add calls to fflush(stdout) to flush the output buffer:
printf("enter two numbers");
fflush(stdout);
printf("gcd is %d",j);
fflush(stdout);
Other than that, it (almost) works as intended on my setup:
enter two numbers
4783780
354340
1
gcd is 20
The only thing is that the \n forces it to read an extra character. (which I chose to be 1)
The problem is
scanf("%d\n",&a);
scanf("%d\n",&b);
Delete the \n, just
scanf("%d",&a);
scanf("%d",&b);
is OK
This line:
printf("enter two numbers");
doesn't print a newline character (\n), and so the output isn't flushed to the console.
Try adding this after the printf:
fflush(stdout);
scanf("%d\n",&a);
scanf("%d\n",&b);
to
scanf("%d%*c",&a);
scanf("%d%*c",&b);