Thursday, January 1, 2015

loop(8)

1:
#include <stdio.h>
///***summation of even numbers:
///***2+4+6+8+.....+n
int main()
{
 int a,b,sum=0;
 printf("please enter a number:");
 scanf("%d",&b);
 for(a=2;a<=b;a=a+2){
    sum=sum+a;
}
    printf("The sum of even number is:%d",sum);
return 0;
}

input: 6
output:12        ///***2+4+6=12

2:
#include <stdio.h>
///summation of odd numbers:
///1+3+5+7+9+11.........+n

int main()
{
 int a,b,sum=0;
 printf("please enter a number:");
 scanf("%d",&b);
 for(a=1;a<=b;a=a+2){
    sum=sum+a;
}
    printf("The sum of odd number is:%d",sum);
return 0;
}
 

input: 6
output:9       ///***1+3+5=9

3:
#include <stdio.h>
///1^2+2^2+3^2+.......+n^2
int main()
{
int a,b,sum=0;
printf("please enter a number:");
while(scanf("%d",&b) !=EOF){ ///***here EOF is END OF FUNCTION
for(a=1;a<=b;a++){
   sum=sum+a*a;
   }
printf("The sum is=%d ",sum);
}
return 0;
}
 
input: 7
output: 140   ///***1^2++2^2+3^2+4^2+5^2+6^2+7^2=140

No comments:

Post a Comment