You are on page 1of 40

Mid-Term assignment of

Programming fundamentals lab

Submitted to Sir Abdul Rauf


Name : Mubashra Qadir
Roll # Mite-f20-007

18 February 2021

Computer Science & Information Technology Department


Problem No 1:
I. What is Programming?
Programming is the process of making a set of instructions that performs task
on computer. Programming can be done using a variety of computer
programming languages, such as Python, and C++.
II. Main data types in C++?
Data types available in C++ are:
Integer (4 byte)
Character (1 byte)
Boolean (1 byte)
Floating Point (4 byte)
Double Floating Point (8 byte)
III. What is header files?
Many programming languages and other computer files have a directive, often
called include, that causes the contents of a second file to be inserted into the
original file. These included files are called header files. Each header file
contains information (or declarations) for a particular group of functions.
#include <iostream>
using namespace std;

IV. Mathematical Operators?


There are five mathematical operators in c++
• Addition +
• Subtraction -
• Multiplication *
• Modulus Operator %
• Division /
V. Why do you need a compiler?
Compilers convert high-level language code to machine (object) code in one
time. Compilers can take a while, because they have to translate high-level code
to lower-level machine language all at once and then save the executable object
code to memory.

VI. What is a source program?


A source program is a text file that contains instructions written in a high level
language. A source program is also called a source file or source code.

VII. Briefly explain escape sequences


Escape sequences are special characters used in control string to modify the
format of an output. These specific characters are translated into another
character or a sequence of characters that may be difficult to represent directly

VIII. Explain selection type of control structures in detail.


Selection structures are used to perform 'decision making' and then branch the
program flow based on the outcome of decision making. Selection structures
are implemented in C/C++ with If, If Else and Switch statements. The selection
control structure allows one set of statements to be executed if a condition is
true and another set of actions to be executed if a condition is false. In any case,
program control will resume with the statement following End If.

IX. Briefly explain relational operators available in C++


A relational operator is used to check the relationship between two operands.
For example, // checks if a is greater than b a > b; .It checks if a is greater than
b or not. If the relation is true, it returns 1 whereas if the relation is false, it
returns 0.
X. What do you mean by comments in C++? Also give examples
Program comments are explanatory statements that you can include in the C++
code. These comments help anyone reading the source code. All programming
languages allow for some form of comments. C++ supports single-line and
multi-line comments. All characters available inside any comment are ignored
by C++ compiler. C++ comments start with /* and end with */. For example −

/* This is a comment */

/* C++ comments can also


* span multiple lines
*/
A comment can also start with //, extending to the end of the line.

XI. What are the differences between machine language and high-level
language?
Machine language, or machine code, consists of binary code and is the only
language that is directly understood by the computer. Both machine code and
assembly languages are hardware specific. A high-level language is a
programming language that uses English and mathematical symbols in its
instructions.

XII. Discuss the working of post and pre-increment operator


Post increment implies the value i is incremented after it has been assigned to k
. However, pre increment implies the value j is incremented before it is assigned
to l . The same applies for decrement.

XIII. What do you mean by operator precedence?


Operator precedence determines the grouping of terms in an expression and
decides how an expression is evaluated. Certain operators have higher
precedence than others; for example, the multiplication operator has a higher
precedence than the addition operator.

XIV. Write a program that produces the following output:


**********************************
**Programming Assignment 1**
**Computer Programming**
**********************************
#include <iostream>
using namespace std; int main()
{
cout << "***********************************" << endl;
cout << " ** Programming Assignment 1 **" << endl;
cout << " ** Computer Programming I **" << endl;
cout << "***********************************" << endl;
return 0;
}
Problem No 2: What are the rules for variable name?
• Variable names in C++ can range from 1 to 255 characters.
• All variable names must begin with a letter of the alphabet or an underscore
(_).
• After the first initial letter, variable names can also contain letters and
numbers.

Problem No 3: Write down the validity status of following variables with


reason.
Variables Validity Reason
_basic valid _can be used
basic-hra invalid -not allowed
#MEAN invalid Special character not allowed

BASICSALARY valid No blank space


group. invalid Full stop not allowed

422 invalid First digit is not valid

over time invalid Blank space


