Calculate power of all the numbers till n-1 - c

Given n, the program should calculate 1^1 + 2^2 + 3^3 + ... till n-1^n-1. Below is my code, in which there is one function inside while loop which and the passed value is from n-1 in the function. The function definition has two variables which return the ans. Output is wrong always 1.
#include <stdio.h>
#include <stdlib.h>
int power(int x, int y)
{
int la, ans;
if(y==0)
return 1;
else
la= (x*power(x, y-1));
ans+=la;
return ans;
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int n, m, a, b, res, res1;
scanf("%d%d", &n, &m);
while(n-- && n>0)
{
a = power(n-1, n-1);
}
printf("%d", a);
}
return 0;
}

Some problems in your code.
As pointed in another answer, your power function was broken:
ans was not initialized
{ } were missing after the else
in the while, you compute x^x, but you forget the result, whearas you
should sum it.
first thing you do in while loop is to decrease n and to compute power(n-1, n-1)
that sound not logical.
Hence, your corrected code could be:
#include <stdio.h>
#include <stdlib.h>
int power(int x, int y)
{
if(y==0)
return 1;
else
return x*power(x, y-1);
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int n, m, b, a = 0;
scanf("%d%d", &n, &m);
while(n>1)
{
--n;
b = power(n, n);
a += b;
printf("%d^%d -> %3d\n",n, n, b);
}
printf("sum= %d", a);
}
return 0;
}
Gives for n = 6:
5^5 -> 3125
4^4 -> 256
3^3 -> 27
2^2 -> 4
1^1 -> 1
sum=3413

C uses braces to form blocks, your power() function looks like it's wanting to use indentation like in Python.
It should probably be:
int power(int x, int y)
{
int la, ans;
if(y==0)
return 1;
else
{
la= (x*power(x, y-1));
ans+=la;
return ans;
}
}
Of course since the first if has a return, the else is pointless, and you can simplify the code:
int power(int x, int y)
{
if (y==0)
return 1;
return x * power(x, y-1);
}
The variable ans was never assigned to, that looked broken so I simplified it out.
Of course this is susceptible to integer overflow.

Related

return code misunderstanding

#include<stdio.h>
int calsum(int x,int y,int z);
int main()
{
while(1)
{
int a, b, c, sum;
printf("Enter any3 numbers");
scanf("%d%d%d", &a, &b, &c);
sum = calsum(a, b, c);
printf("sum=%d\n", sum);
}
}
int calsum (int x, int y, int z)
{
int d;
d = x + y + z;
if(d > 2)
return d;
else
d = 1;
return;
}
when I am giving input as -1 1 0 my output should be 1 but it is giving 0
why?
it is all about adding three numbers
int calsum (int x,int y,int z)
{
return ;
}
Your function is declared and defined to return an int, but your return statement is expressionless. It's a language constraint violation.
The behavior of your program is undefined. Funny results is a possible outcome in this case.
Update your calsum function as below. You are assigning d=1 in else part but not returning it.
int calsum (int x,int y,int z){
int d;
d=x+y+z;
if(d>2)
return d;
else
return 1;
}

How to print this series x-(x^3/3!)+(x^5/5!)-(x^7/7!)+...(x^n/n!) in C/C++?

