Posts

Showing posts from January, 2023

25: Write a program that takes a character as input and outputs whether it is a digit, a lowercase letter, an uppercase letter, or a special character using switch statement ================> (if you have to check whether it is a digit or not use range 'o' ...'9' <=================

  // Write a program that takes a character as input and outputs whether it is a digit, // a lowercase letter, an uppercase letter, or a special character using switch // statement #include <iostream> using namespace std ; int main () {     char input ;     cout << "Enter a character: " ;     cin >> input ;     switch ( input ){         case 'A' ... 'Z' :         cout << "This is an uppercase letter \n " ;         break ;         case 'a' ... 'z' :         cout << "This is a lowercase letter \n " ;         break ;         case '0' ... '9' :         cout << "This is a digit \n " ;         break ;         default :         cout << "This is a special character \n " ...

24: Write a program that takes three integer inputs from the user, and then checks if any two of them are equal. If so, the program should print out "Two numbers are equal" otherwise, it should print "All numbers are different".

  // Write a program that takes three integer inputs from the user, // and then checks if any two of them are equal. // If so, the program should print out "Two numbers are equal" otherwise, // it should print "All numbers are different". #include <iostream> using namespace std ; int main () {     int numa , numb , numc ;     cout << "Enter the first integer: " ;     cin >> numa ;     cout << "Enter the second integer: " ;     cin >> numb ;     cout << "Enter the third integer: " ;     cin >> numc ;     if ( numa == numb && numa == numc )     {         cout << "All three integers are equal \n " ;     }     else if ( numa == numb || numb == numc || numc == numa )     {         cout << "Two numbers are equal \n " ;     } ...

23: Write a program that takes the temperature in Celsius and converts it to Fahrenheit or vice versa, depending on user input.

  // Write a program that takes the temperature in Celsius and // converts it to Fahrenheit or vice versa, depending on user input. #include <iostream> using namespace std ; int main () {     char choose ;     float temp_c , temp_f ;     cout << "Choose \n 'F' or 'f' for fahrenhiet \n 'C' or 'c' for celcius \n " ;     cin >> choose ;     if ( choose == 'C' || choose == 'c' )     {         cout << "Enter the temperature in Celcius: " ;         cin >> temp_c ;     }     else if ( choose == 'F' || choose == 'f' )     {         cout << "Enter the temperature in fahrenhiet: " ;         cin >> temp_f ;     }     else     {         cout << "Invalid Input \n " ;         return...

22: Rock, Paper, Scissors game

  // Make a rock paper scissors game #include <iostream> #include <ctime> using namespace std ; int main (){         int computer_move ;         char user_move ;         cout << "Choose 'r' for rock, 'p' for paper or 's' for scissors: " ;             cin >> user_move ;         computer_move = time ( 0 )% 3 ;             if ( computer_move == 1 ){                 computer_move = 'r' ;             }             else if ( computer_move == 2 ){                 computer_move = 's' ;             }             else computer_move = 'p' ;                      ...

21: Write a program to check if a year is leap year or not. If a year is divisible by 4 then it is leap year but if the year is century year like 2000, 1900, 2100 then it must be divisible by 400.

// Write a program to check if a year is leap year or not. // If a year is divisible by 4 then it is leap year but if the year is // century year like 2000, 1900, 2100 then it must be divisible by 400. #include < iostream > using namespace std ; int main () {     int year ;     cout << " Enter an year: " ;     cin >> year ;     if (( year % 4 == 0 && year % 100 != 0 ) || year % 400 == 0 )     {         cout << " This is a leap year \n " ;     }     else         cout << " This is not a leap year \n " ;     return 0 ; } Input 1: Enter an year: 300 Output 1: This is not a leap year Input 2: Enter an year: 400 Output 2: This is a leap year   Input 3: Enter an year: 2001 Output 3: This is not a leap year Input 4: Enter an year: 2400 Output 4: This is a leap year

20: Ask user to enter age, sex ( M or F ), marital status ( Y or N ) and then using following rules print their place of service. If employee is female, then she will work only in urban areas. If employee is a male and age is in between 20 to 40 then he may work in anywhere. If employee is male and age is in between 40 to 60 then he will work in urban areas only. And any other input of age should print "ERROR".

  // Ask user to enter age, sex ( M or F ), marital status ( Y or N ) and then using following rules print their place of service. // if employee is female, then she will work only in urban areas. // if employee is a male and age is in between 20 to 40 then he may work in anywhere // if employee is male and age is in between 40 t0 60 then he will work in urban areas only. // And any other input of age should print "ERROR". #include <iostream> using namespace std ; int main (){         int age ;         char sex , marriage ;         cout << "Enter your age: " ;         cin >> age ;             if ( age >= 20 && age <= 60 ){                 cout << "Enter your sex 'm' or 'f': " ;                 cin >> sex ;         ...

19: Write a program to check whether a entered character is lowercase ( a to z ) or uppercase ( A to Z ). ========> to write full english write (letter >= 'A' && letter <= 'Z') <=========

      // Write a program to check whether a entered character is lowercase ( a to z ) or uppercase ( A to Z ). #include <iostream> using namespace std ; int main (){         char letter ;         cout << "Enter the alphabet: " ;         cin >> letter ;         if ( letter >= 'A' && letter <= 'Z' ){             cout << "The alphabet is uppercase \n " ;         }         else if ( letter >= 'a' && letter <= 'z' ){             cout << "The alphabet is lowercase \n " ;         }         else cout << "This is not an alphabet \n " ;     return 0 ; } Input 1: Enter the alphabet: F Output 1: The alphabet is uppercase Input 2: Enter the alphabet: d Output 2: The alph...

18: Modify the above question to allow student to sit if he/she has medical cause. Ask user if he/she has medical cause or not ( 'Y' or 'N' ) and print accordingly.

    // PREVIOUS QUESTION: // A student will not be allowed to sit in exam if his/her attendence is less than 75%. // Take following input from user // Number of classes held // Number of classes attended. // And print // percentage of class attended // Is student is allowed to sit in exam or not. // NEW QUESTION : // Modify the above question to allow student to sit if he/she has medical cause. // Ask user if he/she has medical cause or not ( 'Y' or 'N' ) and print accordingly. #include <iostream> using namespace std ; int main (){         int classes_held , classes_attended , percentage ;         char medical_cause ;             cout << "Enter the number of classes held: " ;             cin >> classes_held ;             cout << "Enter the number of classes attended: " ;             cin ...

17: A student will not be allowed to sit in exam if his/her attendence is less than 75%. Take following input from user Number of classes held Number of classes attended. And print percentage of class attended Is student is allowed to sit in exam or not.

  // A student will not be allowed to sit in exam if his/her attendence is less than 75%. // Take following input from user // Number of classes held // Number of classes attended. // And print // percentage of class attended // Is student is allowed to sit in exam or not. #include <iostream> using namespace std ; int main (){         int classes_held , classes_attended , percentage ;             cout << "Enter the number of classes held: " ;             cin >> classes_held ;             cout << "Enter the number of classes attended: " ;             cin >> classes_attended ;             percentage = ( classes_attended * 100 ) / classes_held ;             cout << "Your attendence is " << percentage << "%" << endl;   ...

16(a): Write a program to print absolute value of a number entered by user.

  // Write a program to print absolute value of a number entered by user. E.g.- // If-> INPUT: 1        Then-> OUTPUT: 1 // If-> INPUT:-1        Then-> OUTPUT: 1 #include <iostream> using namespace std ; int main (){         float number , absolute ;             cout << "Enter the number: " ;             cin >> number ;             if ( number < 0 ){                 absolute = - 1 * number ;                 cout << "The absolute value of " << number << " is: " << absolute <<endl;             }             else cout << "The absolute value of " << number << " is " << number <<endl;   ...

16(b): Write a program to print absolute value of a number entered by user using cmath

  // Write a program to print absolute value of a number entered by user. E.g.- // If-> INPUT: 1        Then-> OUTPUT: 1 // If-> INPUT:-1        Then-> OUTPUT: 1 #include < iostream > #include < cmath > using namespace std ; int main () {     float number , absolute ;     cout << " Enter the number: " ;     cin >> number ;     absolute = abs ( number );     cout << " The absolute of " << number << " is: " << absolute ;     return 0 ; }

15: Take input of age and name of 3 people by user and determine oldest and youngest among them with his age. -_-_-_-_-_-_-_-_-(line with spaces input concept)-_-_-_-_-_-_-_-_

  // Take input of age and name of 3 people by user and determine oldest and youngest among them with his age. #include < iostream > using namespace std ; int main () {     string person_1 , person_2 , person_3 ;     int age_1 , age_2 , age_3 ;     cout << " Enter the name of first person: " ;     getline ( cin >> ws , person_1 );     cout << " Enter his age: " ;     cin >> age_1 ;         cout << " \n Enter the the name of second person: " ;     getline ( cin >> ws , person_2 );     cout << " Enter his age: " ;     cin >> age_2 ;         cout << " \n Enter the name of third person: " ;     getline ( cin >> ws , person_3 );     cout << " Enter his age: " ;     cin >> age_3 ;     if ( age_1 > age_2 )   ...

14: The School has following rules for grading system: a. Below 25 - F, b. 25 to 45 - E, c. 46 to 50 - D, d. 51 to 60 - C, e. 61 to 80 - B, f. Above 80 - A. Ask user to enter marks and print the corresponding grade.

      // The School has following rules for grading system: // a. Below 25 - F // b. 25 to 45 - E // c. 46 to 50 - D // d. 51 to 60 - C // e. 61 to 80 - B // f. Above 80 - A // Ask user to enter marks and print the corresponding grade. #include <iostream> using namespace std ; int main (){         float total_marks , obtained_marks , percentage ;         cout << "Enter obtained marks: " ;         cin >> obtained_marks ;         cout << "Enter total marks: " ;         cin >> total_marks ;                 percentage = ( obtained_marks / total_marks ) * 100 ;             if ( percentage < 25 ){                 cout << "Your grade is: 'F'" << endl << " \n Better luck next time \n " ;        ...