Computer Science, asked by Snjupq, 5 months ago

APL to add 10 natural numbers and store the result in $500

Answers

Answered by derenamanjha1234
0

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.

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.

Answered by ishmeetkaur15
0

ANSWER :-

APL to add 10 natural numbers and store the result in $500

Similar questions