queue. invalid Full stop not allowed

population in 2006 invalid Blank space


mindovermatter valid No blank space
FLOAT invalid Reserved word
hELLO valid 1st character is alphabet

team’svictory invalid Special character not allowed

Plot # 3 invalid Special character and blank space not


allowed

2015_DDay invalid 1st character is digit

Int invalid Reserved word


main Invalid Reserved word
LHR_Temp valid Underscore allowed

char invalid Reserved word


Float invalid Reserved word
Problem No 4: Point out the errors (if any) in the following C++
Statements.
I. The statement cin >> num; is equivalent to the statement num >> cin;
Ans: No, these two are not equal to each other. Because cin is a operator in
c++ used to take value by user.
II. The statement cin >> x >> y; requires the input values for x and y to
appear on the same line.
Ans: yes
III. k = ( a * b ) ( c + ( 2.5a + b ) ( d + e ) ;
Ans: this statement is missing one bracket and operator .correct statement is
k = ( a * b ) *( c + ( 2.5*a + b ) *( d + e ))
IV. si = principal * rateofinterest * numberofyears / 100 ;
Ans: This statement is corret
V. If a = 4; and b = 3; then after the statement a = b; the value of b is still 3.
Ans : This statement is also corect
VI. In the statement cin >> y; y can only be an int or a double variable.
Ans : This statement is incorrect because y can be of any data type
VII. area = 3.14 * r ** 2;
Ans: This statment is incorrect because the correct formula of area is 3.14*r2.
VIII. Suppose x = 5. After the statement y = x++; executes, y is 5 and x is 6.
Ans: correct
IX. k = ((a * b) + c) (2.5 * a + b);
Ans : ((a * b) + c) *(2.5 * a + b);
X. a = b = 3 = 4
Ans: this is wrong way of declaring variable and correct is a=3; b=4;

Problem No 5: Evaluate the following expressions and show their


hierarchy.
(a) g = big / 2 + big * 4 / big - big + abc / 3 ; (abc = 2.5, big = 2, assume g to be a
float)
G=? =3.8333
1. big/2 =2/2=12
2. big*4=2*4=83
3. big*4/big=44
4. abc/3=0.83335
5. big/2+big*4/big=56
6. big/2+big*4/big-big=37
7. big/2+big*4/big-big+abc/3=3.8333

(b)on = ink * act / 2 + 3 / 2 * act + 2 + tig ;


(ink = 4, act = 1, tig = 3.2, assume on to be an int)

1) int*act=42
2) int*act/2=23
3) 3/2=1.54
4) 3/2*act=1.55
5) ink*act/2+3/2*act=3.56
6) int*act/2+3/2*act+2=5.57
7) int*act/2+3/2*act+2+tig=8.7

(c) z = a * b / 2 - 3 / 2 * b + 2 - c ; (a = 8, b = 5, c = 4.1, assume z to be an int)

1. a*b=402
2. a*b/2=203
3. 3/2=1.54
4. 3 / 2 * b=7.55
5. a * b / 2 - 3 / 2 * b=12.56
6. a* b / 2 - 3 / 2 * b+2=14.57
7. a* b / 2 - 3 / 2 * b+2-c=10.4

(d) s = qui * add / 4 - 6 / 2 + 2 / 3 * 6 / god;


(qui = 4, add = 2, god = 2, assume s to be an int)
1) qui*add=82
2) qui*add/4=23
3) 6/2=34
4) 2/3=0.6665
5) 2/3*6=3.9966
6) 2/3*6/god=1.9987
7) qui*add/4-6/2=18
8) qui*add / 4 - 6 / 2 + 2 / 3 * 6 / god =2.998

