Posts

95: Take input of user first name and last name and join the name and output full name. (join two strings)

  #include < iostream > using namespace std ; string stjoin ( string string1 , string string2 ) {     return string1 + ' ' + string2 ; } int main () {     string fname , lname , full_name ;     cout << " Enter your first name: " ;     getline ( cin , fname );     cout << " Enter your last name: " ;     getline ( cin , lname );     full_name = stjoin ( fname , lname );     cout << full_name ;     return 0 ; }

94: ***(Syntax)*** Take length from user and print area and volume using function

  #include < iostream > using namespace std ; float square ( float length ) {     return length * length ; } float cube ( float length ) {     return length * length * length ; } int main () {     float length ;     cout << " Enter the length: " ;     cin >> length ;     float area = square ( length ), volume = cube ( length );     cout << " Area is equal to: " << area << " cm^2 " << endl ;     cout << " Volume is equal to: " << volume << " cm^3 " ;     return 0 ; } Precaution: You must assign value to length or take input from user before calling the function as in  float area = square ( length )  

93: ***(Syntax)*** Make a happy birthday song for user defined name using function

  #include < iostream > using namespace std ; void hbd ( string name , int age ) {     cout << " Happy Birthday to " << name << ' . ' << endl ;     cout << " Happy Birthday to " << name << ' . ' << endl ;     cout << " Happy Birthday dear " << name << ' . ' << endl ;     cout << " Happy Birthday to " << name << ' . ' << endl << endl ;     cout << " You are now " << age << " years old " << endl << endl ; } int main () {     string name ;     int age ;     cout << " Enter your name: " ;     getline ( cin , name );     cout << " Enter your age: " ;     cin >> age ;     cout << endl ;     hbd ( name , age );         return ...

92: Print a Happy birthday song using function

  #include < iostream > using namespace std ; void hbd () {     cout << " Happy Birthday to you " << endl           << " Happy Birthday to you " << endl           << " Happy Birthday dear you " << endl           << " Happy Birthday to you " ; } int main () {     hbd ();     cout << endl           << endl ;     hbd ();     cout << endl           << endl ;     hbd ();     return 0 ; }

91: Number Guessing Game

  // Number Guessing Game #include < iostream > #include < ctime > using namespace std ; int main () {     int num , guess , tries = 0 ;     srand ( time ( NULL ));     num = rand () % 100 + 1 ;     cout << " \n ************ Number Guessing Game ************ \n\n " ;     cout << " The number is between (1-100) \n " ;     do     {         cout << " Choose a number: " ;         cin >> guess ;         tries ++;         if ( guess < num )         {             cout << " \n Try a higher number \n\n " ;         }         else if ( guess > num )         {             cout << " \n Try a lower number \n\n " ;    ...

90: Random event generator

  // Random event generator #include < iostream > #include < ctime > using namespace std ; int main () {     int num ;     srand ( time ( NULL ));     num = rand () % 6 + 1 ;     switch ( num )     {         case 1 :         cout << " You won a lottery ticket " ;         break ;         case 2 :         cout << " You won a concert ticket " ;         break ;         case 3 :         cout << " You won a laptop " ;         break ;         case 4 :         cout << " You won a smart watch " ;         break ;         case 5 :         cout << " You won a mobile phone " ;       ...

89: Generate random number in C++

  // Generate random number in C++ #include < iostream > #include < ctime > using namespace std ; int main () {     int num1 ;     srand ( time ( NULL ));     num1 = rand ()% 6 + 1 ;     cout << num1 ;         return 0 ; } In  num1 = rand ()% 6 + 1 ;   rand ()  will generate a random number. % 6  is the range upto which you want to generate random numbers. + 1  will let you start number from 1 not from 0.

88: Using switch statement Write a C program to input marks of five subjects Physics, Chemistry, Biology, Mathematics and Computer. Calculate percentage and grade according to following: // Percentage >= 90% : Grade A Percentage >= 80% : Grade B Percentage >= 70% : Grade C Percentage >= 60% : Grade D Percentage >= 40% : Grade E Percentage < 40% : Grade F

  // Using switch statement Write a C program to input marks of five subjects Physics, Chemistry, Biology, Mathematics and Computer. //     Calculate percentage and grade according to following: //     Percentage >= 90% : Grade A //     Percentage >= 80% : Grade B //     Percentage >= 70% : Grade C //     Percentage >= 60% : Grade D //     Percentage >= 40% : Grade E //     Percentage < 40% : Grade F #include < iostream > using namespace std ; int main () {     float phy_g , phy_t , chem_g , chem_t , bio_g , bio_t , math_g , math_t , comp_g , comp_t , per ;     cout << " Enter your Physics marks: " ;     cin >> phy_g ;     cout << " Out of: " ;     cin >> phy_t ;     cout << " Enter your Chemistry marks: " ;     cin >> chem_g ;     cout << " Out of...

87: Program to check whether a year is a leap year or not. Using switch statement

  // Program to check whether a year is a leap year or not. Using switch statement #include < iostream > using namespace std ; int main () {     int year ;     cout << " Enter the year: " ;     cin >> year ;     switch ( year % 4 == 0 && year % 100 == 0 )     {     case 1 :         cout << year << " is a leap year " ;         break ;         case 0 :         cout << year << " is not a leap year " ;         break ;     default :         cout << " Invalid Input " ;         break ;     }     return 0 ; }

86: Program to take the value from the user as an input week number and print weekday by using the switch statement.

// Program to take the value from the user as an input week number and print weekday by using the switch statement. #include < iostream > using namespace std ; int main () {     int day ;         cout << " Enter No. of Days (1-7) " ;     cin >> day ;         switch ( day )     {     case 1 :         cout << " Its Monday " ;         break ;     case 2 :         cout << " Its tuesday " ;         break ;     case 3 :         cout << " Its wednesday " ;         break ;     case 4 :         cout << " Its thusday " ;         break ;     case 5 :         cout << " It's Friday " ;         break ;     ...

85: Program to take the value from the user as input any character and check whether it is the alphabet, digit or special character. Using the switch statement.

  // Program to take the value from the user as input any character and check whether it is the alphabet, // digit or special character. Using the switch statement. // ***************************************************************************************************************** #include < iostream > using namespace std ; int main () {     char alpha ;     cout << " Enter a character: " ;     cin >> alpha ;     switch ( alpha )     {     case ' A ' ... ' Z ' :         cout << alpha << " is an uppercase alphabet " ;         break ;     case ' a ' ... ' z ' :         cout << alpha << " is a lowercase alphabet " ;         break ;     case ' 0 ' ... ' 9 ' :         cout << alpha << " is a digit " ;         br...

84: Program to take the value from the user as input any alphabet and check whether it is vowel or consonant. Using the switch statement.

  // Program to take the value from the user as input any alphabet and check whether it is vowel or consonant. Using the switch statement. // ***************************************************************************************************************** #include < iostream > using namespace std ; int main () {     char alpha ;     cout << " Enter an alphabet: " ;     cin >> alpha ;     switch ( alpha )     {     case ' A ' :     case ' a ' :     case ' E ' :     case ' e ' :     case ' I ' :     case ' i ' :     case ' O ' :     case ' o ' :     case ' U ' :     case ' u ' :         cout << alpha << " is a vowel " ;         break ;     case ' B ' :     case ' b ' :     case ' C ' :     case ' c ' :...