Write a C language program to add numbers between 20 and 10 with the help of for loop?
its my exam please help i will mark you as a brillient....!!
Answers
Answer:
Sum of Natural Numbers Using for Loop
#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;
}
printf("Sum = %d", sum);
return 0;
}
The above program takes input from the user and stores it in the variable n. Then, for loop is used to calculate the sum up to n.
Explanation:
Sum of Natural Numbers Using while Loop
#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;
}
Output
Enter a positive integer: 100
Sum = 5050
In both programs, the loop is iterated n number of times. And, in each iteration, the value of i is added to sum and i is incremented by 1.
I think it may help u bro