26. Program that sums the sequence of integers assuming that first integer read specifies the number of values remaining to be entered.
Chapter 6:
Looping Structures
Programming Exercise
Problem # 26:
Write a program that sums the sequence of integers assuming that first integer read specifies the number of values remaining to be entered. The program should read one value per input statement. A typical input sequence might be 5 100 200 150 300 500. The first integer 5 indicates that subsequent five values are to be summed.
Solution:
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int n1, n2, n3, sum=0;
cout<<"\nEnter a number:";
cin>>n1;
for(n2=1;n2<=n1;n2++)
{
cout<<"Enter a number:";
cin>>n3;
sum=sum+n3;
}
cout<<"\nThe sum of the subsequent values after first integer are:"<<sum<<endl;
getch();
}
#include<conio.h>
using namespace std;
int main()
{
int n1, n2, n3, sum=0;
cout<<"\nEnter a number:";
cin>>n1;
for(n2=1;n2<=n1;n2++)
{
cout<<"Enter a number:";
cin>>n3;
sum=sum+n3;
}
cout<<"\nThe sum of the subsequent values after first integer are:"<<sum<<endl;
getch();
}
Let me know in the comment section if you have any question.
Previous Post:
25. Program to add the first seven terms of the following series usign for loop: 1/1! + 2/2! + 3/3!
Comments
Post a Comment