Posts

Showing posts from March, 2023

81: Program to find the maximum between three numbers. Using the switch statement.

  // Program to find the maximum between three numbers. Using the switch statement. // **************************************************************************************************************************** #include < iostream > using namespace std ; int main () {     float num1 , num2 , num3 ;     cout << " Enter first number: " ;     cin >> num1 ;     cout << " Enter second number: " ;     cin >> num2 ;     cout << " Enter third number: " ;     cin >> num3 ;     switch ( num1 > num2 )     {     case 1 :         switch ( num1 > num3 )         {         case 1 :             cout << num1 << " is the greatest " ;             break ;         case 0 :   ...

80: Program to find the maximum between two numbers. Using the switch statement.

  // Program to find the maximum between two numbers. Using the switch statement. // **************************************************************************************************************************** #include < iostream > using namespace std ; int main () {     float num1 , num2 ;     cout << " Enter first number: " ;     cin >> num1 ;     cout << " Enter second number: " ;     cin >> num2 ;     switch ( num1 > num2 )     {     case 1 :         cout << num1 << " is greater than " << num2 ;         break ;     case 0 :         cout << num2 << " is greater than " << num1 ;         break ;     default :         cout << " Invalid Input " ;         break ; ...

79: You are writing a program to convert temperatures between Celsius and Fahrenheit. You need to declare variables to store the input temperature and the converted temperature, as well as an if-else statement to check which conversion to perform based on user input.

  // You are writing a program to convert temperatures between Celsius and Fahrenheit. You need to declare // variables to store the input temperature and the converted temperature, as well as an if-else statement // to check which conversion to perform based on user input. #include < iostream > using namespace std ; int main () {     double temperature , celcius , fahrenheit ;     char c_or_f ;     cout << " If you want to convert Celcius to Fahrenheit input (Y or y) \n If you want to convert Fahrenheit to  Celcius input (N or n) \n " ;     cin >> c_or_f ;     if ( c_or_f == ' Y ' || c_or_f == ' y ' )     {         cout << " Enter the temperature in degree Celcius: " ;         cin >> temperature ;         fahrenheit = ( temperature * 9 / 5 ) + 32 ;         cout << " Temperat...

78: You are developing a movie ticket booking system, and you need to declare variables to store the number of tickets and the ticket price, as well as an if-else statement to calculate the total cost based on the number of tickets and the discount offered. 20% discount on five tickets

  // You are developing a movie ticket booking system, and you need to declare variables to store the number // of tickets  and the ticket price, as well as an if-else statement to calculate the total cost based on the // number of tickets and the discount offered. 20% discount on five tickets #include < iostream > using namespace std ; int main () {     int number , price , total , discount ;     cout << " Enter the number of tickets you bought: " ;     cin >> number ;     cout << " Enter the price of 1 ticket: Rs. " ;     cin >> price ;     if ( number < 5 )     {         total = number * price ;         cout << " Total cost: Rs. " << total << endl ;     }     else if ( number >= 5 )     {         discount = ( number * price * 0.2 )...

77: You are developing a banking application, and you need to declare a variable called "balance" of type float to store the current account balance. You also need to create an if-else statement to check if the balance is sufficient before allowing the user to make a withdrawal.

// You are developing a banking application, and you need to declare a variable called "balance" // of type float to store the current account balance. You also need to create an if-else statement to check // if the balance is sufficient before allowing the user to make a withdrawal. #include < iostream > using namespace std ; int main () {     float balance , withdrawal , remaining ;     cout << " Enter your current account balance: Rs. " ;     cin >> balance ;     cout << " Enter amount you want to withdraw: Rs. " ;     cin >> withdrawal ;     cout << " \n ************************************************************* \n " ;     if ( withdrawal > balance )     {         cout << " \n You do not have sufficient balance to make this withdrawal \n " ;     }     else if ( withdrawal <= balance )   ...

76: ***(Syntax)*** Some important elements in Loop

  1. Break statement:     This is used to break out of a loop if some condition is true. #include < iostream > using namespace std ; int main () {     for ( int i = 1 ; i <= 20 ; i ++)     {         if ( i == 13 )         {             break ;         }         cout << i << ' ' ;     }         return 0 ; } In this program, when the value of i is 13 , we will break out of the loop. 2: Continue Statement: #include < iostream > using namespace std ; int main () {     for ( int i = 1 ; i <= 20 ; i ++)     {         if ( i == 13 )         {             continue ;         }         cout << i << ' ' ;     } ...

75: Write a program to print the reverse of a given number using a while loop

  //  21. Write a program to print the reverse of a given number using a while loop //     ************************************************************************************************** #include < iostream > using namespace std ; int main () {     int num , numf = 0 , a = 0 ;     cout << " Enter a number: " ;     cin >> num ;     int b = num ;     while ( num > 0 )     {         a = num % 10 ;               // getting last digit of num         numf = numf * 10 + a ;       // * by 10 adds a zero to which a is added         num = num / 10 ;             // removing last digit of num             }     cout << " The reverse of " << b << " is: " << nu...

