// WHILE LOOP
#include<stdio.h>
int main()
{
int n,i=1,sum=0;
printf("ENTER THE VALUE OF N:");
scanf("%d",&n);
printf("First %d numbers are\n",n);
while (i<=n)
{
printf("%7d ",i); //%7 to print the digit within 7 slots
sum = sum +i;
i++;
}
printf("\nSum = %d\n",sum);
return 0;
}
// DO WHILE LOOP
#include<stdio.h>
int main ()
{
int n,i=1,sum=0;
printf("ENTER THE VALUE OF N:");
scanf("%d",&n);
printf("First %d numbers are\n",n);
do
{
printf("%7d,",i);
sum=sum+i;
i++;
}
while(i<=n);
printf("\n\nSum=%d\n",sum);
return 0;
}
//FOR LOOP
#include<stdio.h>
int main()
{
int n,i=1,sum=0;
printf("ENTER THE VALUE OF N:");
scanf("%d",&n);
printf("First %d numbers are\n",n);
for(i=1;i<=n;i++)
{
printf("%7d",i);
sum=sum+i;
}
printf("\n\nSum = %d",sum);
return 0;
}
No comments:
Post a Comment