1 + 2 + 4 + 8 +................+ n

//1 + 2 + 4 + 8 + 16 + 32 +................+ n
Program:

#include<stdio.h>
int main()
{
int n,i,sum=0,k=1;
printf("Enter n: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+k;
k=k*2;
}
printf("sum= %d",sum);
return 0;
}

Output:



No comments:

Post a Comment

x^y using recursion

Program: #include<stdio.h> int power(int a,int b); int main() { int x,y,ans; printf("Enter x and y:\n "); scanf(...