Home | | Computer Science 11th std | Example C++ Program: Flow of Control

Computer Science - Example C++ Program: Flow of Control | 11th Computer Science : Chapter 10 : Flow of Control

Chapter: 11th Computer Science : Chapter 10 : Flow of Control

Example C++ Program: Flow of Control

C++ Statements, C++ Control Statements, C++ Iteration statements, C++ Jump statements

Illustration 10.1 C++ program to calculate total expenses using if statement

#include<iostream>

using namespace std;

int main()

{

      int qty, dis=0;

      float rate, tot;

      cout<<"\nEnter the quantity ";

     cin>>qty;

      cout<<"\nEnter the rate ";

      cin>>rate;

      if ( qty> 500)

      dis=10;

      tot = (qty * rate) - ( qty * rate * dis / 100);

      cout<<"The total expenses is "<< tot;

      return 0;

} 

Output

First Run

Enter the quantity 550

Enter the rate 10

The total expenses is 4950

Second Run

Enter the quantity 450

Enter the rate 10

The total expenses is 4500


Illustration 10.2 C++ program to check whether a person is eligible to vote using if statement

#include <iostream>

using namespace std;

int main()

{

      int age;

      cout<< "\n Enter your age: ";

      cin>> age; if(age>=18)

      cout<< "\n You are eligible for voting ....";

      cout<< "This statement is always executed.";

      return 0;

}

Output

Enter your age: 23

You are eligible for voting….

This statement is always executed.


Illustration 10.3 C++ program to calculate bonus using if statement

#include<iostream>

using namespace std;

int main()

{

int bonus,yr_of_ser;

cout<<"\nEnter your year of service ";

cin>>yr_of_ser;

      if ( yr_of_ser> 3 )

      {

      bonus=2000;

      cout<<"\n Your bonus is " <<bonus;

      }

cout<<"\nCongratulations...";

return 0;

}

Output

Enter your year of service 5

Your bonus is 2000

Congratulations...


Illustration 10.4 C++ program to find whether the given number is even number or odd number using if-else statement

#include <iostream>

using namespace std;

int main()

{

      int num, rem;

      cout<< "\n Enter a number: ";

      cin>>num;

      rem = num % 2;

      if (rem==0)

           cout<< "\n The given number" <<num<< " is Even";

      else

           cout<< "\n The given number "<<num<< " is Odd";

      return 0;

}

Output

Enter number: 10

The given number 10 is Even


Illustration 10.5 – C++ program to calculate commission according to grade using nested if statement

#include <iostream>

using namespace std;

int main()

{

      int sales, commission;

      char grade;

      cout << "\n Enter Sales amount: ";

      cin >> sales;

      cout << "\n Enter Grade: ";

      cin >> grade;

      if (sales > 5000)

      {

           commission = sales * 0.10;

           cout << "\n Commission: " << commission;

      }

      else

      {

           commission = sales * 0.05;

           cout << "\n Commission: " << commission;

      }

      cout << "\n Good Job ..... ";

      return 0;

}

Output:

Enter Sales amount: 6000

Enter Grade: A

Commission: 600

Good Job .....


Illustration 10.6 C++ program to find your grade using if-else ladder.

#include <iostream>

using namespace std;

int main ()

{

int marks;

cout<<" Enter the Marks :";

cin>>marks;

if( marks >= 60 )

      cout<< "Your grade is 1st class !!" <<endl;

           else if( marks >= 50 && marks < 60)

                   cout<< "your grade is 2nd class !!" <<endl;

                             else if( marks >= 40 && marks < 50)

                                      cout<< "your grade is 3rd class !!" <<endl;

else

      cout<< "You are fail !!" <<endl;

return 0;

}

Output

Enter the Marks :60

Your grade is 1st class !!


Illustration 10.7 – C++ program to find greatest of two numbers using conditional operator

#include <iostream>

using namespace std;

int main()

