A program to find sum of n numbers by using while loop in c++.

This program add up to n numbers which will be provided by user. Firstly user will enter a fix number to indicate how many numbers he wants to add and then he will enter n numbers.

 

Let’s open turbo c/c++ and write the following code:

 

 

\ Code:

 

 


#include<iostream.h>

#include<conio.h>

void main()

{

float   n,sum=0;

clrscr();

cout<<”enter how many numbers you want to add ? : ”;

cin>>n;

cout<<”enter “<<n<<”numbers then enter 0 \n”;

while(n!=0)

{

cin>>n;

sum=sum+n;

}

cout<<”total sum is : “<<sum;

getch();

}

 

 

Now, Run the program and see output:

 

 

Output:

 

 

output

 

 

Happy coding.

 

 

 

 

 

Tags: , , ,