C Program to Add First n Numbers using While loop

Write C Program to Add First n Numbers using While loop program to add two numbers

//WAP to add all numbers up to n

#include <stdio.h>

int main()
{
	int n, sum = 0;

	printf("Enter the number :--> ");
	scanf("%d", &n);

	int i = 1;

	printf("The sum of first %d number is :--> ", n);

	while(i <= n)
	{
		sum = sum + i;
		i++;
	}
	printf("%d",sum);
	return 0;

}

Output:

Enter the number :--> 5
The sum of first 5 number are :--> 15

≪ Previous | Next ≫