Calculating n! and x^n and proceeding the calculation [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 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.

Related

Logical error in simple addition program in C [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 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;
}

Output coming unreasonably large in C for the equation x+ y^2 + z^3 [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
//Experiment 3 Task 4 b.)
#include<stdio.h>
void main(){
int x,y,z,ans;
printf("\nEnter the value of x. ");
scanf("%i",&x);
printf("\nEnter the value of y. ");
scanf("%i",&y);
printf("\nEnter the value of z. ");
scanf("%i",&z);
ans = (x + (y^2) + (z^3));
printf("\nAnswer = %i",&ans);
}
OUTPUT:
Enter the value of x. 1
Enter the value of y. 2
Enter the value of z. 4
Answer = -483189484
There are two issues with your code:
With regards to large value, you are printing address rather than actual answer. So remove the & when you print
The ^ operator in C represents the bitwise XOR. So you can either use the pow function, (dont forget to put #include <math.h>) or write your own version of it to get correct results.

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.

no answer from simple subtraction code in C [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
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.
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.
Improve this question
i wrote code, and it gives me answers like 0.00 and 1.00, not actual math answer. Where i made mistake? (im begginer, dont scream on me :) )
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a,b;
float x;// score
// Request 1
printf("Zadanie 1(12)\n");
// Calculate the square root of the given interval
printf("Oblicz pierwiastek rownania w podanym przedziale\n");
// Give me a number a
printf("Podaj liczbe a:\n");
scanf("%d",&a);
// Give me a number b
printf("Podaj liczbe b:\n");
scanf("%d", &b);
x=&a-&b;
//answer
printf("%f", x);
system("PAUSE");
return 0;
}
x = a - b
not
x = &a - &b
Explanation: The & operator gives you the memory address of a, which you need to give to scanf, so that it can place data there. But you do math on the actual value of a.. which is just a.
because of pointer arithmetic:
x = &a - &b;
computes the distance between the addresses of a and b, which are probably close since they're auto variables of the same type declared close-by. My guess is that you could get 1 or -1 (or any other integer value but not 0 since a and b are located in 2 separate addresses) then put in a float.
(the difference of addresses of 2 consecutive integers is 1, independently of the size of the integer)
You need of course to do:
x = a - b;
You've been misled by the fact that scanf needs a pointer on a or b to be able to write a value when reading the input.

Program working fine for smaller values but throwing Segmentation Fault for bigger values [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 7 years ago.
Improve this question
In the following program when the value of N is less than 100 the program is executing perfectly but for bigger values of N its showing segmentation fault.I sit because of less memory or anything wrong with program??
#include<stdio.h>
int main()
{
int N,iteration,MAX_ITERATONS;
int i,j,k,n,index,boundary1,boundary2;
double h[2][100][100];
int current = 0;
int next = 1;
printf("Enter the number of points\n ");
scanf("%d", &N);
boundary1=0.4*N;
boundary2=(0.6*N);
printf("The boundary is between %d and %d .\n",boundary1,boundary2);
for(k=0;k<2;k++)
{
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
if((i==0)&&(j>=boundary1&&j<boundary2))
h[k][i][j]=100;
else
h[k][i][j]=20;
}
}
}
printf("Initial Values\n");
index = N/10;
for(i=0;i<N;)
{
for(j=0;j<N;)
{
printf("%.2f\t",h[0][i][j]);
j=j+index;
}
i=i+index;
printf("\n");
}
}
When N > 100, h is accessed to an index greater than 100, inside the nested for loop
h[k][i][j]=100;
but h is defined as
double h[2][100][100];
You are going out of bounds for h
If you want N as greater than 100 you need to redefine h or malloc it.

Resources