Posts

44: Write a program to check if a character is a vowel or consonant.

// Write a program to check if a character is a vowel or consonant. #include < iostream > using namespace std ; int main () {     char alpha ;     cout << " Enter an alphabet: " ;     cin >> alpha ;     if ( alpha == ' A ' || alpha == ' a ' || alpha == ' E ' || alpha == ' e ' || alpha == ' I ' || alpha == ' i ' || alpha == ' O ' || alpha == ' o ' || alpha == ' U ' || alpha == ' u ' )     {         cout << alpha << " is an vowel \n"               << endl ;     }     else if ( alpha == ' B ' || alpha == ' b ' || alpha == ' C ' || alpha == ' c ' || alpha == ' D ' || alpha == ' d ' || alpha == ' F ' || alpha == ' f ' || alpha == ' G ' || alpha == ' g ' || alpha == ' H ' || alpha == ' h '...

44: Write a program that takes name as input and if it has characters more than 15 it outputs Name cannot have more than 15 characters otherwise output Welcome name

  // Write a program that takes name as input and if it has characters more than 15 // it outputs Name cannot have more than 15 characters // Otherwise output Welcome name #include < iostream > using namespace std ; int main () {     int charcters ;     string name ;     cout << " Enter your name: " ;     getline ( cin , name );     name . length () <= 15 ? cout << " Welcome " << name << endl :          cout << " Name cannot have more than 15 characters \n " ;     return 0 ; }

43: ***(Syntax)*** Some useful ---string--- methods

.length( ) .length( ) function tells you the length of the string. #include < iostream > using namespace std ; int main () {     int charcters ;     string word ;     cout << " Enter a word: " ;     getline ( cin , word );     cout << " There are " << word.length() << " characters in word: " << word ;     return 0 ; } .empty( ) .empty( )  function is a boolean attribute that is used when you have to check whether the user have given some input or not #include < iostream > using namespace std ; int main (){     string name ;     cout << " Enter your name: " ;     getline ( cin , name );     if ( name.empty () ){         cout << " You didn't write anything \n " ;     }     else cout << " Welcome " << name ;     return 0 ; } In this program,...

42: ***(Syntax)*** Calculating number of characters and spaces in a string 1- Make a program to calculate the number of characters in a string 2- Modify this and also tell how many spaces are there in that sentence

// 1- Make a program to calculate the number of characters in a string // 2- Modify this and also tell how many spaces are there in that sentence #include < iostream > #include < string > using namespace std ; int main () {     string sentence ;     int characters , space = 0 ;     cout << " Enter the sentence: " ;     getline ( cin >> ws , sentence );     for ( int i = 0 ; i < sentence . length (); i ++)     {         if ( sentence [ i ] == ' ' )         {             space ++ ;         }     }     characters = sentence . length () - space ;     cout << " There are " << characters << " characters and " << space << " spaces in sentence: \n "           << sentence ;     return 0...

41: Write a program to show the number of character you wrote in a sentence, number of characters in your name, number of characters in string

  // Write a program to show the number of character you wrote in a sentence, // number of characters in your name, number of characters in string #include < iostream > using namespace std ; int main () {     string name ;     int space = 0 , characters ;     cout << " Enter your name: " ;     getline ( cin , name );     for ( int i = 0 ; i < name . length (); i ++)     {         if ( name [ i ] == ' ' )         {             space ++;         }     }     characters = name . length () - space ;     cout << " Your name has " << characters << " characters \n " ;     return 0 ; }

40: ***(Syntax)*** Write a program to calculate the number of spaces in a sentence

.length( ) function:   sentence . length ()  This operator is used to calculate the number of characters in  sentence  . // Write a program to calculate the number of spaces in a sentence #include < iostream > #include < string > using namespace std ; int main () {     string sentence ;     int space = 0 ;     cout << " Enter the sentence: " ;     getline ( cin >> ws , sentence );     for ( int i = 0 ; i < sentence . length (); i ++)     {         if ( sentence [ i ] == ' ' )         {             space ++;         }     }     cout << " There are " << space << " spaces in sentence: \n "           << sentence ;     return 0 ; } Note: 1:  Whenever you have to calculate space or some ot...