74: Write a program in C++ to display the n terms of odd natural number and their sum.

  // Write a program in C++ to display the n terms of odd natural number and their sum. Go to the editor //     Sample Output: //     Input number of terms: 5 //     The odd numbers are: 1 3 5 7 9 //     The Sum of odd Natural Numbers upto 5 terms: 25 //     ************************************************************************************************** #include < iostream > using namespace std ; int main () {     int num , a = 0 , sum = 0 ;     cout << " \n Enter the number of terms: " ;     cin >> num ;     cout << " \n *************************************************** \n\n " ;     cout << " The odd numbers are: " ;     for ( int i = 1 ; i > 0 ; i += 2 )     {         cout << i << ' ' ;         a ++;         sum = sum + i ; ...

73: Write a program in C++ to display the cube of the number upto given an integer.

  // Write a program in C++ to display the cube of the number upto given an integer. Go to the editor //     Sample Output: //     Input the number of terms : 5 //     Number is : 1 and the cube of 1 is: 1 //     Number is : 2 and the cube of 2 is: 8 //     Number is : 3 and the cube of 3 is: 27 //     Number is : 4 and the cube of 4 is: 64 //     Number is : 5 and the cube of 5 is: 125 //     ************************************************************************************************** #include < iostream > using namespace std ; int main () {     int num , a ;     cout << " \n Input the number of terms: " ;     cin >> num ;     cout << " \n *************************************** \n\n " ;     for ( int i = 1 ; i <= num ; i ++)     {         a = i * i * i ;       ...

72: Write a program in C++ to print a square pattern with # character.

  // Write a program in C++ to print a square pattern with # character. Go to the editor //     Sample Output: //     Print a pattern like square with # character: //     -------------------------------------------------- //     Input the number of characters for a side: 4 //     # # # # //     # # # # //     # # # # //     # # # # //     ************************************************************************************************** #include < iostream > using namespace std ; int main () {     int num ;     cout << " \n Print a pattern like square with # character: \n " ;     cout << " \n ---------------------------------------------------- \n\n " ;     cout << " Input the number of characters for a side: " ;     cin >> num ;     cout << endl ;     for ( int i = 1 ; i <...

71: Write a program in C++ to list composite numbers from 1 to an upperbound.

  // Write a program in C++ to list composite numbers from 1 to an upperbound. Go to the editor //     Sample Output: //     Input the upperlimit: 25 //     The composite numbers are: //     4 6 8 9 10 12 14 15 16 18 20 21 22 24 25 #include < iostream > using namespace std ; int main () {     int limit ;     cout << " \n Input an upperlimit: " ;     cin >> limit ;     cout << " \n ************************************************************** \n\n " ;     int a ;     cout << " The composite numbers upto " << limit << " are: \n " ;     for ( int i = 1 ; i <= limit ; i ++)     {         a = 0 ;         for ( int j = 1 ; j <= i ; j ++)         {             if ( i % j == 0 )       ...

70: Write a program in C++ to calculate the series (1) + (1+2) + (1+2+3) + (1+2+3+4) + ... + (1+2+3+4+...+n).

  // Write a program in C++ to calculate the series (1) + (1+2) + (1+2+3) + // (1+2+3+4) + ... + (1+2+3+4+...+n). Go to the editor //     Sample Output: //     Input the value for nth term: 5 //     1 = 1 //     1+2 = 3 //     1+2+3 = 6 //     1+2+3+4 = 10 //     1+2+3+4+5 = 15 //     The sum of the above series is: 35 //     ***************************************************************** #include < iostream > using namespace std ; int main () {     int num , a , sum = 0 ;     cout << " \n Enter a number: " ;     cin >> num ;     cout << " \n ******************************************** \n\n " ;     for ( int i = 1 ; i <= num ; i ++)     {         a = 0 ;         for ( int j = 1 ; j <= i ; j ++)         {   ...

69: Write a program in C++ to calculate the sum of the series (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + ... + (n*n).

  // Write a program in C++ to calculate the sum of the series (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + ... + (n*n). Go to the editor //     Sample Output: //     Input the value for nth term: 5 //     1*1 = 1 //     2*2 = 4 //     3*3 = 9 //     4*4 = 16 //     5*5 = 25 //     The sum of the above series is: 55 //     ************************************************************************************************** #include < iostream > using namespace std ; int main () {     int num ;     cout << " \n Input the value of nth terms: " ;     cin >> num ;  cout << " \n *************************************************** \n\n " ;         int sum = 0 , product ;         for ( int i = 1 ; i <= num ; i ++)         {             produc...

68: Write a program in C++ to find the sum of the series 1 + 1/2^2 + 1/3^3 + ..+ 1/n^n

// Write a program in C++ to find the sum of the series 1 + 1/2^2 + 1/3^3 + ..+ 1/n^n. Go to the editor //     Sample Output: //     Input the value for nth term: 5 //     1/1^1 = 1 //     1/2^2 = 0.25 //     1/3^3 = 0.037037 //     1/4^4 = 0.00390625 //     1/5^5 = 0.00032 //     The sum of the above series is: 1.29126 //     ************************************************************************************************** #include < iostream > #include < cmath > using namespace std ; int main () {     int num ;     cout << " \n Input the value for nth term: " ;     cin >> num ; cout << " \n ************************************************************ \n\n " ;                 float sum = 0 , result ;         for ( int i = 1 ; i <= num ; i ++)     ...