You are on page 1of 2

Sum of Odd Num (Sum=1+3+5+......

+nth term)

#include<stdio.h>
main()
{
int n,sum=0,i;
printf("\n Enter the value of n:");
scanf("%d",&n);
while(i<=n)

{
sum=sum+i;
i=i+2;
}
printf("\n Sum of odd numbers from 1 to %d is:%d",sum);

return 0;
}

Output 1:
Enter the value of n:5

Sum of odd numbers is:9

Output 2:
Enter the value of n:20

Sum of odd numbers is:100


Sum of n^3 numbers (sum=13+23+.......+n3)

#include<stdio.h>
#include<math.h>
main()
{
int n,sum=0,i;
printf("\n Enter the value of n:");
scanf("%d",&n);
i=1;
while (i<=n)
{
sum=sum+pow(i,3);
i=i+1;
}
printf("\n Sum of the series from 1^3 to %d^3 is:%d",sum);
return 0;
}

Output:

Enter the value of n:2

Sum of the series from 1^3 to 9^3 is:2024

You might also like