39: Write a ternary program to show that a number is even or odd

  // Write a ternary program to show that a number is even or odd #include < iostream > using namespace std ; int main () {     int num ;     cout << " Enter a number: " ;     cin >> num ;     num % 2 == 0 ? cout << " This is an even number " : cout << " This is an odd number " ;     return 0 ; }

38: Write a ternary program to show that if your marks are lower than 33%, you fail. Otherwise you pass

  // Write a ternary program to show that if your marks are lower than 33%, you fail. Otherwise you pass #include < iostream > using namespace std ; int main () {     float marks , total , percent ;     cout << " Enter your obtain marks: " ;     cin >> marks ;     cout << " Enter your total marks: " ;     cin >> total ;     percent = ( marks / total ) * 100 ;     percent >= 33 ? cout << " You Pass " : cout << " You fail " ;           return 0 ; }

37: ***(Syntax)*** Ternary operator

Ternary Operator in C++ A ternary operator evaluates the test condition and executes a block of code based on the result of the condition. In other words,  "You can use ternary operator in place of simple if-else statement." Its name is  Ternary Operator  beccause it take three operands:  condition, expression1, expression2 Its  syntax   is :   condition ? expression1 : expression2 ;   Here: first operand is a  condition , and: If the condition is true,  expression1   will be executed. If the condition is false,  expression2   will be executed. Another way to write this is: cout << ( codition ? Expression1 : Expression2 );

36: Build a program to calculate the hypotenuse of a right triangle

  // Build a program to calculate the hypotenuse of a right triangle #include < iostream > #include < cmath > using namespace std ; int main () {     float base , perpendicular , hypotenuse ;     cout << " Enter the base of the triangle: " ;     cin >> base ;     cout << " Enter the perpendicular of the triangle: " ;     cin >> perpendicular ;     hypotenuse = sqrt (( pow ( base , 2 ) + pow ( perpendicular , 2 )));     cout << " The hypotenuse of this triangle is: " << hypotenuse << endl ;     return 0 ; } Or: // Build a program to calculate the hypotenuse of a right triangle #include < iostream > #include < cmath > using namespace std ; int main () {     float base , perpendicular , hypotenuse ;     cout << " Enter the base of the triangle: " ;     cin >> base ; ...

35: Write a program in C++ to check whether a number is prime or not. Go to the editor // Sample Output: // Input a number to check prime or not: 13 // The entered number is a prime number.

  // 5. Write a program in C++ to check whether a number is prime or not. Go to the editor // Sample Output: // Input a number to check prime or not: 13 // The entered number is a prime number. #include < iostream > using namespace std ; int main () {     int num , number = 0 ;     cout << " Input a number to check whether prime number or not: " ;     cin >> num ;     for ( int i = 1 ; i <= num ; i ++)     {         if ( num % i == 0 )         {             number ++;         }     }     if ( number == 2 )     {         cout << " The entered number is a prime number. \n " ;     }     else         cout << " The entered number is not a prime number \n " ;     return 0 ; }

34: Write a program in C++ to display n terms of natural number and their sum. // Sample Output: // Input a number of terms: 7 // The natural numbers upto 7th terms are: // 1 2 3 4 5 6 7 // The sum of the natural numbers is: 28

  // 3. Write a program in C++ to display n terms of natural number and their sum. // Sample Output: // Input a number of terms: 7 // The natural numbers upto 7th terms are: // 1 2 3 4 5 6 7 // The sum of the natural numbers is: 28 #include < iostream > using namespace std ; int sum = 0 ; int main () {     int num ;     cout << " Input a number of terms: " ;     cin >> num ;     cout << " The natural numbers upto " << num << " number of terms are: " << endl ;     for ( int i = 1 ; i <= num ; i ++)     {         cout << i << " " ;     }     for ( int i = 1 ; i <= num ; i ++)     {         sum = sum + i ;     }     cout << endl           << " The sum of natural numbers is: " << sum << end...