“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
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