2. Write a program to calculate the sum of first 10 natural number.

on

For Loop

#include<iostream> using namespace std; int main() { int i = 1, sum=0; while(i<=10) { cout<<i<<"\t"; sum +=i; i++ ; // i = i +1 } cout<<"\nSum = "<<sum; return 0; }

For Loop

#include<iostream> using namespace std; int main() { int sum = 0; for(int i = 1; i<=10;i++) { cout<<i<<"\t"; } cout<<"\nSum = "<<sum; return 0; }

Do - While Loop

#include<iostream> using namespace std; int main() { int i = 1, sum=0; do { cout<<i<<"\t"; i++; }while(i<=10); cout<<"\nSum = "<<sum; return 0; }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s