Thursday, July 28, 2011

Display odd numbers upto a given number and display their sum and average

#include<stdio.h>
void main()

{
int i,num=1,n,sum=0;
float avg;
clrscr();
printf("Enter the limit : upto ");
scanf("%d",&n);
if((n%2)==0)
n=n/2;
else
n=(n+1)/2;
for(i=0;i<n;i++)
 {
 printf("%d\t",num);
 sum+=num;
 num+=2;
 }
avg=sum/n;
printf("\nSum = %d",sum);
printf("\nAverage = %f",avg);
getch();
}

0 comments:

Post a Comment