Introduction to C++
Flow of Control
Evaluation
Part – II
Answers to all the questions (2 Marks):
1. What is a null statement and compound statement?
Answer:
(i) The "null or empty statment" is a statement
containing only a semicolon.
(ii) A group of statements enclosed by pair of braces {}. This
group of statements is called as a compound statement or a block.
2. What is selection statement? write it's types?
Answer: The selection statement means the statement(s) are executed
depends upon a condition. If a condition is true, a true block (a set of
statements) is executed otherwise a false block is executed. This statement is
also called decision statement or selection statement because it helps in
making decision about which set of statements are to be executed.
types are
(i) if statement
(ii) if-else statement
(iii) rested if
(iv) switch-case.
3. Correct the following code sigment:
if (x=1)
p= 100;
else
p = 10;
Answer:
if (x==l)
p=100;
else
p=10;
4. What will be the output of the following code:
int year;
cin >> year;
if (year % 100 == 0)
if ( year % 400 == 0)
cout << "Leap";
else
cout << "Not Leap year";
If the input given is (i) 2000 (ii) 2003 (iii) 2010?
Answer:
(i) leap year
(ii) Not Leap year
(iii) Not Leap year.
5. What is the output of the following code?
for (int i=2; i<=10 ; i+=2)
cout << i;
Answer: 2 4
6 8 10.
6. Write a for loop that displays the number from 21 to 30.
Answer:
for (i=21; i<=30; i++)
cout << i;
7. Write a while loop that displays numbers 2, 4, 6, 8.......20.
Answer:
int i=2;
while (i <= 20)
{
Cout << i;
if=2;
}
8. Compare an if and a ? : operator.
Answer:
if
Multiple statements are used
Eg:
if(p = = 10)
p= 100;
else
p =10;
?:
only one statement used. It is an alternative statement for
if_else.
Eg:
(p ==10)? p=100;
p=10;
Part – III
Answers to all the questions (3 Marks):
1. Convert the following if-else to a single conditional statement:
if (x >= 10)
a = m + 5;
else
a = m;
Answer:
a = (x > =10) ? m+5 :m;
2. Rewrite the following code so that it is functional:
v = 5;
do;
{
total += v;
cout << total;
while v <= 10
Answer:
int v = 5;
do
{
total +=v;
cout<<total;
v=v+l;
}while (v<=10);
3. Write a C++ program to print multiplication table of a given number.
Answer:
#include<iostream>
using namespace std;
int main()
{
int num;
cout<<"Enter Number To Find Multiplication table
";
cin>>num;
for(int a=l ;a<=10;a++)
{
Cout<<num<<" * "<<a<<" =
"<<num*a<<endl;
}
return 0;
}
4. Write the syntax and purpose of switch statement.
Answer:
The switch statement is a multi-way branch statement.
It provides an easy way to dispatch execution to different parts
of code based on the value of the expression. The switch statement replaces
multiple if-else sequence.
The syntax of the
switch statement is:
switch(expression)
{
case constant 1:
statement(s);
break;
case constant 2:
statement(s);
break;
:
:
default:
statement(s);
}
5. Write a short program to print following series:
(a) 1 4 7 10 .. .. .. 40
Answer:
#include<iostream.h>
using namespace std;
int main()
{
for (int i=l; i<=40; i + =3)
cout<< i << endl;
}
getch();
}
Part – IV
Answers to all the questions (5 Marks):
1. Explain control statement with suitable example.
Answer: Control statements are statements that alter the sequence of
flow of instructions. If the statements are executed sequentially, the flow is
called as sequential flow. In some situations, if the statements alter the flow
of execution like branching, iteration, jumping and function calls, this flow
is called as control flow.
Sequence statement:
Statement 1
↓
Statement 2
↓
Statement 3
The sequential statement are the statements, that are executed
one after another only once from top to bottom. These statement do not alter
the flow of execution. These statement are called as sequential flow
statements. They are always end with a semicolon (;).
If - else : In if-else statement, first the expression or condition is
evaluated either true of false. If the result is true, then the statements
inside true-block is executed and false-block is skipped. If the result is
false, then the statement inside the false-block is executed i.e., the
true-block is skipped.
if (rem==0)
cout<< "\n The given number" <<num<<"
is Even";
else
cout<<"\n The given number"<<num<<"
is Odd";
2. What entry control loop? Explain any one of the entry control loop with suitable example.
Answer: In loop, if the condition checked in the beginning of execution
of the loop is called entry control loop.
While loop : A while loop is a control flow statement that allows the loop
statements to be executed as long as the condition is true. The while loop is
an entry-controlled loop because the test-expression is evaluated before the
entering into a loop.
The while loop
syntax is :
while ( Text expression )
{
Body of the loop;
}
Statement-x;
The control flow and flow chart of the while loop is shown below
:
In while loop, the test expression is evaluated and if the test
expression result is true, then the body of the loop is executed and again the
control is transferred to the while loop. When the test expression result is
false the control is transferred to statement-x.
Example :
#include<iostream>
using namespace std;
int main ()
{
int i=l, sum=0;
while(i<=10)
{
sum=sum+l;
i++;
}
cout<<"The sum of 1 to 10 is"<<sum;
return 0;
}
Output: The sum of 1 to 10 is 55
3. Write a program to find the LCM and GDC of two numbers.
Answer:
program to find GDC and LCM
#include<iostream.h>
using namespace std;
int main()
{
int nl,n2, a, b, gcd, lcm;
cout<<"Enter two numbers"<<endl;
cin>>nl>>n2;
a=nl;
b=n2;
while (n1 != n2)
{
if(n1>n2)
nl = nl − n2;
else
n2 = n2 − n1;
}
gcd = nl;
cout<< "GCD=" <<gcd;
lcm = (a * b) / gcd;
cout <<"LCM =" << lcm;
}
4. Write programs to find the sum of the following series:
(a) x- x2/2! + x3/3! - x4/4! + x5/5! - x6/6!
(b) x+ x2/2 + x3/3 +....+ xn/n
Answer:
(a) //To find the sum of the series
#include.<iostream>
using namespace std;
int main ()
{
int i, x, sl=0, s2=0, j;
cout<<"Enter the value of x"<<endl;
cin>>x;
for (i=1; i<=5; i+=2)
{
int f = 1;
for(j=l;j<=i;j++)
f=f*j;
s1 = s1+(Math.pow(x, i y f);
}
for (i=2; i<=6; i+=2)
{
int f = 1;
for (j=l; j<=i;j++)
f=f*j;
s2 = s2+(Math.pow(x, i y f);
}
cout <<"sum of series ="<<(sl −s2);
return 0;
}
(b) # program to find the same of the series
#include<iostream.h>
using namespace std;
int main ()
{
int i, x, n, s=0;
cout<<"Enter the value of x"<<endl;
cin>>x;
cout<<"Enter number of terms"<<endl;
cin>>n;
for (i=l; i<=n; i++)
s=s+(pow(x,i)/i);
cout<<"The sum="<<s;
getch();
}
5. Write a program to find sum of the series
S = 1 + x + x2 +..... + xn
Answer:
//program to find the sum of series
#include<iostream.h>
using namespace std;
int main ()
{
int x, n, i, s = 0;
cout<<"Enter the values of x"<<endl;
cin>>x;
cout<<"Enter the number of terms"<<endl;
cin>>n;
for (i=0; i<=n; i++)
s = s+pow(x, i);
cout<<"The sum="+s;
}
Hands on practice:
Write C++ program to slove the following problems :
1. Tempeature - conversion program that gives the user the option of converting Fahrenheit to Celsius or Celsius to Fahrenheit and depending upon user's choice.
Answer:
#include<iostream>
using namespace std;
int main ()
{
int a;
cout<<"l. For Celsius to Fahrenheit.\n";
cout<<"2. For
Fahrenheit To Celsius. \n";
cout<<"3. For Exit\n\n";
cout<<"Enter Your Choice\n";
switch(a)
{
double cel, feh;
case 1: cout<<"Enter The Temperature in Celsiush\n";
cin>>cel;
feh = (cel*9/5)+32;
cout<<"\nTemperature in Fahrenheit is =
"<<feh;
break;
case 2: cout<<"Enter The Temperature in
Fahrenheit\n|; cin>>feh
cel = (feh−32)*5/9;
cout<<"\nTemperature in Celsius is = "<<cel;
break;
case 3: exit (0);
default:cout<<"\nEnter The Right Choice\n";
break;
}
return 0;
}
2. The program requires the user to enter two numbers and an operator. It then carries out the specified arithmetical operation: addition, subtraction, multiplication or division of the two numbers. Finally, it displays the result.
Answer:
#include<iostream>
using namespace std;
int main ( )
{
char op;
float numl, num2;
cout<<"Enter operator either + or − or * or/:";
cin>>op;
cout<<"Enter two operands:";
cin>>num 1 >>num2;
switch(op)
{
Case '+':
cout<<numl+num2;
break;
case '−':
count<<num 1 − num2;
break;
case '*':
cout<<numl *num2;
break;
case '/':
cout<<numl/num2;
break;
default:
//if the operator is other than +,−,* or/, error message is
shown cout<<"Error! operator is not correct"
break;
}
return 0;
}
Output:
Enter operator either + or − or * or divide;-
Enter two operands:
3.4
8.4
3.4−8.4 = 5.0
3. Program to input a character and to print whether a given character is an alphabet, digit or any other character.
Answer:
#include<iostream>
using namespace std;
int main ()
{
char ch;
cout<<"Enter any character:";
ch=getchar();
if (isalpha (ch))
cout<<" Alphabet";
else if (isdigit(ch))
cout<<"Number";
else
cout<<"Special Character";
return 0;
}
4. Program to print whether a given character is an uppercase or a lowercase character or a digit or any other character. use ASCII codes for it. The ASCII codes are as given below:
Characters : ASCII Range
'0' - '9' : 48 - 57
'A' - 'Z' : 65 - 90
'a' - 'z' : 97 - 122
other characters : 0- 255 excluding the above mentioned codes.
Answer:
#include<iostream>
using namespace std;
int main()
{
char ch;
cout<< "Enter a character"<<endl;
cin>>ch;
if ((ch>=65 & & ch <=90)
cout << "Alphabet: upper case";
else if ((ch>= 97 && ch <= 122))
cout <<"Alphabet: Lower case";
else if (ch>= 48&& ch<=57)
cout <<"Digt";
else
cout<<"Special Character";
return 0;
}
5. Program to calculate the factorial of an integer.
Answer:
#include<iostream>
using namespace std;
int main ()
{
int n, i, f = 1;
cout<<"Enter a number"<<endl;
cin>>n;
for (i =1; i<=n; i++)
f=f*i;
cout<<"Factorial of a given number ="
<<f<<endl;
return 0;
}
6. Program that print 1 2 4 8 16 32 64 128.
Answer:
#include<iostream>
#include<math.h>
using namespace std;
int main ()
{
for (i=0; i<8; i ++)
cout<<pow(2,i)<<"t";
return 0;
}
7. Program to generate divisors of an interger.
Answer:
#include<iostream>
using namespace std;
int main ()
{
int n, i;
cout<<"Enter a number"<<endl;
cin>>n;
for (i=i;i<=n; i++)
{
if(n%i== 0)
cout<<i<<"\t";
}
return 0;
}
8. Program to print fibonacci series i.e., 0 1 1 2 3 5 8......
Answer:
#include<iostream>
using namespace std;
int main ()
{
int n, i, a = 0, b = 1
cout<<"Enter number of terms"<<end1;
cin>>n;
cout<*Fibonacci series"<<endl;
cout<<a<<"\t"<<b;
for(i=3;i<=n; i ++)
{
c=a+b;
cout<<c<<"\t";
a =b;
b=c;
}
return 0 ;
}
9. Programs to produces the following design using nested loops
Answer:
(a) #include<isotream>
using namespace std;
int main ()
{
for(int i = 1; i < = 5; i++)
{
for(int j =5; j >= i;. j − −)
{
cout<<j"\t";
}
cout<<'n';
}
return 0 ;
{
(b) #include<iostream>
using namespace std;
int main ( )
{
int n = 65, rows;
cout<<"Enter number' of rows: "; cin>>rows;
for (int i = 65; i<=(65+rows−l); i++)
}
for(int j = 65; j<=i; j++)
{
cout<<(char)j>>"\t";
}
return 0;
}
(c) #include<iostream>
using namespace std;
int main ()
{
int space, rows;
cout<<"Enter number of rows:";
cin>> rows;
for (int i = rows, k =0 ; i > = 1; i − − )
{
for (space = rows −i; space >=1; − − space)
{
cout<<" " ;
}
While(k ! = 2*i − 1)
{
Cout << “*”;
}
Cout<<end1;
}
10. Program to check whether square root of a number is prime or not.
Answer:
#include<iostream>
#include<math.h>
using namespace std;
int main ()
{
tint n, i, m,c ;
cout<<"Enter a number"<<end1;
cin>>n;
m=sqrt(n);
for (i=l;l<m; i++)
{
if(m%i== 0)
c++;
}
if(c==2)
cout<<"The square root of a given number is
prime"<<end1;
else
cout<<“The square root of a given number is not
prime"<<endl;
}
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.