(e) s = 1 / 3 * a / 4 - 6 / 2 + 2 / 3 * 6 / g; (a = 4, g =
3, assume s to be an int)
1. 1/3=0.3332
2. 1/3*a=1.3323
3. 1/3*a/4=0,3334
4. 6/2=35
5. 2/3=0.6666
6. 2/3*6=0.1117
7. 2/3*6/g=0.0378
8. 1 / 3 * a / 4 - 6 / 2=-2.6679
9. 1 / 3 * a / 4 - 6 / 2+2/3*6/g=-2.63
(f) x = 1 / 3 * a / 4 * 6 / 2 + 2 / 3 + 6 / g; (a = 7, g =
6, assume x to be an int)
1. 1/3=0.3332
2. 1/3*a=2.3313
3. 1/3*a/4=0.582754
4. 1/3*a/4*6=3.49655
5. 1/3*a/4*6/2=1.748256
6. 2/3=0.6667
7. 6/G=18
8. 1/3*a/4*6/2+2/3=2.414259
9. 1/3*a/4*6/2+2/3+6/G=2.41425

Problem No 6: Convert the following equations into corresponding C++


statements

A. corresponding C statement:
a*a*a+b*b*b+c*c*c+3*a*b*c

B. corresponding C statement:
A=a(x-1)x*x/2*a*b

C. corresponding C statement:
Z = ( 8.8 * ( a + b ) * 2 / c - 0.5 +2 * a / ( q + r ) ) / ( ( a + b ) * ( 1 / m ) )

D.corresponding C statement:
R =( 2 * v + 6.22 * ( c + d ) ) / ( ( g + v ) )

E.corresponding C statement:
A = ( 7.7 * b * ( x * y + a ) / c - 0.8 ) / ( ( x + a ) * ( 1 / y ) )
Problem No 07: What would be the output of the following programs.
Q.1
Suppose x, y, z, and w are int variables. What value is assigned to
each of these variables after the last statement executes?
x = 5; z = 3; y = x - z;
z = 2 * y + 3;
w = x - 2* y + z;
z = w - x; w++;
SOLUTION:
#include<iostream>
using namespace std;
int main ()
{
int x,y,z,w;
cout<<"value of x ="<<x<<endl;
y=x-z;
cout<<"value of y="<<y<<endl;
z = 2 *y + 3;
cout<<"value of z="<<z<<endl;
w = x - 2 * y + z;
cout<<"value of w="<<w<<endl;
z = w - x;
cout<<"final value of z="<<z<<endl;
cout<<w++;
}
Q:2 Suppose x, y, and z are int variables and w and t are double variables. What
value is assigned to each of these variables after the last statement executes? x = 17;
y = 15; x = x + y / 4; z = x % 3 + 4; w = 17 / 3 + 6.5; t = x / 4.0 + 15 % 4 - 3.5;

#include <iostream>
using namespace std;
int main()
{
int x,y,z; double w,t;
x=17; y=15;
x = x + y / 4;
cout<<"value of x="<<x<<endl;
z = x % 3 + 4;
cout<<"value of z="<<z<<endl;
w = 17 / 3 + 6.5;
cout<<"value of w="<<w<<endl;
t = x / 4.0 + 15 % 4 - 3.5;
cout<<"value of t="<<t<<endl;
}
Q:3 #include<iostream> #include<conio.h> using namespace std; int main() { int i
= 2, j = 3, k, l ; float a, b ; k = i / j * j ; l = j / i * i ; a = i / j * j ; b = j / i * i ;
cout<<k<<"\t"<<l<<"\t"<<a<<"\t"<<b; getche(); }
Q:4Suppose x, y, and z are int variables and x = 2, y = 5, and z = 6. What is the
output of each of the following statements? cout << "x = " << x << ", y = " << y
<< ", z = " << z << endl; cout << "x + y = " << x + y << endl; cout << "Sum of "
<< x << " and " << z << " is " << x + z << endl; cout << "z / x = " << z / x <<
endl; cout << "2 times " << x << " = " << 2 *x << endl;

Q:5#include<iostream> #include<conio.h> using namespace std; int main() {


int i = 2, j = 3, k, l ; float a, b ; k = i + j / j ; l = j + i / i ; a = i + j / j ; b = j - i / i ;
cout<<k<<"\t"<<l<<"\t"<<a<<"\t"<<b; getche(); }
Q:6Q.7 #include<iostream> #include<conio.h> using namespace std; int main()
{ float a = 5, b = 2 ; float c ; c = a + b/2 ; cout<<a<<"\t"<<b<<"\n"<<c; getche();
}

Problem No 08: Write the complete code in C++ for the given problems.
Q:1
#include <iostream>
using namespace std;
int main ()
{
cout<<”HELLO WORLD_”;
}

