4. (C) Write a program to find the sum of numbers from 1 to 100

4. (C) Write a program to find the sum of numbers from 1 to 100
Write a program to find the sum of numbers from 1 to 100


#include <stdio.h>
int main()
{
int n, i, sum = 0;
printf("Enter a positive integer: ");
scanf("%d",&n);
i = 1;
while ( i <=n )
{
sum += i;
++i;
}
printf("Sum = %d",sum);
return 0;
}
#include <stdio.h>
int main()
{
int n, i, sum = 0;
printf("Enter a positive integer: ");
scanf("%d",&n);
for(i=1; i <= n; ++i)
{
sum += i; // sum = sum+i;
}
printf("Sum = %d",sum);
return 0;
}


#include <stdio.h>
int main()
{
int n, i, sum = 0;
do {
printf("Enter a positive integer: ");
scanf("%d",&n);
}
while (n <= 0);
for(i=1; i <= n; ++i)
{
sum += i; // sum = sum+i;
}
printf("Sum = %d",sum);
return 0;
}
4. (C) Write a program to find the sum of numbers from 1 to 100 4. (C) Write a program to find the sum of numbers from 1 to 100 Reviewed by admin on December 23, 2019 Rating: 5

No comments:

Powered by Blogger.