{

      int a, b, largest;

      cout << "\n Enter any two numbers: ";

      cin >> a >> b;

      largest = (a>b)? a : b;

      cout << "\n Largest number : " << largest;

      return 0;

}

Output:

Enter any two numbers: 12 98

Largest number : 98


Illustration 10.8 – C++ program to demonstrate switch statement

#include <iostream>

using namespace std;

int main()

{

      int num;

      cout << "\n Enter week day number: ";

      cin >> num;

      switch (num)

      {

           case 1 : cout << "\n Sunday"; break;

           case 2 : cout << "\n Monday"; break;

           case 3 : cout << "\n Tuesday"; break;

           case 4 : cout << "\n Wednessday"; break;

           case 5 : cout << "\n Thursday"; break;

           case 6 : cout << "\n Friday"; break;

           case 7 : cout << "\n Saturday"; break;

           default: cout << "\n Wrong input....";

      }

}

Output:

Enter week day number: 6

Friday

 

Illustration 10.9 – C++ program to demonstrate switch statement

#include <iostream>

using namespace std;

int main()

{

      char grade;

      cout << "\n Enter Grade: ";

      cin >> grade;

switch(grade)

      {

      case 'A' : cout << "\n Excellent...";

      break;

      case 'B' :

      case 'C' : cout << "\n Welldone ...";

      break;

      case 'D' : cout << "\n You passed ...";

      break;

      case 'E' : cout << "\n Better try again ...";

      break;

      default : cout << "\n Invalid Grade ...";

}

cout << "\n Your grade is " << grade;

return 0;

}

Output:

Enter Grade: C

Welldone ...

Your grade is C


Illustration 10.10 C++ program to check for zero or non-zero and odd or even using  nested switch statement

#include <iostream>

using namespace std;

int main()

{

int a = 8;

cout<<"The Number is : " <<a <<endl;

switch (a)

      {

      case 0 :

      cout<<"The number is zero" <<endl;

      break;

      default:

      cout<<"The number is a non-zero integer" <<endl;

      int b = a % 2;

      switch (b)

      {

      case 0:

      cout<<"The number is even" <<endl;

      break;

      case 1:

      cout<<"The number is odd" <<endl;

      break;

      }

      }

return 0;

}

Output

The Number is : 8

The number is a non-zero integer

The number is even


Illustration 10.11 C++ program to display numbers from 0 to 9 using for loop

#include <iostream>

using namespace std;

int main ()

{

int i;

for(i = 0; i< 10; i ++ )

      cout<< "value of i : " <<i<<endl;

return 0;

}

Output

value of i : 0

value of i : 1

value of i : 2

value of i : 3

value of i : 4

value of i : 5

value of i : 6

value of i : 7

value of i : 8

value of i : 9


Illustration 10.12 C++ program to sum the numbers from 1 to 10 using for loop

#include <iostream>

using namespace std;

int main ()

{

int i,sum=0;

for(i=1; i<=10;i++)

{

      sum=sum+i;

}

cout<<"The sum of 1 to 10 is "<<sum;

return 0;

}

Output

The sum of 1 to 10 is 55


Illustration 10.13 (a) C++ program to sum the numbers from 1 to 10

#include <iostream>

using namespace std;

int main ()

{

int i, sum=0, n;

cout<<"\n Enter The value of n";

cin>>n;

i =1;

for ( ; i<=10;i++)

      {

      sum += i;

     }

cout<<"\n The sum of 1 to " <<n<<"is "<<sum;

return 0;

}

Output

Enter the value of n 5

The sum of 1 to 5 is 15


Illustration 10.13 (b) C++ program to sum the numbers from 1 to 10

#include <iostream>

using namespace std;

int main ()

{

int i, sum=0, n;

cout<<"\n Enter The value of n";

cin>>n;

i =1;

for ( ; i<=10; )

{

      sum += i;

      ++i;

}

cout<<"\n The sum of 1 to " <<n<<"is "<<sum;

return 0;

}

Output

Enter the value of n 5

