SlideShare a Scribd company logo
SJEM2231 STRUCTURED PROGRAMMING 
NAME: WAN MOHAMAD FARHAN BIN AB RAHMAN 
TUTORIAL 3/ LAB 3 (13rd OCTOBER 2014) 
(Use different loop structures for problems 2, 4 and 5, and understand how it works) 
QUESTION 1 
Write a program to find the given number is odd or even. First test whether the given number is non zero positive number. 
#include <iostream> 
using namespace std; 
int main() 
{ 
int number; 
ULANG: 
cout << "Please enter any positive integer: "; 
cin >> number; 
if(number > 0) 
{ 
cout << "The given number is non zero positive number"<<endl; 
} 
else 
{ 
cout << "The given number is not a positive number. Try again" <<endl; 
goto ULANG; 
} 
if(number % 2 ==0) 
cout <<"Thus, " << number << " is an even number" << endl;
else 
cout <<"Thus, " << number << " is an odd number" <<endl; 
return 0; 
} 
// Output : 
Please enter any positive integer: -1 
The given number is not a positive number. Try again 
Please enter any positive integer: 0 
The given number is not a positive number. Try again 
Please enter any positive integer: 5 
The given number is non zero positive number 
Thus, 5 is an odd number 
//Alternative method for question 1 
#include<iostream> 
using namespace std; 
int main() 
{ 
int n; 
cout << "Enter a number: "; 
cin >> n; 
if (n>0 && n%2==0) 
{ 
cout<<"nThe number is non zero positive number"<<endl; 
cout << "The number is even number"<<endl; 
} 
else if (n>0 && n%2!=0)
{ 
cout<<"nThe number is non zero positive number"<<endl; 
cout << "The number is odd number"<<endl; 
} 
else 
cout<<"nThe number is zero or negative number"<<endl; 
return 0; 
} 
//Output: 
Enter a number: 0 
The number is zero or negative number 
Enter a number: 8 
The number is non zero positive number 
The number is even number 
Enter a number: 11 
The number is non zero positive number 
The number is odd number 
QUESTION 2 
Write a program to print the multiplication table as shown below(lower triangular matrix). 
1 
2 4 
3 6 9 
4 8 12 16 
5 10 15 20 25 
… 
10 20 30 40 50 60 70 80 90 100
//Using for loop : 
#include <iostream> 
using namespace std; 
int main() 
{ 
for (int i=1 ; i<=10; i++) 
{ 
for(int j=1; j<=10; j++) 
{ 
if (j>i) break; 
cout << i*j << "t"; 
} 
cout << endl; 
} 
return 0; 
} 
// Or we can write this way, still using for loop but slightly different: 
#include <iostream> 
using namespace std; 
int main() 
{ 
for (int i=1 ; i<=10; i++) 
{ 
for(int j=1; j<=i; j++) 
{ 
cout << i*j << "t"; 
}
cout << endl; 
} 
return 0; 
} 
//Using while loop 
#include<iostream> 
using namespace std; 
int main() 
{ 
int i=1; 
while(i<=10) 
{ 
int j=1; 
while(j<=i) 
{ 
cout<<i*j <<"t"; 
j++; 
} 
cout <<endl; 
i++; 
} 
return 0; 
} 
//Using do while loop 
#include<iostream> 
using namespace std;
int main() 
{ 
int i=1, j=1; 
do 
{ 
do 
{ 
/*We may replace int j=1 here instead of declaring j=1 above */ 
cout << i*j <<"t"; 
j++; 
} while(j<=i); 
j=1; /*we can eliminate this j=1 if we declare int j=1 above */ 
cout<<endl; 
i++; 
} while(i<=10) 
return 0; 
} //The output we will get is just the same: 
1 
2 4 
3 6 9 
4 8 12 16 
5 10 15 20 25 
6 12 18 24 30 36 
7 14 21 28 35 42 49 
8 16 24 32 40 48 56 64 
9 18 27 36 45 54 63 72 81 
10 20 30 40 50 60 70 80 90 100
QUESTION 3 
Write a program that solves quadratic equations of the form ax2 + bx + c = 0, where a, b, and c are given real number and x is the unknown (This will NOT apply if a is zero, so that condition must be checked separately. The formula also fails to work (for real numbers) if the expression under the square root is negative) 
#include <iostream> 
#include <cmath> 
using namespace std; 
int main() 
{ 
// Variable Declarations 
double a, b, c; 
//Variable Inputs 
cout << "Enter the value of a: "; 
cin >> a; 
cout << "Enter the value of b: "; 
cin >> b; 
cout << "Enter the value of c: "; 
cin >> c; 
//Computations 
double discriminant = (pow(b,2) - 4*a*c); 
double positive_root = (((-b) + sqrt(discriminant))/(2*a)); 
double negative_root = (((-b) - sqrt(discriminant))/(2*a)); 
if (discriminant == 0) 
{ 
cout << "nThe discriminant is "<< discriminant << endl; 
cout << "The equation has a single root.n"; 
}
else if (discriminant < 0) 
{ 
cout << "nThe discriminant is "<< discriminant << endl; 
cout << "The equation has two complex roots.n"; 
} 
else 
{ 
cout << "nnThe discriminant is " << discriminant << endl; 
cout << "The equation has two real roots.n"; 
} 
//Final Root Values 
cout << "The roots of the quadratic equation are x = "; 
cout << negative_root<< "," << positive_root << endl; 
return 0; 
} //Output: 
Enter the value of a: 1 
Enter the value of b: 6 
Enter the value of c: 8 
The discriminant is 4 
The equation has two real roots. 
The roots of the quadratic equation are x = -4,-2 
//Alternative method for question 3 
#include<iostream> 
#include<cmath> 
using namespace std;
int main() 
{ 
double a, b, c, discriminant, root_1, root_2; 
cout<<"Please enter value of a: "; 
cin>>a; 
cout<<"nPlease enter value of b: "; 
cin>>b; 
cout<<"nPlease enter value of c: "; 
cin>>c; 
discriminant= (b*b)-(4*a*c); 
if (discriminant>0) 
{ 
cout<<"nThe equation has 2 distinct root" <<endl; 
} 
else if(discriminant==0) 
{ 
cout<<"nThe equation has equal root"<<endl; 
} 
else if(discriminant<0) 
{ 
cout<<"nThe equation has complex root" <<endl; 
} 
root_1= ((-b + (sqrt(d)))/(2*a)); 
root_2= ((-b - (sqrt(d)))/(2*a)); 
cout<< "The first root is "<<root_1 <<"n"<<"while the second root is " << root_2 <<endl; 
return 0; 
}
//Output: 
Please enter value of a: 1 
Please enter value of b: 6 
Please enter value of c: 8 
The equation has 2 distinct root 
The first root is -2 
while the second root is -4 
QUESTION 4 
Write a program to find the mean (average) of N numbers. 
//Using while loop 
#include<iostream> 
using namespace std; 
int main() 
{ 
int N, i, b, sum=0; 
float average; 
cout<<"Please enter the value of N: "; 
cin>> N; 
i =1; 
while(i<=N) 
{ 
cout << "Value " << i << " is:"; 
cin >> b; 
sum= sum + b; 
i++; 
}
cout<<"The sum is: "<< sum <<endl; 
average= float (sum)/ N; 
cout<<"Thus, the average is: "<<average<< endl; 
return 0; 
} 
//Output for while loop: 
Please enter the value of N: 5 
Value 1 is:10 
Value 2 is:7 
Value 3 is:3 
Value 4 is:2 
Value 5 is:114 
The sum is: 136 
Thus, the average is: 27.2 
//Using do while loop 
#include<iostream> 
using namespace std; 
int main() 
{ 
int i=1, b,sum=0,N; 
float average; 
cout<<"Please enter a number N: "; 
cin>> N;
do 
{ 
cout << "Value " << i << " is:"; 
cin >> b; 
sum=sum+b; 
i++; 
} while(i<=N); 
cout<<"nThe summation of N number is: " << sum <<endl; 
average= float (sum)/N; 
cout<<"The average is " << average <<endl; 
return 0; 
} 
//Output for do while loop 
Please enter the value of N: 5 
Value 1 is:10 
Value 2 is:7 
Value 3 is:3 
Value 4 is:2 
Value 5 is:114 
The sum is: 136 
Thus, the average is: 27.2 
//Using for loop 
#include<iostream> 
using namespace std; 
int main()
{ 
int i, b, sum=0, N; 
float average; 
cout<<"Please enter a number N: "; 
cin>> N; 
for (i=1; i<=N ; i++) 
{ 
cout << "Value " << i << " is: "; 
cin >> b; 
sum= sum +b; 
average= float(sum)/N; 
} 
cout<<"nThe summation of N number is: "<< sum <<endl; 
cout<<"The average is " << average <<endl; 
return 0; 
} 
//Output for for loop: 
Please enter the value of N: 5 
Value 1 is:10 
Value 2 is:7 
Value 3 is:3 
Value 4 is:2 
Value 5 is:114 
The sum is: 136 
Thus, the average is: 27.2
QUESTION 5 
Write a program to find the sum of EVEN numbers (2+4+6+…+N) and ODD numbers (1+3+ ... +N) up to N numbers. 
//Using for loop 
#include<iostream> 
using namespace std; 
int main() 
{ 
int i, N, sum_odd=0, sum_even=0; 
cout<<"Please enter a number N: "; 
cin>>N; 
for(i=1; i<=N; i++) 
{ 
if(i%2==0) 
sum_even=sum_even +i; 
} 
cout<<"nThe summation of even number of N is: " <<sum_even <<endl; 
for(i=1; i<=N; i++) 
{ 
if(i%2!=0) 
sum_odd=sum_odd +i; 
} 
cout<<"The summation of odd number of N is: " <<sum_odd <<endl; 
return 0; 
}
//Output for for loop 
Please enter a number N: 10 
The summation of even number of N is: 30 
The summation of odd number of N is: 25 
//Using do while loop 
#include <iostream> 
using namespace std; 
int main() 
{ 
int i=1, N, sum_even=0, sum_odd=0; 
cout << "Enter any positive integer, N: "; 
cin >> N; 
while (i<= N) 
{ 
if (i%2 ==0) 
sum_even = sum_even + i; 
else 
sum_odd = sum_odd + I; 
i++; 
} 
cout << "nThe summation of even numbers is " << sum_even << endl; 
cout << "The summation of odd numbers is " << sum_odd << endl; 
return 0; 
}
//Output for while loop 
Enter any positive integer, N: 10 
The summation of even numbers is 30 
The summation of odd numbers is 25 
//Using do while loop: 
#include <iostream> 
using namespace std; 
int main() 
{ 
int i=1, N, sum_even=0, sum_odd=0; 
cout << "Enter any positive integer, N: "; 
cin >> N; 
do 
{ 
if (i%2 ==0) 
sum_even = sum_even + i; 
else 
sum_odd = sum_odd + i; 
i++; 
} while (i<= N); 
cout << "nThe summation of even numbers is " << sum_even << endl; 
cout << "The summation of odd numbers is " << sum_odd << endl; 
return 0; 
}
//Output for do while loop 
Enter any positive integer, N: 10 
The summation of even numbers is 30 
The summation of odd numbers is 25