Q:2
#include<iostream>
Using namespace std;
Int main( )
{
cout<<"\\"<<"\""<<"\\""\""<<"I"<<"\\""\""<<"\\""\""<<endl;

cout<<"\t"<<"\\"<<"\""<<"\\""\""<<"Love"<<"\\""\""<<"\\""\""<<endl;
cout<<"\t""\t"<<"\\"<<"\""<<"\\""\""<<"Pakistan"<<"\\""\"";

Q:3
#include<iostream> using
namespace std; int main()

cout<<" I "<<endl;
cout<<" Love "<<endl;
cout<<"pakistan_"<<endl;
}
Q:4
#include<iostream> using

namespace std; int main()


{

cout<<"I"<<endl;
cout<<" Love "<<endl;

cout<<"
pakistan"<<endl;

Problem No 09: Consider the following program segment


//include statement(s) //using
namespace statement int
main()
{
//variable declaration
//executable statements
//return statement
}

➢ Write C++ statements that include the header files iostream.


➢ Write a C++ statement that allows you to use cin, cout, and endl without
the prefix std::
➢ Write C++ statements that declare the following variables: num1, num2,
num3, and average of type int.
➢ Write C++ statements that store 125 into num1, 28 into num2, and -25
into num3
➢ Write a C++ statement that stores the average of num1, num2, and num3,
into average.
➢ Write C++ statements that output the values of num1, num2, num3, and
average.
Solution: #include<iostream>
using namespace std;
int main() {
int num1, num2, num3, average;
num1=125; num2=28; num3=-25
average=(num1+num2+num3)/3;
cout<<num1<<endl<<num2<<endl<<num3<<end
l<<average<<endl;
}
Problem No 10: What does the following code print?
cout << "*\n**\n***\n****\n*****" << endl;
#include<iostream> using
namespace std; int main()
{

cout << "*\n**\n***\n****\n*****" << endl;


}

Problem No 11: Write C++ statements to accomplish each of the


following tasks.
(a) Declare variables sum and x to be of type int.
(b) Set variable x to 1.
(c) Set variable sum to 0.
(d) Add variable x to variable sum and assign the result to variable sum.
(e) Print "The sum is: "followed by the value of variable sum

Solution;
#include<iostream>
using namespace std;
int main()
{
int x=1,sum=0;
int result=x+sum;
cout<<" the sum is="<<result;
}

Problem No 12: State the values of each of these int variables after the
calculation is performed. Assume that, when each statement begins
executing, all variables have the integer value 5.
(a) product *= x++;
(b) quotient /= ++x;

Solution;
#include<iostream>
using namespace std;
int main() {
int product=5,quotient=5,x=5;
cout<<"before formula the product is="<<product<<endl;
cout<<"before formula the quotient ="<<quotient<<endl;
cout<<"before increment the value of x is="<<x<<endl;
product*=x++;
quotient/=++x;
cout<<"after formula the product is="<<product<<endl;
cout<<"after formula the quotient is="<<quotient<<endl;
cout<<"after increment the value of x is="<<x; return 0;
}

Problem No 13: What, if anything, prints when each of the following C++
statements is performed? If nothing prints, then answer “nothing.”
Assume x= 2 and y=3

cout<<x; 2

cout<<x+x; 4

cout<<"x="; X=

cout<<"x ="<< x; X=2

cout<<x+y<<"="<< y+x; 5=5

cin >> x>>y; nothing but if user type x=2


then the output is 2etc
// cout << "x +y="<<x+y; Nothing, this is comment

) cout<<"\n"; error

Problem No 14: Write a program that asks the user to enter two
numbers, obtains the two numbers from the user and prints the
sum, product, difference, and quotient of the two numbers.
#include<iostream>
using namespace std;
int main() {
int num1, num2;
cout<< "Enter two integers: "; //
cin>> num1 >> num2;
cout<< "The sum is " << num1 + num2;
cout << "\nThe product is " << num1 * num2;
cout<< "\nThe difference is " << num1 - num2;
cout<< "\nThe quotient is " << num1 / num2 << endl;
}

