Tuesday, 4 October 2022

sum of n number using for loop in c++

 This is the program to find the sum of n number in c using a for loop

What is for loop?

       The for loop is the easiest looping statement which allow code to be executed repeatedly.

It contains three different statements (initialization, condition or test- expression and update expression(s))

separated by semicolon(;). 


coding:

# include <iostream>
using namespace std;
int main()
{
    int n,sum=0,i;
    cout<<"Enter how many number to be add:";
    cin>>n;
    int num[n];
    for( i=1;i<=n;i++)
    {
        cout<<"Enter the number"<<i<<":\t";
        cin>>num[i];
        sum=sum+num[i];
    }
    cout<<"THe sum of numbers are\t"<<sum;
    return 0;
}


OUTPUT
:

Enter how many numbers to be add:4
Enter the number1: 34
Enter the number2: 3
Enter the number3: 34
Enter the number4: 4
THe sum fo numbers are 70



No comments:

Post a Comment

Word Basic Exercise

Exercise 1: Opening a document Show and hide the ribbon Move, show, and hide options of Quick Access toolbar Exercise 2: Cre...