The sum of 1 to 5 is 15


Illustration 10.14 C++ program to sum numbers from 1 to 10 using while loop

#include <iostream>

using namespace std;

int main ()

{

int i=1,sum=0;

while(i<=10)

{

      sum=sum+i;

      i++;

}

cout<<"The sum of 1 to 10 is "<<sum;

return 0;

}

Output

The sum of 1 to 10 is 55


Illustration 10.15 C++ program to sum numbers from 1 to 10 using while loop

#include <iostream>

using namespace std;

int main ()

{

int i=1,num,avg,sum=0;

while(i<=5)

{

      cout<<"Enter the number : ";

      cin>>num;

      sum=sum+num;

      i++;

}

avg=sum/5;

cout<<"The sum is "<<sum<<endl;

cout<<"The average is "<<avg;

return 0;

}

Output

Enter the number : 1

Enter the number : 2

Enter the number : 3

Enter the number : 4

Enter the number : 5

The sum is 15

The average is 3


Illustration 10.16 C++ program to display number from 10 to 1 using do-while loop

#include <iostream>

using namespace std;

int main ()

{

int n = 10;

do

{

      cout<<n<<", ";

      n--;

}while (n>0) ;

}

Output

10, 9, 8, 7, 6, 5, 4, 3, 2, 1


Illustration 10.17 C++ program to display matrix multiplication table using nested for loop

#include<iostream>

using namespace std;

int main(void)

{

      cout<< "A multiplication table:" <<endl <<" 1\t2\t3\t4\t5\t6\t7\t8\t9" <<endl<< "" <<endl;

      for(int c = 1; c < 10; c++)

      {

      cout<< c << "| ";

      for(int i = 1; i< 10; i++)

      {

      cout<<i * c << '\t';

      }

      cout<<endl;

      }

return 0;

}

Output

A multiplication table:    

1    2   3      4        5        6        7        8        9

1| 1     2      3        4        5        6        7        8        9

2| 2     4      6        8        10      12      14      16      18

3| 3     6      9        12      15      18      21      24      27

4| 4     8      12      16      20      24      28      32      36

5| 5     10    15      20      25      30      35      40      45

6| 6     12    18      24      30      36      42      48      54

7| 7     14    21      28      35      42      49      56      63

8| 8     16    24      32      40      48      56      64      72

9| 9     18    27      36      45      54      63      72      81


Illustration 10.18 C++ program to calculate average of given numbers using goto statement

#include <iostream>

using namespace std;

int main()

{

float num, average, sum = 0.0;

int i, n;

cout<< "Maximum number of inputs: ";

cin>> n;

for(i = 1; i<= n; ++i)

{

cout<< "Enter n" <<i<< ": ";

cin>>num;

if(num< 0.0)

{

// Control of the program move to jump: goto jump;

}

sum += num;

}

jump:

average = sum / (i - 1);

cout<< "\nAverage = " << average; return 0;

}

Output

Maximum number of inputs: 5

Enter n1: 10

Enter n2: 20

Enter n3: -2 Average = 15


Illustration 10.19 C++ program to count N numbers using break statement

#include <iostream>

Using namespace std;

int main ()

{

int count = 0;

do

{

cout<< "Count : " << count <<endl;

count++;

if( count > 5)

{

      break;

}

}while( count < 20 );

return 0;

}

Output

Count : 0

Count : 1

Count : 2

Count : 3

Count : 4

Count : 5


Illustraion 10.20 C++ program to display numbers from 1 to 10 except 6 using continue statement

#include <iostream>

using namespace std;

int main()

{

for (int i = 1; i<= 10; i++) {

if (i == 6)

continue;

else

cout<<i<< " ";

}

return 0;

}

Output

1 2 3 4 5 7 8 9 10


Tags : Computer Science , 11th Computer Science : Chapter 10 : Flow of Control
Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
11th Computer Science : Chapter 10 : Flow of Control : Example C++ Program: Flow of Control | Computer Science


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.