More Related Content

PDF
C++ TUTORIAL 1
PDF
C++ TUTORIAL 8
PDF
C++ TUTORIAL 5
PDF
C++ TUTORIAL 9
PDF
C++ TUTORIAL 10
PDF
C++ TUTORIAL 4
PDF
C++ TUTORIAL 7
PDF
C++ TUTORIAL 6
C++ TUTORIAL 1
C++ TUTORIAL 8
C++ TUTORIAL 5
C++ TUTORIAL 9
C++ TUTORIAL 10
C++ TUTORIAL 4
C++ TUTORIAL 7
C++ TUTORIAL 6

What's hot (20)

PDF
C++ TUTORIAL 2
PPTX
New presentation oop
PPTX
C sharp 8
DOCX
Basic Programs of C++
PPT
PDF
Container adapters
PDF
Static and const members
PDF
C++ programs
PDF
Polymorphism
PDF
Stl algorithm-Basic types
DOCX
Opp compile
PDF
C++ Programming - 4th Study
PPTX
Ee 3122 numerical methods and statistics sessional credit
DOCX
Programa en C++ ( escriba 3 números y diga cual es el mayor))
PDF
Implementing stack
PDF
C++ Programming - 1st Study
DOCX
Travel management
PDF
Go a crash course
PDF
C++ Programming - 14th Study
PDF
C++ Programming - 2nd Study
C++ TUTORIAL 2
New presentation oop
C sharp 8
Basic Programs of C++
Container adapters
Static and const members
C++ programs
Polymorphism
Stl algorithm-Basic types
Opp compile
C++ Programming - 4th Study
Ee 3122 numerical methods and statistics sessional credit
Programa en C++ ( escriba 3 números y diga cual es el mayor))
Implementing stack
C++ Programming - 1st Study
Travel management
Go a crash course
C++ Programming - 14th Study
C++ Programming - 2nd Study
Ad