I wish to write a program which calculates the series x-(x^3/3!)+(x^5/5!)-(x^7/7!)+...(x^n/n!) by taking x and n as user inputs.
This is what i've tried, and well there's no output when I enter the values for x,n:
#include<stdio.h>
#include<math.h>
//#include<process.h>
#include<stdlib.h>
double series(int,int);
double factorial(int);
int main()
{
double x,n,res;
printf("This program will evaluate the following series:\nx-(x^3/3!)+(x^5/5!)-(x^7/7!)+...(x^n/n!)\n");
printf("\nPlease enter a value for x and an odd value for n\n");
scanf("%lf%lf",&x,&n);
/*if(n%2!=0)
{
printf("Please enter a positive value!\n");
exit(0);
}*/
res=series(x,n);
printf("For the values you've entered, the value of the series is:\n %lf",res);
}
double series(int s, int t)
{
int i,sign=1; double r,fact,exec;
for(i=1;i<=t;i+2)
{
exec=sign*(pow(s,i)/factorial(i));
r+=exec;
sign*=-1;
}
return r;
}
double factorial(int p)
{
double f=1.0;
while(p>0)
{
f*=p;
p--;
}
return f;
}
When I enter values for x and n, it simply shows nothing.
While I've written in C, C++ solutions are also appreciated.
Output window in code::blocks
The loop
for(i=1;i<=t;i+2)
in the function series() is an infinite loop when t >= 1 because i isn't updated in the loop. Try changing + to += and use
for(i=1;i<=t;i+=2)
instead. Also it seems you should use type int for x and n in the function main() because the arguments of series() is int. Don't forget to change the format specifier when changing their types.
Thanks to all those who helped. Here's the final working code:
#include<stdio.h>
#include<math.h>
#include<process.h>
#include<stdlib.h>
double series(int,int);
double factorial(int);
int main()
{
int x,n; double res;
printf("This program will evaluate the following series:\nx-(x^3/3!)+(x^5/5!)-(x^7/7!)+...(x^n/n!)\n");
printf("\nPlease enter a value for x and an odd value for n\n");
scanf("%d%d",&x,&n);
if(n%2==0)
{
n=n-1;
}
res=series(x,n);
printf("For the values you've entered, the value of the series is:\n%lf",res);
}
double series(int s, int t)
{
int i,sign=1; double r=0.0,fact,exec;
for(i=1;i<=t;i+=2)
{
exec=sign*(pow(s,i)/factorial(i));
r+=exec;
sign*=-1;
}
return r;
}
double factorial(int p)
{
double f=1;
while(p>0)
{
f*=p;
p--;
}
return f;
}
in loop we step by two for getting odd numbers.by multiplying the current temp variable by the previous temp variable in the loop with neccesary terms like x square and dividing by i*(i-1) i.e for factorial and multiply with -1 i.e to achive negavtive number alternatively. by using this temp variable and adding it to sum variable in every iteration will give us answer.
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int n, x;
cout << "enter x and no.of terms: ";
cin >> x >> n;
float sum = 0, temp = x;
for (int i = 3; i < 2 * n + 2; i = i + 2)
{
temp = ((-1 * temp) *(x*x)) / i*(i-1);
sum = sum + temp;
}
cout << x + sum;
return 0;
}
// series x-(x^3/3!)+(x^5/5!)-(x^7/7!)+...(x^n/n!)
#include<stdio.h>
#include<math.h>
double factorial (int);
double calc (float, float);
int
main ()
{
int x, deg;
double fin;
printf ("x-(x^3/3!)+(x^5/5!)-(x^7/7!)+...(x^n/n!)\n");
printf ("Enter value of x\n");
scanf ("%d", &x);
printf ("highest degree in denom i.e., 1 or 3 or 5 whatever, it should be odd .\n");
scanf ("%d", &deg);
fin = calc (x, deg);
printf ("the summation of series =%1f\n", fin);
return 0;
}
double calc (float num, float res)
{
int count, sign = 1;
double rres = 0;
for (count = 1; count <= res; count += 2)
{
rres += sign * (pow (num, count) / factorial (count));
sign *= -1;
}
return (rres);
}
double factorial (int num)
{
int count;
double sum = 1;
for (count = 1; count <= num; count++)
{
sum *= count;
}
return (sum);
}

Calculating power raised to a number

