Posts

67: Write a program in C++ to find the sum of digits of a given number

  // Write a program in C++ to find the sum of digits of a given number. Go to the editor //     Sample Output: //     Input a number: 1234 //     The sum of digits of 1234 is: 10 //     ************************************************************************************************** #include < iostream > using namespace std ; int main () {     int num ;     cout << " \n Input a number: " ;     cin >> num ;     int num_for = num , num_while = num ;     cout << " \n ******************************************************** \n\n " ;     int sum_for , rem_for ;     for ( sum_for = 0 ; num_for > 0 ; num_for = num_for / 10 )     {         rem_for = num_for % 10 ;         sum_for = sum_for + rem_for ;     }     cout << " The sum of digits of " <...

66: Write a program in C++ to find the sum of digits of a given number

// Write a program in C++ to find the sum of digits of a given number. Go to the editor //     Sample Output: //     Input a number: 1234 //     The sum of digits of 1234 is: 10 //     ************************************************************************************************** #include < iostream > using namespace std ; int main () {     int num , numr ;     cout << " Input a number: " ;     cin >> num ;     numr = num ;     int a = 0 , sum = 0 ;     while ( num > 0 )     {         a = num % 10 ;         num = num / 10 ;         sum = sum + a ;     }     cout << " The sum of digits of " << numr << " is: " << sum ;     return 0 ; }

65: Write a program in C++ to find the Greatest Common Divisor (GCD) of two numbers.

  // Write a program in C++ to find the Greatest Common Divisor (GCD) of two numbers. Go to the editor //     Sample Output: //     Input the first number: 25 //     Input the second number: 15 //     The Greatest Common Divisor is: 5 //     ************************************************************************************************** #include < iostream > using namespace std ; int main () {     int num1 , num2 , gcd = 0 ;     cout << " Input the first number: " ;     cin >> num1 ;     cout << " Input the second number: " ;     cin >> num2 ;     for ( int i = 1 ; i <= num1 && i <= num2 ; i ++)     {         if ( num1 % i == 0 && num2 % i == 0 )         {             gcd = i ;         } ...

64: Write a program in C++ to find the last prime number occur before the entered number.

  // Write a program in C++ to find the last prime number occur before the entered number. //     Sample Output: //     Input a number to find the last prime number occurs before the number: 50 //     47 is the last prime number before 50 //     ************************************************************************************************** #include < iostream > using namespace std ; int main () {     int num ;     cout << " Input the number to find the last Prime number occurs before that number: " ;     cin >> num ;     int a , b ;     for ( int i = 1 ; i < num ; i ++)     {         a = 0 ;         for ( int j = 1 ; j <= i ; j ++)         {             if ( i % j == 0 )             {        ...

63: Write a program in C++ to find the factorial of a number using for and while loop

// Write a program in C++ to find the factorial of a number. Go to the editor //     Sample output: //     Input a number to find the factorial: 5 //     The factorial of the given number is: 120 //     ************************************************************************************************** #include < iostream > using namespace std ; int main () {     int num ;     cout << " \n Input a number to find factorial of: " ;     cin >> num ;     cout << " \n ***************************************************************** \n\n " ;     int a = 1 ;     for ( int i = 1 ; i <= num ; i ++)     {         a = a * i ;     }     cout << " The factorial of " << num << " is: " << a << endl ;     cout << " \n *******************************************...

62: Write a program in C++ to find prime number within a range

  // Write a program in C++ to find prime number within a range. Go to the editor //     Input number for starting range: 1 //     Input number for ending range: 100 //     The prime numbers between 1 and 100 are: //     2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 //     The total number of prime numbers between 1 to 100 is: 25 //     ************************************************************************************************** #include < iostream > using namespace std ; int main () {     int start , end ;     cout << " \n Input number for starting range: " ;     cin >> start ;     cout << " Input number for ending range: " ;     cin >> end ;     cout << " \n ********************************************************** \n\n " ;     cout << " The Prime numbers between " ...

61: Write a program in C++ to check whether a number is prime or not using for, while.

// Write a program in C++ to check whether a number is prime or not using for, while, do while loop. //     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 ;     cout << " \n Input a number to check prime or not: " ;     cin >> num ;     cout << " \n *********************************************************** \n\n " ;     int a = 0 ;     for ( int i = 1 ; i <= num ; i ++)     {         if ( num % i == 0 )         {             a ++;         }     }     if ( a == 2 )     {   ...

60: ***(Syntax)*** Write a program in C++ to find the perfect numbers between the user-defined range.

// Write a program in C++ to find the perfect numbers between the user-defined range. // The perfect numbers between 1 to 500 are: // 6 // 28 // 496 // ************************************************************************************************** #include < iostream > using namespace std ; int main () {     int start , end ;     cout << "\n Enter the starting number of range: " ;     cin >> start ;     cout << " Enter the ending number of range: " ;     cin >> end ;     cout << "\n ********************************************************************************* \n\n" ;     int sum ;     for ( int i = start ; i <= end ; i ++ )     {         sum = 0 ;         for ( int j = 1 ; j < i ; j ++ )         {             if ( i % j == 0 )   ...

59: Write a program that displays ASCII code of a character stored in a variable.

// Write a program that displays ASCII code of a character stored in a variable. #include < iostream > using namespace std ; int main () {     char num ;     cout << " Enter a number or a character: " ;     cin >> num ;     cout << " ASCII value of " << num << " is " << int ( num );     return 0 ; }

58: Write a program in C++ to find the first 10 natural numbers.

  // 1. Write a program in C++ to find the first 10 natural numbers. // Sample output: // The natural numbers are: // 1 2 3 4 5 6 7 8 9 10 #include < iostream > using namespace std ; int main () {     int j = 1 , k = 1 ;     for ( int i = 1 ; i <= 10 ; i ++)     {         cout << i << ' ' ;     }     cout << endl ;     while ( j <= 10 )     {         cout << j << ' ' ;         j ++;     }     cout << endl ;     do     {         cout << k << ' ' ;         k ++;     } while ( k <= 10 );     return 0 ; }

57: Write a program in C++ to check if the user entered a perfect number or not using for, while and do while loop

  // Write a program in C++ to check if the user entered a perfect number or not #include < iostream > using namespace std ; int main () {     int num ;     cout << " \n Enter a number: " ;     cin >> num ;     cout << " \n ************************************************************** \n\n " ;     int a = 0 ;     cout << " Factors of " << num << " are: " ;     for ( int i = 1 ; i < num ; i ++)     {         if ( num % i == 0 )         {             cout << i << ' ' ;             a = a + i ;         }     }     if ( a == num )     {         cout << " \n The number " << num << " is a Perfect number \n " ;     } ...

56: Write a program in C++ to display n terms of natural number and their sum. Go to the editor

// Write a program in C++ to display n terms of natural number and their sum. Go to the editor // 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 main () {     cout << " \n **************************************************************************** \n\n " ;     int numf , a = 0 ;     cout << " Enter the number of terms: " ;     cin >> numf ;     cout << " The natural numbers upto " << numf << " terms are: " << endl ;     for ( int i = 1 ; i <= numf ; i ++)     {         cout << i << " " ;     }     for ( int i = 1 ; i <= numf ; i ++) ...