Similar to C++ TUTORIAL 3 (20)

PPT
PDF
C++ Nested loops, matrix and fuctions.pdf
PDF
25422733 c-programming-and-data-structures-lab-manual
PPT
DOCX
C programs
PDF
Please use the code below and make it operate as one program- Notating.pdf
PDF
C++ practical
PPT
ch5_additional.ppt
PPTX
oops practical file.pptx IT IS A PRACTICAL FILE
DOCX
Oops lab manual
PPT
901131 examples
PPTX
MUST CS101 Lab11
PPTX
Computational Physics Cpp Portiiion.pptx
PDF
54602399 c-examples-51-to-108-programe-ee01083101
PDF
You will be implementing the following functions. You may modify the.pdf
PDF
C++ in 10 Hours.pdf.pdf
DOCX
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
PPTX
Assignement of c++
PPT
Counting and looping
C++ Nested loops, matrix and fuctions.pdf
25422733 c-programming-and-data-structures-lab-manual
C programs
Please use the code below and make it operate as one program- Notating.pdf
C++ practical
ch5_additional.ppt
oops practical file.pptx IT IS A PRACTICAL FILE
Oops lab manual
901131 examples
MUST CS101 Lab11
Computational Physics Cpp Portiiion.pptx
54602399 c-examples-51-to-108-programe-ee01083101
You will be implementing the following functions. You may modify the.pdf
C++ in 10 Hours.pdf.pdf
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
Assignement of c++
Counting and looping
Ad

