Logical error in simple addition program in C [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 months ago.
Improve this question
I ran the following program in vs code:
int main() {
int a;
int b;
int c=a+b;
printf("enter the value of a:");
scanf("%d",&a);
printf("enter the value of b:");
scanf("%d",&b);
printf("the value of their sum is: %d",c);
return 0;
}
i am getting this as output:
enter the value of a:6
enter the value of b:7
the value of their sum is: 8129784
the mathematics doesn't add up please can someone point out the error.

int c=a+b;
On this line, variable a and b do not have known values yet!
They just have whatever random value might be in memory
They do not get assigned values until the scanf statements.
So you have added together two unknown values.
To fix it:
int main() {
int a;
int b;
printf("enter the value of a:");
scanf("%d",&a);
printf("enter the value of b:");
scanf("%d",&b);
int c=a+b; // A and B have values now. NOW you can add them together.
printf("the value of their sum is: %d",c);
return 0;
}

Related

Error in a recursive function to print out a series [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
So, I am supposed to print out the following series using a C program:
I decide to use a recursive approach and come up with this code:
#include<stdio.h>
float series(int n, float x)
{
float prod;
if(n==1)
return 1;
else
{
prod = (x*x)/((2*n-2)(2*n-3)); //line 10
return prod*series(n-1,x);
}
}
int main()
{
int n;
float x;
printf("\n Enter the values of n and x : ");
scanf("%d %f",&n,&x);
printf("\n The series is :");
for(int i=1;i<=n;i++)
printf(" %f,",series(i,x));
printf("\n\n");
return 0;
}
But this gives an error on line 10:
error: called object type 'int' is not a function or function pointer
I don't see any syntactical error on the line. It would be great if you could point it out.
Thank You!
prod = (x*x)/((2*n-2)(2*n-3)); //line 10
should be
prod = (x*x)/((2*n-2)*(2*n-3)); //line 10
The compiler sees this as a function call where 2*n-2 is the function pointer and 2*n-3 is the argument.

Calculating n! and x^n and proceeding the calculation [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
Write a C program to calculate (x^n)/(n!) where x is a floating point number and n is an integer greater than or equal to zero.
I coded the following:
#include<stdio.h>
#include<math.h>
void main()
{
float x,p;
int i,n,f=1;
printf("Enter the value of x,n\n");
scanf("%d %d",&x,&n);
if(n>0)
{
for(i=1;i<=n;i++)
{
f=f*i;
}
p=(float)pow(x,n)/f;
printf("The value of p is %.3f",p);
}
if(n==0)
{
p=(float)pow(x,n)/1;
printf("The value of p is %d",p);
}
getch();
}
But this is not running well. Where have I gone wrong?
PS: Edit
In your question I have recognize 3 problems.
main problem is scanf("%d %d",&x,&n); should be change into scanf("%f %d",&x,&n);
because x is `float type #dragosht has mentioned it.
printf("The value of p is %d",p); should be correct as printf("The value of p is %f",p); beacause p is also float type.
It is better to set p = 0; at the beginning because you did not assign value to p using keyboard. There for some times you will get corrupted values because of this.

Why can't I get the program to answer anything but 0.00? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
The program runs fine but keeps giving me an answer of 0.00. I have floated the numbers and answer and it asks for the first and second number but I cannot see where I have gone wrong.
#include <stdio.h>
#include <math.h>
int main()
{
float sub;
float num1 = 18.73;
float num2 = 20.00;
printf("Please enter the total of the meal: \n");
scanf("%f", &num1);
printf("Please enter the amount of money you have: \n");
scanf("%f", &num2);
sub = num2 - num1;
printf("\nYour change is: %.2f\n", &sub);
return 0;
}
You're printing the address of sub:
Do this:
printf("\nYour change is: %.2f\n", sub);
instead of:
printf("\nYour change is: %.2f\n", &sub);
In your code I have found 2 mistakes
Don't assign greater than 0 numbers to variables when you use scanf
float num1 = 0; //use this
float num2 = 0;
When you output a number don't use & in printf. That is the reason you were given 0.
printf("\nYour change is: %.2f\n", sub);
Finally, you don't need to use #include <math.h> in these king of programmes

Unable to understand the meaning of errors in C language using Turbo C. I've coded this code [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
This is what I've coded - the exchange of 2 numbers in c language using turbo c. I've tried real hard to google the solution but the explanation is not understandable as I'm a mere beginner. I'm having my internals on 20th and this is what they might ask along with 40 other programs.....
You should write printf("please enter"); instead of printf{"please enter"};
The same with scanf.
When you see Statement missing ; error, you definitely have an language error.
Functions take arguments between () and {} are used for blocks of code. For example:
if (1) {
printf("Printing\n");
printf("Printing again\n");
}
Another thing is, that we assign value to the left side of =.
a = a-b
a = a+b
Your code should be:
#include<stdio.h>
void main() {
int a,b;
printf("Enter two (2) numbers, please\n");
scanf("%d%d", &a, &b);
a = a+b; //assign a to the sum of current a and b
b = a-b;
a = a-b;
printf("The numbers a, b after some calculations are: a=%d and b=%d\n", a, b);
}
Use parenthesis () instead of braces {} in your functions printf() , scanf().
printf("please enter 2 nos");
scanf("%d %d" &a,&b);
printf("our exchanged numbers are a = %d" and b=%d" a,b);
And you can't assign value to a expression, it will give lvalue error. So to remove this error change,
a-b = a change it to a=a-b and change all the expressions.

Please check my code. won't return a right value [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
#include <stdio.h>
int f_totprice(int,int,int,int);
int main()
{
int menu_1;
int menu_2;
int menu_3;
int menu_4;
int totprice;
int received_money;
while (1){
printf("what do you want to order?\n");
printf("pizza: ");
scanf("%d",&menu_1);
printf("cheese: ");
scanf("%d",&menu_2);
printf("curry: ");
scanf("%d",&menu_3);
printf("soup: ");
scanf("%d",&menu_4);
printf("======================================\n");
totprice=f_totprice(menu_1,menu_2,menu_3,menu_4);
printf("total price is %d\n",totprice); /* this part returns a wrong value */
printf("received ");
scanf("%d",&received_money);
printf("Your change is %d",received_money-totprice);
}
}
int f_totprice (int a, int b, int c, int d)
{
int total;
char price[]={500,1000,1500,2000};
total=a*price[0]+b*price[1]+c*price[2]+d*price[3];
return total;
}
so, i just wrote a code to make a simple menu and the price part doesn't return the total value of my function below.. it keeps saying "the total value is -600,-300,-190" etc instead of integers. in my eyes there is nothing wrong with it. please help? :(
char price[]={500,1000,1500,2000};
Can you figure out why this cannot work?
You should find out how to turn on warnings on your compiler.
You are using char array instead of interger array, and printing the output in integer type (%d), Hence it is printing the ASCII equivalent of the sum , Please change character array to integer array.
int price[]={500,1000,1500,2000};
Your problem lies here
int f_totprice (int a, int b, int c, int d)
{
int total;
char price[]={500,1000,1500,2000};
total=a*price[0]+b*price[1]+c*price[2]+d*price[3];
return total;
}
Why are you using a char array,
Use int array

Resources