I am calculating the power of x raised to n. I can't understand one thing: why is it showing segmentation fault when I am both declaring and initializing the temp variable at the start? I do know what segmentation fault is, but why is it showing.
#include<stdio.h>
int power(int x,unsigned int y)
{
int temp=power(x,y/2);
if(y==0)
return 1;
if(y%2==0)
return temp*temp;
else
return x*temp*temp;
}
//Driver function
int main(int u, int v)
{
printf("Enter the value of u and v");
scanf("%d %u",&u,&v);
printf("%d",power(u,v));
return 0;
}
You will recurse infinitely. You need a small adjustment [please pardon the gratuitous style cleanup]:
#include <stdio.h>
int
power(int x, unsigned int y)
{
//int temp = power(x, y / 2);
if (y == 0)
return 1;
int temp = power(x, y / 2);
if (y % 2 == 0)
return temp * temp;
else
return x * temp * temp;
}
// Driver function
int
main(int argc,char **argv)
{
int u;
unsigned int v;
printf("Enter the value of u and v");
scanf("%d %u", &u, &v);
printf("%d\n", power(u, v));
return 0;
}

C - Powers of a^b for big numbers using Array

The question consists of two numbers, a and b, and the answer to it is the sum of digits of a^b.
I have written the below code. It is giving correct result in all cases. But when the input is as such a < b, then after giving the correct answer, I am getting segmentation fault.
I tried a lot to debug it but could not identify the issue. Any help would be greatly appreciated.
Thanks in advance..!
#include<stdio.h>
void power (int, int, int *product);
int main()
{
int a,b,product[200];
scanf("%d %d",&a, &b);
power(a,b,product);
return 0;
}
void power(int a, int b, int *product)
{
int i,m,j,x,temp,sum=0;
int *p = product;
*(p+0)=1; //initializes array with only 1 digit, the digit 1
m=1; // initializes digit counter
temp=0; //Initializes carry variable to 0.
for(i=1;i<=b;i++)
{
for(j=0;j<m;j++)
{
x = (*(p+j))*a+temp;
*(p+j)=x%10;
temp = x/10;
}
while(temp>0) //while loop that will store the carry value on array.
{
*(p+m)=temp%10;
temp = temp/10;
m++;
}
}
//Printing result
for(i=m-1;i>=0;i--)
sum = sum + *(p+i);
printf("\n%d",sum);
printf("\n");
}
I hope the below code does what you are trying to do. Which is simple and looks good too.
#include<stdio.h>
void power (int, int);
int main()
{
int a,b;
scanf("%d %d",&a, &b);
power(a,b);
return 0;
}
void power(int a, int b)
{
int c=1,sum=0;
while(b>0)
{
c = c*a;
b--;
}
printf("%d\n",c);
while(c!=0)
{
sum = sum+(c%10);
c =c/10;
}
printf("%d\n",sum);
}

Exercise in C to calculate sum from x to y

My teacher wants the sum of all numbers from x to y... like x+(x+1)+(x+2)...until y. But I think I'm doing something wrong here!
Can someone advice me what is wrong here?
#include <stdio.h>
int sum_naturals(int n)
{
return (n-1) * n / 2;
}
int sum_from_to(int m)
{
return (m-1) * m / 2;
}
void test_sum_naturals(void)
{
int x;
scanf("%d", &x);
int z = sum_naturals(x);
printf("%d\n", z);
}
void test_sum_from_to(void)
{
int x;
int y;
scanf("%d", &x);
scanf("%d", &y);
int z = sum_naturals(x);
int b = sum_from_to(y);
printf("%d\n", z);
}
int main(void)
{
//test_sum_naturals();
test_sum_from_to();
return 0;
}
Your code should in fact be:
int sum_naturals(int n)
{
return (n+1) * n / 2;
}
int sum_from_to(int m)
{
return (m+1) * m / 2;
}
Notice + instead of your -.
To find the sum just add in the function test_sum_from_to this line:
printf("The sum is %d", b-z);
Here's one solution :
#include<stdio.h>
int sum_naturals(int n)
{
return (n+1) * n / 2;
}
int sum_from_x_to_y(int x, int y){
return sum_naturals(y) - sum_naturals(x);
}
main()
{
printf ("Sum: %d \n",sum_from_x_to_y(5, 10));
printf ("Sum: %d \n",sum_from_x_to_y(0, 10));
printf ("Sum: %d \n",sum_from_x_to_y(0, 5));
return 0;
}
Note : sum from 0 to N is (n+1)*n/2 and not (n-1)*n/2

Resources