Recently uploaded (20)

PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Lesson notes of climatology university.
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Basic Mud Logging Guide for educational purpose
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Classroom Observation Tools for Teachers
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Cell Structure & Organelles in detailed.
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Pre independence Education in Inndia.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Institutional Correction lecture only . . .
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
GDM (1) (1).pptx small presentation for students
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Lesson notes of climatology university.
Final Presentation General Medicine 03-08-2024.pptx
Basic Mud Logging Guide for educational purpose
Supply Chain Operations Speaking Notes -ICLT Program
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Classroom Observation Tools for Teachers
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
VCE English Exam - Section C Student Revision Booklet
Cell Structure & Organelles in detailed.
Module 4: Burden of Disease Tutorial Slides S2 2025
Pre independence Education in Inndia.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Institutional Correction lecture only . . .
Microbial disease of the cardiovascular and lymphatic systems
Renaissance Architecture: A Journey from Faith to Humanism
FourierSeries-QuestionsWithAnswers(Part-A).pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
GDM (1) (1).pptx small presentation for students

C++ TUTORIAL 3

  • 1. SJEM2231 STRUCTURED PROGRAMMING NAME: WAN MOHAMAD FARHAN BIN AB RAHMAN TUTORIAL 3/ LAB 3 (13rd OCTOBER 2014) (Use different loop structures for problems 2, 4 and 5, and understand how it works) QUESTION 1 Write a program to find the given number is odd or even. First test whether the given number is non zero positive number. #include <iostream> using namespace std; int main() { int number; ULANG: cout << "Please enter any positive integer: "; cin >> number; if(number > 0) { cout << "The given number is non zero positive number"<<endl; } else { cout << "The given number is not a positive number. Try again" <<endl; goto ULANG; } if(number % 2 ==0) cout <<"Thus, " << number << " is an even number" << endl;
  • 2. else cout <<"Thus, " << number << " is an odd number" <<endl; return 0; } // Output : Please enter any positive integer: -1 The given number is not a positive number. Try again Please enter any positive integer: 0 The given number is not a positive number. Try again Please enter any positive integer: 5 The given number is non zero positive number Thus, 5 is an odd number //Alternative method for question 1 #include<iostream> using namespace std; int main() { int n; cout << "Enter a number: "; cin >> n; if (n>0 && n%2==0) { cout<<"nThe number is non zero positive number"<<endl; cout << "The number is even number"<<endl; } else if (n>0 && n%2!=0)
  • 3. { cout<<"nThe number is non zero positive number"<<endl; cout << "The number is odd number"<<endl; } else cout<<"nThe number is zero or negative number"<<endl; return 0; } //Output: Enter a number: 0 The number is zero or negative number Enter a number: 8 The number is non zero positive number The number is even number Enter a number: 11 The number is non zero positive number The number is odd number QUESTION 2 Write a program to print the multiplication table as shown below(lower triangular matrix). 1 2 4 3 6 9 4 8 12 16 5 10 15 20 25 … 10 20 30 40 50 60 70 80 90 100
  • 4. //Using for loop : #include <iostream> using namespace std; int main() { for (int i=1 ; i<=10; i++) { for(int j=1; j<=10; j++) { if (j>i) break; cout << i*j << "t"; } cout << endl; } return 0; } // Or we can write this way, still using for loop but slightly different: #include <iostream> using namespace std; int main() { for (int i=1 ; i<=10; i++) { for(int j=1; j<=i; j++) { cout << i*j << "t"; }
  • 5. cout << endl; } return 0; } //Using while loop #include<iostream> using namespace std; int main() { int i=1; while(i<=10) { int j=1; while(j<=i) { cout<<i*j <<"t"; j++; } cout <<endl; i++; } return 0; } //Using do while loop #include<iostream> using namespace std;
  • 6. int main() { int i=1, j=1; do { do { /*We may replace int j=1 here instead of declaring j=1 above */ cout << i*j <<"t"; j++; } while(j<=i); j=1; /*we can eliminate this j=1 if we declare int j=1 above */ cout<<endl; i++; } while(i<=10) return 0; } //The output we will get is just the same: 1 2 4 3 6 9 4 8 12 16 5 10 15 20 25 6 12 18 24 30 36 7 14 21 28 35 42 49 8 16 24 32 40 48 56 64 9 18 27 36 45 54 63 72 81 10 20 30 40 50 60 70 80 90 100
  • 7. QUESTION 3 Write a program that solves quadratic equations of the form ax2 + bx + c = 0, where a, b, and c are given real number and x is the unknown (This will NOT apply if a is zero, so that condition must be checked separately. The formula also fails to work (for real numbers) if the expression under the square root is negative) #include <iostream> #include <cmath> using namespace std; int main() { // Variable Declarations double a, b, c; //Variable Inputs cout << "Enter the value of a: "; cin >> a; cout << "Enter the value of b: "; cin >> b; cout << "Enter the value of c: "; cin >> c; //Computations double discriminant = (pow(b,2) - 4*a*c); double positive_root = (((-b) + sqrt(discriminant))/(2*a)); double negative_root = (((-b) - sqrt(discriminant))/(2*a)); if (discriminant == 0) { cout << "nThe discriminant is "<< discriminant << endl; cout << "The equation has a single root.n"; }
  • 8. else if (discriminant < 0) { cout << "nThe discriminant is "<< discriminant << endl; cout << "The equation has two complex roots.n"; } else { cout << "nnThe discriminant is " << discriminant << endl; cout << "The equation has two real roots.n"; } //Final Root Values cout << "The roots of the quadratic equation are x = "; cout << negative_root<< "," << positive_root << endl; return 0; } //Output: Enter the value of a: 1 Enter the value of b: 6 Enter the value of c: 8 The discriminant is 4 The equation has two real roots. The roots of the quadratic equation are x = -4,-2 //Alternative method for question 3 #include<iostream> #include<cmath> using namespace std;
  • 9. int main() { double a, b, c, discriminant, root_1, root_2; cout<<"Please enter value of a: "; cin>>a; cout<<"nPlease enter value of b: "; cin>>b; cout<<"nPlease enter value of c: "; cin>>c; discriminant= (b*b)-(4*a*c); if (discriminant>0) { cout<<"nThe equation has 2 distinct root" <<endl; } else if(discriminant==0) { cout<<"nThe equation has equal root"<<endl; } else if(discriminant<0) { cout<<"nThe equation has complex root" <<endl; } root_1= ((-b + (sqrt(d)))/(2*a)); root_2= ((-b - (sqrt(d)))/(2*a)); cout<< "The first root is "<<root_1 <<"n"<<"while the second root is " << root_2 <<endl; return 0; }
  • 10. //Output: Please enter value of a: 1 Please enter value of b: 6 Please enter value of c: 8 The equation has 2 distinct root The first root is -2 while the second root is -4 QUESTION 4 Write a program to find the mean (average) of N numbers. //Using while loop #include<iostream> using namespace std; int main() { int N, i, b, sum=0; float average; cout<<"Please enter the value of N: "; cin>> N; i =1; while(i<=N) { cout << "Value " << i << " is:"; cin >> b; sum= sum + b; i++; }
  • 11. cout<<"The sum is: "<< sum <<endl; average= float (sum)/ N; cout<<"Thus, the average is: "<<average<< endl; return 0; } //Output for while loop: Please enter the value of N: 5 Value 1 is:10 Value 2 is:7 Value 3 is:3 Value 4 is:2 Value 5 is:114 The sum is: 136 Thus, the average is: 27.2 //Using do while loop #include<iostream> using namespace std; int main() { int i=1, b,sum=0,N; float average; cout<<"Please enter a number N: "; cin>> N;
  • 12. do { cout << "Value " << i << " is:"; cin >> b; sum=sum+b; i++; } while(i<=N); cout<<"nThe summation of N number is: " << sum <<endl; average= float (sum)/N; cout<<"The average is " << average <<endl; return 0; } //Output for do while loop Please enter the value of N: 5 Value 1 is:10 Value 2 is:7 Value 3 is:3 Value 4 is:2 Value 5 is:114 The sum is: 136 Thus, the average is: 27.2 //Using for loop #include<iostream> using namespace std; int main()
  • 13. { int i, b, sum=0, N; float average; cout<<"Please enter a number N: "; cin>> N; for (i=1; i<=N ; i++) { cout << "Value " << i << " is: "; cin >> b; sum= sum +b; average= float(sum)/N; } cout<<"nThe summation of N number is: "<< sum <<endl; cout<<"The average is " << average <<endl; return 0; } //Output for for loop: Please enter the value of N: 5 Value 1 is:10 Value 2 is:7 Value 3 is:3 Value 4 is:2 Value 5 is:114 The sum is: 136 Thus, the average is: 27.2
  • 14. QUESTION 5 Write a program to find the sum of EVEN numbers (2+4+6+…+N) and ODD numbers (1+3+ ... +N) up to N numbers. //Using for loop #include<iostream> using namespace std; int main() { int i, N, sum_odd=0, sum_even=0; cout<<"Please enter a number N: "; cin>>N; for(i=1; i<=N; i++) { if(i%2==0) sum_even=sum_even +i; } cout<<"nThe summation of even number of N is: " <<sum_even <<endl; for(i=1; i<=N; i++) { if(i%2!=0) sum_odd=sum_odd +i; } cout<<"The summation of odd number of N is: " <<sum_odd <<endl; return 0; }
  • 15. //Output for for loop Please enter a number N: 10 The summation of even number of N is: 30 The summation of odd number of N is: 25 //Using do while loop #include <iostream> using namespace std; int main() { int i=1, N, sum_even=0, sum_odd=0; cout << "Enter any positive integer, N: "; cin >> N; while (i<= N) { if (i%2 ==0) sum_even = sum_even + i; else sum_odd = sum_odd + I; i++; } cout << "nThe summation of even numbers is " << sum_even << endl; cout << "The summation of odd numbers is " << sum_odd << endl; return 0; }
  • 16. //Output for while loop Enter any positive integer, N: 10 The summation of even numbers is 30 The summation of odd numbers is 25 //Using do while loop: #include <iostream> using namespace std; int main() { int i=1, N, sum_even=0, sum_odd=0; cout << "Enter any positive integer, N: "; cin >> N; do { if (i%2 ==0) sum_even = sum_even + i; else sum_odd = sum_odd + i; i++; } while (i<= N); cout << "nThe summation of even numbers is " << sum_even << endl; cout << "The summation of odd numbers is " << sum_odd << endl; return 0; }
  • 17. //Output for do while loop Enter any positive integer, N: 10 The summation of even numbers is 30 The summation of odd numbers is 25