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;
}
Related
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;
}
I am new to c ,while i am writing a basic program in c ,it is showing two answers ..1)when i declare and intialize variables separately 2)when I declare and initialize variable in a same line.can any one tell me my mistake please?
#include <stdio.h>
#include <stdlib.h>
void sum()
{
printf("enter the numbers to be added\n");
int x=scanf("%d",&x);
int y=scanf("%d",&y);
int sum=(x+y);
printf("the sum of two numbers is %d\n",sum);
}
int main()
{
printf("welcome to addition calculator\n");
sum();
return 0;
}
I am getting 2 as answer when i gave 3 and 4 as inputs
scanf("%d", &x) will store the read number into x. It will return the number of successfully read fields (1 in your case). If you assign that return value to x afterwards, you overwrite whatever the user entered with that 1. And 1 + 1 produce 2.
Solution:
int x;
int y;
scanf("%d",&x);
scanf("%d",&y);
As David reminds in comments, you might want to check that all fields were read successfully. For example, in your case, if you enter a non-digit, scanf will not resolve the %d field as successful, and will return 0. You can test this result to make sure the user did what they were supposed to do:
int x;
int y;
while (scanf("%d", &x) != 1) {
printf("Enter a NUMBER, you illiterate buffoon!\n");
}
while (scanf("%d", &y) != 1) {
printf("Enter a NUMBER! You managed with %d, how is this suddenly hard now?!\n", x);
}
scanf() function returns 1 if it scan successfully otherwise it return 0. That's why when you put an integer to x, scanf() return 1 and assign it to x(x=1). Same for y(y=1).
As x=1 and y=1.
sum = 2
Hello guys I started learning C few weeks ago and I am trying to make my first useful program, I am trying to create a squared equation calculator but no matter what I type in the input it always outputs "no solution2" can anyone take a look and help me ?
examples for input :
0 0 0=
1 4 1=
#include <stdio.h>
#include <math.h>
int main()
{
printf("enter a\n");
double a; scanf("%1f", &a);
printf("\nenter b\n");
double b; scanf("%1f", &b);
printf("\nenter c\n");
double c; scanf("%1f", &c);
if (a==0)
{
if (b==0)
{
if (c==0)
printf("x can be every number\n");
else
printf("no solution1\n");
}
else
{
printf("x equals %.2f\n", ((-1)*c) / b);
}
}
else
{
double delta = (b*b)-(4*(a*c));
if (delta>0)
{
double sqrtDlt = sqrt(delta);
printf("x1 = %4.2f\n", (((-1)*b) + sqrtDlt) / 2 * a);
printf("x2 = %4.2f\n", (((-1)*b) - sqrtDlt) / 2 * a);
}
else
printf("no solution2\n");
}
return 0;
}
For double use specifier %l(ell)f not %1(one)f in scanf.
Note that this is one place that printf format strings differ substantially from scanf (and fscanf, etc.) format strings. For output, you're passing a value, which will be promoted from float to double when passed as a variadic parameter. For input you're passing a pointer, which is not promoted, so you have to tell scanf whether you want to read a float or a double, so for scanf, %f means you want to read a float and %lf means you want to read a double.
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){
// ...
I would like to avoid 'floating point error in this code.
A purpose of this code is to gain 'The average of whole numbers' but the number of 'whole numbers' is limited by the input of users. Please help me.
#include <stdio.h>
int main(void)
{
int num=0;
int limit;
int result=0;
printf("number of integer: ");
scanf("&d", &limit);
while(num<limit)
{
int output;
printf("Input integer : ");
scanf("%d", &output);
result += output;
num++;
}
printf("average of total integer: %d \n", result/limit);
return 0;
}
Thank you for reading.
When you divide 2 integers, the result is also an integer.
To return a float, you need to cast one of the arguments as a float.
So your last line becomes
printf("average of total integer: %f \n", result/(float)limit);
As the result of two integer dividing is also an integer,so it as
printf("average of total integer: %f \n", result/(float)limit);
when you type cast the variable limit to float what happens is that result will be implicitly converted to float and so the result is a float.