Problem No 15: Write a program that inputs three integers from the
keyboard and prints the sum, average, product, smallest and largest of
these numbers.

#include <iostream>
using namespace std;
int main() {
int num1, num2, num3, ;
cout << "Input three different integers: ";
cin>> num1 >> num2 >> num3;
cout << "Sum is " << num1 + num2 + num3
<< "\nAverage is " << (num1 + num2 + num3) / 3
<< "\nProduct is " << num1 * num2 * num3 << endl;
if((num1 >= num2) && (num1 >=
num3))
cout << "Largest number: " <<
n1;
else if ((num2 >= num1) && (num2
>= num3))
cout << "Largest number: " <<
num2;
else
cout << "Largest number: " <<
num3;
return 0;
}

Problem No 16: Write a program that prompts the user to enter the
weight of a person in kilograms and outputs the equivalent weight in
pounds. Output both the weights rounded to two decimal places. (Note
that 1 kilogram = 2.2 pounds.) Format your output with two decimal
places.

# include <iostream>
using namespace std;
int main() {
double weight;
double poundWeight;
cout<<"Enter the weight in kilogram"<<endl;
cin>>weight;
poundWeight=(weight*2.2);
cout<<"The weight in kilograms is:"<< weight<<endl;
cout<<"The weight in pounds is:"<<poundWeight<<endl;
}

Problem No 17: Write a program that accepts as input the mass, in


grams, and density, in grams per cubic centimeters, and outputs the
volume of the object using the formula: density = mass / volume. Format
your output to two decimal places.
#include<iostream> using
namespace std; int main() {
float g;
float d;
float volume;
cout << "Please input the mass in grams: ";
cin >> g;
cout << "Please input the density in grams per cubic
centimeter:";
cin >> d;
volume = g / d;
cout << "Volume:" << volume << endl;
}

Problem No 18: Enter three angles of a triangle and find if the triangle is
valid or not. (Hint: if the sum of all angles is equal to 180, the triangle will
be considered as valid triangle)

#include<iostream>
using namespace std;
int main() {
int angle1,angle2,angle3;
cout<<"Enter the three angles of triangle:";
cin>>angle1>>angle2>>angle3;
if (angle1+angle2+angle3==180){
cout<<"Triangle is valid";
}
else{
cout<<"Triangle is not valid"; return 0;
}
}

Problem No 19: Write a program that prints the numbers 1 to 4 on the


same line with each pair of adjacent numbers separated by one space.
Do this in several ways:
(a) Using one statement with one stream insertion operator.
(b) Using one statement with four stream insertion operators.
(c) Using four statements.

#include <iostream>
using namespace std; int main
() {
// Part A
cout << "1 2 3 4\n";
cout << "1 " << "2 " << "3 " << "4\n"; << "1 ";
cout << "2 ";
cout << "3 ";
cout << "4" << endl;
return 0;
}

Problem No 20: Write a program that reads an integer and determines


and prints whether it’s odd or even. [Hint: Use the modulus operator. An
even number is a multiple of two. Any multiple of two leaves a remainder
of zero when divided by 2.

#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if ( num % 2 == 0 )
cout << "The number " << num << " is even."
<<endl;
if ( num % 2 != 0 ) cout << "The number " << num
<< " is odd." << endl;
return 0;
}

Problem No 21: Write a program that inputs a five-digit integer,


separates the integer into its digits and prints them separated by three
spaces each. [Hint: Use the integer division and modulus operators.] For
example, if the user types in 42339, the program should print: 4 2 3 3 9.

#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a five-digit number: ";
cin >> num;
cout << num / 10000 << " ";
num = num % 10000;
cout << num / 1000 << " ";
num = num % 1000;
cout << num / 100 << " ";
num = num % 100;
cout << num / 10 << " ";
num = num % 10; cout <<
num << endl; return 0;
}

Problem No 22: Receive two integers from user at run time and swap
their values.

#include <iostream>
using namespace std; int
main() {
int a = 5, b = 10, temp;
cout << "Before swapping." << endl;
cout << "a = " << a << ", b = " << b <<
endl;
temp = a;
a = b;
b = temp;
cout << "\nAfter swapping." << endl;
cout << "a = " << a << ", b = " << b <<
endl;
return 0;
}

