Course Content
Detailed Content of Programming in C
0/1
Introduction
0/1
Structure of C program
0/1
Answers of ‘C’ Pointers Programs
0/1
About Lesson

Calculate the Sum of N Numbers using a While Loop.

#include <stdio.h>

int main() {
int N, sum = 0, i = 1;

// Taking input for N
printf(“Enter a positive integer N: “);
scanf(“%d”, &N);

// Calculating the sum of numbers from 1 to N using a while loop
while (i <= N) {
sum += i;
i++;
}

printf(“Sum of numbers from 1 to %d: %dn”, N, sum);

return 0;
}