Problem No 23: Fakhar’s basic salary is input through the keyboard. His
dearness allowance is 40% of basic salary, and house rent allowance is
20% of basic salary. Write a program to calculate his gross salary.

# include<iostream>
using namespace std;
int main() {
int bs, gs, da, hr;
cout<<("Enter the basic salary:");
cin>>bs;
da=40*bs/100;
hr =20*bs/100;
gs= bs + da + hr;
cout<<"The gross salary is="<<gs;
return 0;
}

Problem No 24: The length & breadth of a rectangle and radius of a


circle are input through the keyboard. Write a program to calculate the
area & perimeter of the rectangle, and the area & circumference of the
circle.

#include<iostream>
using namespace std;
int main() {
int length , breadth , radius ;
cout<<"Enter Length Of Rectangle = ";
cin>>length;
cout<<"Enter Breadth Of Rectangle = ";
cin>>breadth;
cout<<"Enter radius Of Rectangle = ";
cin>>radius;
float area;
area = length*breadth;
cout<<"Area of rectangle ="<<area<<endl;
float perimeter;
perimeter = length + breadth;
perimeter = perimeter * 2;
cout<<"Perimeter of rectangle =
"<<perimeter<<endl;
float circleArea;
circleArea = 3.14 * radius * radius;
cout<<"Area of Circle = "<<circleArea<<endl;
float circumference ;
circumference = 2 * 3.14 * radius;
cout<<"circumference Of circle = "<<circumference;

Problem No 25: Write single C++ statements or portions of statements that


do the following:
(a) Input integer variable x with cin and >>.
(b) Input integer variable y with cin and >>.
(c) Set integer variable i to 2.
(d) Pre-increment variable i by 1.

#include <iostream>
using namespace std;
int main() {
int x, y;
cin>>x;
cin>>y;
int i=2;
cout<<++i;
}
Problem No 26: Write a program that asks the user to enter two integers,
obtains the numbers from the user, and then prints the larger number
followed by the words "is larger." If the numbers are equal, print the
message "These numbers are equal."

#include <iostream>
using namespace std;
int main() {
int num1, num2;
cout << "Enter two integers: ";
cin >> num1 >> num2;
if ( num1 == num2 ) cout << "These numbers are equal." << endl;
if ( num1 > num2 )
cout << num1 << " is larger." << endl;
if ( num2 > num1 ) cout << num2 << "
is larger." << endl;
return 0;
}

Problem No 27: Write a program that reads two integers and determines
and prints if the first is a multiple of the second. [Hint: Use the modulus
operator.]

#include <iostream>
using namespace std;
int main() {
int num1, num2;
cout << "Enter two integers: ";
cin >> num1 >> num2;
if ( num1 % num2 == 0 ) {
cout << num1 << " is a multiple of " << num2 <<
endl;
}
if ( num1 % num2 != 0 ) {
cout << num1 << " is not a multiple of " << num2
<< endl;
}
return 0;
}

Problem No 28: Write a program to print the following series in same


pattern. 2, 4, 8, 16, 32, 64 … 1024

#include<iostream>
using namespace std;
int main() {
int i;
for(i=1;i<=1024;i*=2){
cout<<i<<" ";
}
return 0;
}

Problem No 29: Write a program to display the factorial of the number


given by user.

#include<iostream>
using namespace std;
int main() {
int num, factorial=1;
cout<<" Enter Number To Find Its
Factorial:";
cin>>num;
for(int a=1;
a<=num;a++) {
factorial=factorial*a;
cout<<"Factorial of Given Number is
="<<factorial<<endl;
}
return 0;
}
Problem No 30: Write a program to display all even numbers till 150.

#include<iostream>
using namespace std;
int main(){
int i=1;
while(i<=50){
if(i%2==0)
{
cout<<i<<endl;
}
i++;
}
}
Problem No 31: Write a program to display all odd numbers till 100.

#include<iostream>
using namespace std;
int main(){
int i=1;
while(i<=100){
if(i%2!=0)
{
cout<<i<<endl;
}
i++;
}

Problem No 32: Write a program to display all the devisors of 7 till 150.

#include<iostream>
using namespace std;
int main() {
int x,y,z;
x=7;y=1; z=7; while(x<147){
x=z;
x=x*y;
cout<<x<<","; y=y+1;
}
}

Problem No 33: Write a program to display all the devisors of a number


given by user till the range which will also defined by user.

#include<iostream>
using namespace std;
int main()
{
int num, d;
cout<<"Enter the number to find the devisors: "<<endl;
cin>>num;
cout<<"Factors of number are "<<endl;
for(d = 1 ; d <= num; ++d)
{
if(num % d == 0)
cout<<d<<endl;
}
return 0;
}
Problem No 34: Find the youngest brother among Ali, Asad & Fakhir,
where user will input the age of them in years.
#include<iostream>
using namespace std;
int main(){
int a, as, f;
cout<<"Age of Ali: "<<endl;
cin >> a;
cout << "Age of Asad: "<<endl;
cin >> as;
cout<<"Age of Fakhir: "<<endl;
cin >> f;
if (a <= as && a <= f){
cout<<"Ali is the youngest brother. "<<endl;
}
else if(as <= a && as <= f){
cout<<"Asad is youngest. "<<endl;
}
else cout<<"Fakhir is youngest. "<<endl;
}

Problem No 35: Write a program to display the answer of following


series. 1+2+3+4+5+6 … +n. Where n will be given by user.

#include<iostream>

using namespace std;

int main(){

int s, sum, count;

cout<<"The series is

1+2+3+4+5+6.....+n.

"<<endl;

cout<<"Enter the

value of n: "<<endl;

cin>>s;

for(count=1;

count<=s; count++)
sum+=count;

cout<<"Sum

"<<sum;

Problem No 36: Write a C++ Program that reads marks obtained by a


student in a test of 100 marks and computes his grade according to the
following criteria.
Marks>=80 grade=A
Marks>=70 & <80 grade=B
Marks>=60 & <70 grade=C
Marks>=50 & <60 grade=D
otherwise grade=F

#include<iostream>
int main(){
int marks;
cout<<"Enter the
obtained marks out of
100: "<<endl;
cin>>marks;
if(marks>=80)
{
cout<<"Grade
A"<<endl;
}
else if(marks>=70
&& marks<80)
{
cout<<"Grade
B"<<endl;
}
else if(marks>=60
&& marks<70)
{
cout<<"Grade
C "<<endl;
}
else if(marks>=50
&& marks<60){
cout<<"Grade
D"<<endl;
}
else {

cout<<"Fail"<<endl;
}
return 0;
}
Problem No 37: Write a program to display 2X where user will input the
value in x.
#include<iostream>

using namespace std; int

main() {

int x, y=1;

cout<<"Enter the

number=";

cin>>x;

while(x>0) {

y=x*2; x--;

}
cout<<y;
}
Problem No 38: Write a program to display yX where user will input the
value in x & y.
#include<iostream>

using namespace std; int

main()

{
int c=1; int

x,y,z;

cout<<"enter the number";

cin>>x;

cout<<"entr the number ";

cin>>y;

while(y>0)

{
z=x*c; c=z;

y=y-1;

}
cout<<z;
}

Problem No 39: Create a simple calculator to add, subtract, multiply and


divide using if else.
#include<iostream>
#include<math.h>
using namespace std;
int main(){
int h, i, op;
cout<<"Enter the two numbers to perform operations of +, -, *, / "<<endl;
cin>> h >> op >> i;
if(op == '+')
{
cout<<h + i<<endl;
}
else if(op == '-')
{
cout<<h - i<<endl;
}
else if(op == '*')
{
cout<<h * i<<endl;
}
else if(op == '/')
{
cout<<h / i<<endl;
}
else
{
cout<<"Invalid "<<endl;
}
}
Problem No 40: Write a program to print Fibonacci series of n terms
where n is input by user.
#include<iostream>
using namespace std;
int main(){
int n,n1,n2,n3,count=1;
cout<<"Enter a number: ";
cin>>n;
n1=0;
n2=1;
while(count<=n){
cout<<n1<<"\n";
n3 = n1 + n2;
n1 = n2;
n2 = n3;
count++;
}

You might also like