c++ programming problems

39
C++ Programming Problems (LOOP STRUCTURES) 1. Write a program that will display the even numbers from 1 to 100. #include<iostream> #include<conio.h> using std::cout; using std::endl; int main() { int counter; for(int counter = 1; counter <= 100; counter++){ if((counter % 2) == 0) cout<< counter<<endl; } getch(); return 0; } 2. Write a program that will compute for and display the sum of the numbers divisible by 3, ranging from 1 to 100. #include<iostream> #include<conio.h> using std::cout; int main() { int counter,sum=0; for(int counter = 1; counter <= 100; counter++){ if((counter % 3) == 0){ sum = sum + counter; } } cout<<"The sum of the numbers divisible by 3: "<<sum; getch();

Upload: daisy-lagapa

Post on 02-May-2017

225 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: C++ Programming Problems

C++ Programming Problems (LOOP STRUCTURES) 1.              Write a program that will display the even numbers from 1 to 100.  #include<iostream>#include<conio.h>using std::cout;using std::endl; int main(){     int counter;     for(int counter = 1; counter <= 100; counter++){                 if((counter % 2) == 0)                             cout<< counter<<endl;     }     getch();     return 0;} 2.              Write a program that will compute for and display the sum of the numbers divis-ible by 3, ranging from 1 to 100.  #include<iostream>#include<conio.h>using std::cout; int main(){     int counter,sum=0;     for(int counter = 1; counter <= 100; counter++){                if((counter % 3) == 0){                             sum = sum + counter;                                             }     }     cout<<"The sum of the numbers divisible by 3: "<<sum;     getch();     return 0;}       

Page 2: C++ Programming Problems

3.              Write a program that will compute and display the sum of the powers of 2 from the first power to the nth power, where n is a nonnegative integer given by the user. ex. if n = 4, then compute 21+22+23+24 = 30; thus display 30 ex. if n = 5, then compute 21+22+23+24+25 = 62; thus display 62 . #include<iostream>#include<conio.h>using std::cout;using std::cin;int main(){     int counter,power,sum=0,x,base;     cout<<"Enter N power: ";     cin>>power;     for(counter = 1; counter <= power; counter++){                 base = 1;                 for(x = 1; x <= counter; x++)                       base = base * 2;                  sum = sum + base;                     }     cout<<"The sum of the powers of 2 from the first power to the nth power: "<<sum;     getch();     return 0;}4.              Write a program that will compute for the following given n & x:  

x1/1 + x2/2 + x4/3 + ... + xn/n     #include<iostream>#include<conio.h>using std::cout;using std::cin;using std::endl;int main(){     int counter,n,x,y;     float result=0, sum=0,base;     cout<<"Enter value for X: ";     cin>>x;     cout<<"Enter value for N: ";     cin>>n;          for(counter = 1; counter <= n; counter++){                 base = 1;                 for(y = 1; y <= counter; y++)                       base = base * x;

Page 3: C++ Programming Problems

                                        result = base / counter;                 sum = sum + result;                                 }     cout<<"The result is: "<<sum;     getch();     return 0;}  

5.              Given an input n assumed one-digit, display a pattern.  ex. if n = 5, display 

1 1_2

1_2_3 1_2_3_4 1_2_3_4_5

#include<iostream>#include<conio.h>using std::cout;using std::cin;using std::endl;int main(){     int z,n,x,y;          cout<<"Enter value for N: ";     cin>>n;          for(x = 1; x <= n; x++){           for(z = n; z > x; z--)                 cout<<" ";             for(y = 1; y <= x; y++){                                                        if(y == x)                      cout<<y;                   else                      cout<<y<<"_";                                                                       }                     cout<<endl;                                     }         getch();     return 0;}

   

Page 4: C++ Programming Problems

7.         Write a program that will display a pattern depending on the value of n entered by the user. ex. if n = 3, display                                                     ex. if n= 4, display*      *                                                                          *           *               *  *                                                                               *      *                *                                                                                   *  *                                                                                                       *   #include<iostream>#include<conio.h>using std::cout;using std::cin;using std::endl;int main(){     int z,n,x,y;          cout<<"Enter value for N: ";     cin>>n;          for(x = 1; x <= n; x++){           for(z = 0; z < x; z++)                 cout<<" ";            cout<<"*";             for(y = x; y < n; y++){                   cout<<" ";                                                              }             for(y = x+1; y < n; y++){                   cout<<" ";                                                              }            if(x < n)                cout<<"*";                     cout<<endl;                                     }         getch();     return 0;}                              

Page 5: C++ Programming Problems

6.              Write a program to display all combinations of A and B (from 1 to 100) that will make the expression false:  (5 * A) – (3 * B) > 30  #include<conio.h>using std::cout;using std::cin;using std::endl;int main(){     int a,b;     cout<<"A\t\tB"<<endl;          for(a = 1; a <= 100; a++){           for(b = 1; b <= 100; b++)                 if(((5 * a) - (3 * b)) <= 30 )                       cout<<a<<"\t\t"<<b<<endl;                                      }          getch();     return 0;}8.         Write a program that reverses an input number.  #include<iostream>#include<conio.h>using std::cout;using std::cin;using std::endl;int main(){     int mod=0,num,rev=0;          cout<<"Enter numeric value: ";     cin>>num;          while(num > 0){            mod = num % 10;            rev = ( rev * 10 ) + mod;            num=num/10;      }      cout<<"Reverse of the given number: "<< rev;      getch();     return 0;}  

Page 6: C++ Programming Problems

9.         Write a program that gets as input a binary number and outputs its corresponding decimal equivalent. Declare your integer variables as long. A long declaration accom-modates 8 digits for a number. Example:  Enter a binary number: 1101    Decimal equivalent: 13 #include<iostream>#include<conio.h>using std::cout;using std::cin;using std::endl;int main(){     int binary, digit, base = 0, decimal = 0;             cout<<"Enter a binary number: ";             cin>>binary;             while(binary){                        digit = binary % 10;                        decimal += digit << base;                        base += 1;                        binary /= 10;            }            cout<<"Decimal equivalent is: "<<decimal;     getch();    return 0;} 10.           Given the value of n from the user, write a program that will display the first n even numbers.

Ex. if n = 5, then display the first 5 even integers which are 2, 4, 6, 8, 10. 

#include<iostream>#include<conio.h>using std::cout;using std::cin; int main(){     int n,x,num=2;          cout<<"Enter no. of even integers: ";     cin>>n;         for(x = 1; x <= n; x++){           if(x==n)               cout<<num<<" ";           else               cout<<num<<" , ";

Page 7: C++ Programming Problems

           num+=2;                                              }         getch();     return 0;}

  

11.           Write a program that accepts a number n and displays the sum of even numbers and the sum of odd numbers from 1 to n. #include<iostream>#include<conio.h>using std::cout;using std::cin;using std::endl;int main(){     int n,x,sumEven = 0,sumOdd=0;          cout<<"Enter value for N: ";     cin>>n;         for(x = 1; x <= n; x++){           if(x%2 == 0)               sumEven = sumEven + x;           else               sumOdd = sumOdd + x;                                                         }      cout<<"Sum of Even Numbers: "<<sumEven<<endl;     cout<<"Sum of Odd Numbers : "<<sumOdd;     getch();     return 0;} 12.       Write a program that will compute for ax given real value a and positive integer x. #include<iostream>#include<conio.h>using std::cout;using std::cin;int main(){     int n,x;       float a = 0,res =1;        cout<<"Enter value for A: ";     cin>>a;         cout<<"Enter value for x: ";     cin>>x;      for(n = 1; n <= x; n++){

Page 8: C++ Programming Problems

           res = res * a;                                                       }      cout<<"Result is: "<<res;          getch();     return 0;}13.       Write a program that reads in a number n and outputs the sum of the squares of the numbers from 1 to n.            ex. if n = 3, then display 14 because 12 + 22 + 32 = 14.  #include<iostream>#include<conio.h>using std::cout;using std::cin;int main(){     int n,x,sum=0;            cout<<"Enter value for n: ";     cin>>n;      for(x = 1; x <= n; x++){           sum = sum + (x*x);                                                       }      cout<<"Result is: "<<sum;          getch();     return 0;}14.       Write a program that asks for the start month and end month and compute for the total number of days from the start month to the end month. Do not use if-then-else, use switch statement. ex.        If start month = 2 and end month = 5, then Total = 28 + 31 + 30 + 31 = 120. Display 120. #include<iostream>#include<conio.h>using std::cout;using std::cin;int main(){     int x,totalDays=0,days,startMonth,endMonth;            cout<<"Enter number of start month: ";     cin>>startMonth;      cout<<"Enter number of end month: ";     cin>>endMonth;      for(x = startMonth; x <= endMonth; x++){

Page 9: C++ Programming Problems

           switch(x){              case 1:                   days = 31;                   break;              case 2:                   days = 28;                   break;              case 3:                   days = 31;                   break;              case 4:                   days = 30;                   break;              case 5:                   days = 31;                   break;              case 6:                   days = 30;                   break;              case 7:                   days = 31;                   break;              case 8:                   days = 31;                   break;              case 9:                   days = 30;                    break;              case 10:                   days = 30;                    break;              case 11:                   days = 30;                    break;              case 12:                   days = 31;                     break;              }             totalDays = totalDays + days;                                                     }      cout<<"Total Number of Days: "<<totalDays;          getch();     return 0;}

15.       Write a program that will display the following: 4, 8, 12, 16, ..., 496

Page 10: C++ Programming Problems

 #include<iostream>#include<conio.h>using std::cout;using std::cin;using std::endl;int main(){     int x;          for(x = 4; x < 500; x+=4){           if(x == 500-4)                cout<<x<<"";           else                cout<<x<<", ";                                                      }          getch();     return 0;} 16.       Write a program that reads in a number n, and then reads in n numbers. The num-bers will be alternately added and subtracted. ex.        If n = 5 and the numbers entered are 4, 14, 5, 6, 1,  then compute for 4 + 14 - 5 + 6 - 1 = 18. Display 18.  #include<iostream>#include<conio.h>using std::cout;using std::cin;int main(){     int n,x,result=0,num;           cout<<"Enter value for n: ";     cin>>n;      for(x = 1; x <= n; x++){           cin>>num;           if(x !=1){              if(x%2 == 0)                  result = result + num;              else                  result = result - num;             }            else{ result = num;}                                                    }      cout<<"Result is: "<<result;          getch();     return 0;

Page 11: C++ Programming Problems

}

17.       Write a program that will ask the user to enter 10 numbers and display the largest number entered.  #include<iostream>#include<conio.h>using std::cout;using std::cin;int main(){     int n,x,largest=0;            for(x = 1; x <= 10; x++){           cout<<"Enter value "<<x<<": ";           cin>>n;            if(largest < n)                  largest = n;                                                     }      cout<<"Largest number is: "<<largest;          getch();     return 0;} 18.       Write a program that will ask the user to enter 100 numbers and display on the screen the highest and lowest of these numbers.  #include<iostream>#include<conio.h>using std::cout;using std::cin;using std::endl;int main(){     int n,x,largest=0,smallest;            for(x = 1; x <= 10; x++){                      cout<<"Enter value "<<x<<": ";           cin>>n;            if(largest < n)                  largest = n;            if(x == 1){                smallest = n;           }           else{                 if(smallest > n)

Page 12: C++ Programming Problems

                     smallest = n;             }                                                 }      cout<<"Largest number is: "<<largest<<endl;     cout<<"Smallest number is: "<<smallest;            getch();     return 0;}  19.        Write a program that will ask the user for a number and display all the factors of the number. #include<iostream>#include<conio.h> using std::cout;using std::cin; int main(){      int n, f, counter = 0;          cout<<"Enter Number to be factored: ";      cin>>n;      for(f = 1; f <= n; f++){         if(n % f == 0){            cout <<f<<"  ";            counter++;             if(counter == 4){                              counter = 0;             }         }      }      getch();      return 0;}      20.        Read any two numbers that represent an amount of money to be deposited and the annual interest rate. Compute and print the annual balances for the first 10 years.#include<iostream>#include<conio.h>

Page 13: C++ Programming Problems

using std::cout;using std::cin;using std::endl;int main(){     int x;       float deposit,balance, interest;        cout<<"Enter amount of deposit: ";     cin>>deposit;         cout<<"Enter annual interest rate: ";     cin>>interest;      balance = deposit;     cout<<endl<<"Years"<<"\t\t"<<"Annual Balances"<<endl<<endl;     for(x = 1; x <= 10; x++){           balance = balance + (balance * (interest/100.0));           cout<<x<<"\t\t"<<balance<<endl;                                                       }      getch();     return 0;}  21.        Write a program that will ask the user for a number n and display the nth Fibon-acci number Fn. Fn defined as follows:                         F0 = 0                        F1 = 1                        F2 = 0 + 1 = 1                        F3 = 1 + 1 = 2                        F4 = 1 + 2 = 3                        F5 = 2 + 3 = 5                                    :                                                           Fn = Fn-1 + Fn-2   #include<iostream>#include<conio.h> using std::cout;using std::cin;using std::endl; int main(){ int n, first = 0, second = 1, next, c=0;    cout<<"Enter a number: ";   cin>>n;   cout<<"F"<<c<<" = "<<first<<endl;

Page 14: C++ Programming Problems

   cout<<"F"<<c+1<<" = "<<second<<endl;   for ( c = 0 ; c <= (n-3) ; c++ ){         next = first + second;                        cout<<"F"<<c+2<<" = "<<first<<" + "<<second<<" = "<<next<<endl;         first = second;         second = next;   }   getch();   return 0;}22.    Write a program that would print out a part of the multiplication table. Get as input the start number and end number.  

Example:        Enter start number:  3 Enter end number  :  5

  Output is 1          2          3         4         5         6         7         8         9         10 3          3          6          9          12        15        18        21        24        27        30 4          4          8          12        16        20        24        28        32        36        40 5          5          10        15        20        25        30        35        40        45        50 #include <iostream>#include<conio.h> using namespace std;int main(){  int startNum,endNum,x; cout<<"Enter start number: "; cin>>startNum; cout<<"Enter end number:   "; cin>>endNum; cout<<endl<<endl; cout<<"     "; for(x = 1; x <= 10; x++){              cout<<x<<" ";       if(x<=99)cout<<" ";       if(x<=9)cout<<" ";} cout<<endl<<endl; for(int y = startNum; y <= endNum; y++){         cout<<" ";         cout<<y;         cout<<"   ";         for(int x = 1; x <= 10; x++){                               cout<<x*y<<" ";              if(x*y<=99)cout<<" ";

Page 15: C++ Programming Problems

              if(x*y<=9)cout<<" ";         }         cout<<endl<<endl; } getch();return 0;}23.       Write a program that will compute and display the sum of the factorials of the numbers from 1 to n, where n is a nonnegative integer given by the user. ex. if n = 3, then compute 1!+2!+3! = 9; thus display 9

ex. if n = 4, then compute 1!+2!+3!+4! = 33; thus display 33  #include<iostream>#include<conio.h>using std::cout;using std::cin;using std::endl;int main(){   int n, x, counter, total = 1,sum = 0;   cout << "Enter value for n: ";   cin >> n;   for(x = 1; x <= n; x++){          total = 1;          for (counter = 1; counter <= x; counter++)              total = total * counter;          sum = sum + total;   }   cout <<"Total factorial is: "<<sum;   getch();     return 0;}                   24.           Write a program that will compute and display the sum of the powers of x from the first power to the nth power, where x and n are nonnegative integers given by the user.

ex. if x = 3 and n = 4, then compute 31 + 32 + 33 + 34 = 120;                         thus display 120

            ex. if x = 2 and n = 5, then compute 21 + 22 + 23 + 24 + 25 = 62;                                     thus display 62  #include<iostream>#include<conio.h>using std::cout;using std::cin;using std::endl;

Page 16: C++ Programming Problems

int main(){   int n, x,y,sum = 0,z ,total=0;   cout << "Enter value for x: ";   cin >> x;   cout << "Enter value for n: ";   cin >> n;   for(y = 1; y <= n; y++){         total = 1;         for(z = 1; z <= y; z++)               total = total * x;         sum = sum + total;             }   cout <<"The sum power is: "<<sum;   getch();     return 0;}25.           Write a program that will display a pattern depending on n.

ex. if n = 4, display                 ex. if n = 3, display     *                    *                        **                                            **                        ***                                          ***                        ****    #include<iostream>#include<conio.h>using std::cout;using std::cin;using std::endl;int main(){   int x,y,n;   cout << "Enter value for n: ";   cin >> n;   for(x = 1; x <= n; x++){         for(y = 1; y <= x; y++)              cout<<"*";          cout<<endl;           }   getch();     return 0;}                                   26.       Given an input n assumed one-digit, display a pattern.  ex. if n = 4, display 1 12 123 1234

Page 17: C++ Programming Problems

#include<iostream>#include<conio.h>using std::cout;using std::cin;using std::endl;int main(){   int x,y,n;   cout << "Enter value for n: ";   cin >> n;   for(x = 1; x <= n; x++){         for(y = 1; y <= x; y++)              cout<<y;          cout<<endl;           }   getch();     return 0;}

 27.       Given an input n assumed one-digit, display a pattern.  ex. if n = 4, display1234 123 12 1

#include<iostream>#include<conio.h>using std::cout;using std::cin;using std::endl;int main(){   int x,y,n;   cout << "Enter value for n: ";   cin >> n;   for(x = 1; x <= n; x++){         for(y = 1; y <= (n-x)+1; y++)              cout<<y;          cout<<endl;           }   getch();     return 0;} 28.       Write a program that will display a pattern depending on n. ex. if n = 4, display ex. if n = 3, display

Page 18: C++ Programming Problems

*                             ** *                           * ** * *                         * * *

* * * *  #include<iostream>#include<conio.h>using std::cout;using std::cin;using std::endl;int main(){     int z,n,x,y;          cout<<"Enter value for N: ";     cin>>n;          for(x = 1; x <= n; x++){           for(z = n; z > x; z--)                 cout<<" ";             for(y = 1; y <= x; y++){                                                        if(y == x)                      cout<<"*";                   else                      cout<<"*"<<" ";                                                                       }                     cout<<endl;                                     }         getch();     return 0;}

29.       Given an input n assumed one-digit, display a pattern.  ex. if n = 4, display 1 21

321 4321

#include<iostream>#include<conio.h>using std::cout;using std::cin;using std::endl;int main(){     int z,n,x,y;

Page 19: C++ Programming Problems

          cout<<"Enter value for N: ";     cin>>n;          for(x = 1; x <= n; x++){           for(z = n; z > x; z--)                 cout<<" ";             for(y = x; y >= 1; y--){                                                 cout<<y;                              }                     cout<<endl;                                     }         getch();     return 0;}  30.       Given an input n assumed one-digit, display a pattern.  ex. if n = 4, display

4 3 2 1 4 3 2 4 3 4

#include<iostream>#include<conio.h>using std::cout;using std::cin;using std::endl;int main(){     int z,n,x,y;          cout<<"Enter value for N: ";     cin>>n;          for(x = 1; x <= n; x++){           for(z = 1; z <= x; z++)                 cout<<" ";             for(y = n; y >= x; y--){                                                  cout<<y<<" ";                                                 }                     cout<<endl;                                     }         getch();     return 0;} 

Page 20: C++ Programming Problems

31.           Write a program that will create a rectangle based on length and width. Use 2 iter-ative/loop statements.

ex. if  length = 5  width = 10  display            **********            *                *            *                *            *                *            **********

#include<iostream>#include<conio.h>using std::cout;using std::cin;using std::endl;int main(){     int z,length,width,x,y;          cout<<"Enter value length: ";     cin>>length;     cout<<"Enter value width: ";     cin>>width;     for(x = 1; x <= length; x++){            cout<<"*";           for(z = 1; z < width-1; z++){                 if(x == 1 || x == length)                      cout<<"*";                 else                      cout<<" ";                   }                 cout<<"*";                                 cout<<endl;                                     }         getch();     return 0;} 33.       Write a program that will display a pattern depending on the value of n entered by the user.

ex. if n = 3, display                             ex. if n= 4, display                                     *                                                          *                                  *  *                                                      *  *                                 * * *                                                    * * *                                  *  *                                                    * * * *                                    *                                                       * * *                                                                                               * *                                                                                                *#include<iostream>

Page 21: C++ Programming Problems

#include<conio.h>using std::cout;using std::cin;using std::endl;int main(){     int z,n,x,y;          cout<<"Enter value for N: ";     cin>>n;          for(x = 1; x <= n; x++){           for(z = n; z > x; z--)                 cout<<" ";             for(y = 1; y <= x; y++){                                                        if(y == x)                      cout<<"*";                   else                      cout<<"*"<<" ";                                                 }                     cout<<endl;                                     }        for(x = 1; x <= n; x++){           for(z = 1; z <= x; z++)                 cout<<" ";             for(y = n - 1; y >= x; y--){                                                if(y == x)                      cout<<"*";                   else                      cout<<"*"<<" ";                                                 }                     cout<<endl;                                     }           getch();     return 0;} 34.       Write a program that will display a pattern depending on the value of n entered by the user.

 ex. if n = 3, display                            ex. if n= 4, display                                     *                                                          *                                  *  *                                                      *  *                                *      *                                                   *     *#include<iostream>#include<conio.h>

Page 22: C++ Programming Problems

using std::cout;using std::cin;using std::endl;int main(){     int z,n,x,y;          cout<<"Enter value for N: ";     cin>>n;          for(x = 1; x <= n; x++){           for(z = n; z >= x; z--)                 cout<<" ";            cout<<"*";            for(y = 1; y < x ; y++){                  cout<<" ";                                                              }             for(y = 1; y < x-1; y++){                   cout<<" ";                                                              }            if (x == 1) cout<<" ";           else if(x <= n )                cout<<"*";                     cout<<endl;                                     }         getch();     return 0;}                                                                                                      *        *    35.           Write a program that will display a pattern depending on the value of n entered by the user. ex. if n = 4, display                            ex. if n= 5, display                                    ****                                                    *****                                           *                                                           *                                       *                                                           *                                    ****                                                      *                                                                                                *****  #include<iostream>#include<conio.h>using std::cout;using std::cin;int main()

Page 23: C++ Programming Problems

{int x,num,y,z;cout<<"Enter value for N: ";cin>>num;for(x = 1;x < num; x++){            if(x == 1 || x == num-1){                        for(y = 1; y <= num; y++)                                    cout<<"*";            }            cout<<"\n";            for(z = x+1; z < num; z++){                        cout<<" ";            }            if(x == num-1)               cout<<" ";            else               cout<<"*";cout<<"\n";}getch();return 0;}                                    36.       Write a program that will compute for the following given n: 1/21 + 2/22 + 3/23 + 4/24 + ... + n/2n

 #include<iostream>#include<conio.h>using std::cout;using std::cin;using std::endl;int main(){     int counter,n,x,y;     float result=0, sum=0,base;     cout<<"Enter value for N: ";     cin>>n;          for(counter = 1; counter <= n; counter++){                 base = 1;                 for(y = 1; y <= counter; y++)                       base = base * 2;                                        result = counter / base;                 sum = sum + result;                                 }

Page 24: C++ Programming Problems

     cout<<"The result is: "<<sum;     getch();     return 0;}37.       Write a program that will compute for the following given n & x: x0/0! + x1/1! + x2/2! + x3/3! + ... + xn/n!   #include<iostream>#include<conio.h>using std::cout;using std::cin;using std::endl;int main(){     int counter,n,x,y;     float result=0, sum=0,base;     cout<<"Enter value for X: ";     cin>>x;     cout<<"Enter value for N: ";     cin>>n;          for(counter = 1; counter <= n; counter++){                 base = 1;                 total = 1;                 for(y = 1; y <= counter; y++)                       base = base * x;                 for (y = 1; y <= counter; y++)                       total = total * y;                                           result = base / total;                 sum = sum + result;                                 }     cout<<"The result is: "<<sum;     getch();     return 0;}

 38.           Write a program that will perform prime factorization.

ex.        Enter number: 24 Prime factorization: 2, 2, 2, 3

ex.        Enter number: 7 Prime factorization: 7 #include<iostream>#include<conio.h>using std::cout;using std::cin;

Page 25: C++ Programming Problems

int main(){   int n,x;   int d = 2;   cout<<"Enter a value: ";   cin>>n;   if(n < 2) return 1;   cout<<"Prime factors of " <<n<<" is ";             while(d < n) {                if(n % d == 0) {                  cout<<d<<" , ";                  n /= d;                }                else {                  if(d == 2) d = 3;                  else d += 2;                }              }              cout<<d;getch();return 0;}

39.           A perfect number is a positive integer that is equal to the sum of all those positive integers (excluding itself) that divide it evenly. The first perfect number is 6 because its divisors (excluding itself) are 1, 2, and 3, and because 6 = 1 + 2 + 3. Write a program to find the first three perfect numbers (include 6 as one of the three). #include<iostream>#include<conio.h>int main()                 {    int i=1, u=1, sum=0;   while(i<=500){    while(u<=500){                                    if(u<i){      if(i%u==0 )      sum=sum+u;     }                               u++;   }                             if(sum==i){    cout<<i<<" is a perfect number."<<"\n";   }   i++;   u=1;  sum=0; }                                getch(); }     

Page 26: C++ Programming Problems

40.           Write a program that takes one real value as input and computes the first integer n such that 2n is greater than or equal to the input value. The program should output both n and 2n. #include<iostream>#include<conio.h>using std::cout;using std::cin;using std::endl;int main(){int x,count = 0, power = 1;float num = 0; cout<<"Enter a value: ";cin>>num;while(power < num){            power = power * 2;            count++;}cout<<"N value: "<<count<<endl;cout<<"2 raise to N: "<<power;getch();return 0;} 41.           Write a program to read in a real value x and output the integer n closest to the cube root of x. The value of x is assumed positive.#include <iostream.h>#include<conio.h>#include<math.h>int main(){    int count = 0;    float cubeRoot = 0, num;    clrscr();    cout<<"Enter a value: ";    cin>>num;    cubeRoot = pow(num,(1/3.0));    while(count < cubeRoot){                        count++;    }    cout<<"Closest Nth value to the cube root of "<<num<<" is: "<<count;    getch();    return 0;}

Page 27: C++ Programming Problems

 42.           Write a program that finds the lowest odd integer among the values entered by the user. Stop asking values when a value less than 1 has been entered. ex. if the values entered by the user are 3, 8, 1, 6, 3, 4, -5 then display 1 ex. if the values entered by the user are 6, 4, 8, 0 then display 'No odd integer' #include<iostream>#include<conio.h>using std::cout;using std::cin; int main(){     int num = 1,x=0,smallest=1;     cout<<"Enter a value: ";     cin>>num;     smallest = num;     while(num >= 1){                    if(num%2 != 0){                                    x = 1;                                    if(smallest > num)                                                    smallest = num;                   }                   cout<<"Enter a value: ";                   cin>>num;     }     if(x == 0)            cout<<"No odd integer!";     else            cout<<"Smallest odd value is: "<<smallest;     getch();     return 0;} 43.           Write a program that will ask for a long number and count how many digits are there in the number.

ex. Enter num: 10854 Output: 5

#include<iostream>#include<conio.h>using std::cout;using std::cin;int main(){

Page 28: C++ Programming Problems

  int num,count=0;  cout<<"Enter a number: ";  cin>>num;   for(;num!=0;num=num/10)      count++;   cout<<"Total digits is:  "<<count;  getch();  return 0;} 44.           Write a program that will ask for a long number and count how many digits in the number are even and how many are odd.

  ex. Enter num: 80572 Output: 3 digits are even

                         2 digits are odd

45.           Write a program that will ask for a value for num and display the product of its even positioned digits (i.e., digits at the tens unit, thousands unit,...). Use long for num.

ex. if num = 412473, then compute for 7*2*4=56; thus display 56 ex. if num = 15678, then compute for 7*5=35; thus display 35 ex. if num = 3, then display 0

 46.           Write a program that will ask for a value for num and display the product of its odd positioned digits (i.e., digits at the ones unit, hundreds unit,...). Use long for num.   

ex. if num = 12473, then compute for 3*4*1 = 12; thus display 12 ex. if num = 5678, then compute for 8 * 6 = 48; thus display 48 ex. if num = 3, then display 3  

47.           Write a program that asks for an input integer and displays the digits in reverse. ex. Enter number: 3562                 ex. Enter number: 10240

Output: 2653                               Output: 04201  #include<iostream>#include<conio.h>using std::cout;using std::cin;using std::endl;int main(){     int mod=0,num,rev=0;          cout<<"Enter numeric value: ";     cin>>num;          while(num > 0){            mod = num % 10;

Page 29: C++ Programming Problems

           rev = ( rev * 10 ) + mod;            num=num/10;      }      cout<<"Reverse of the given number: "<< rev;      getch();     return 0;} 48.       Write a program that asks for a long value and swap every two digits in this value. ex. If n = 12345, display 21435 If n = 811230, display 182103 If n = 10101, display 01011   49.       Write a program that asks for values from the user and find out which number was entered the most number of times (mode) and how many times this number was entered (frequency). Stop asking for values once a -1 has been entered. Assume that the numbers entered are positive and in increasing order and there is only one mode in the given inputs.  

ex. If the following numbers were entered: 1, 2, 2, 2, 3, 3, 6, 6, 7, 7, 7, 7, -1 The mode is 7 and its frequency is 4

ex.        If the following numbers were entered: 2, 3, 3, 3, 4, 5, 5, 7, 8, 8, 8, 8, 8, -1 The mode is 8 and its frequency is 5  #include<iostream>#include<conio.h>using std::cout;using std::cin;int main(){int num = 0,x, count=1, largest = 0,prevNum=0,mode=0;while(num >= 0){            cout<<"Enter a value: ";            cin>>num;            if(num == prevNum){                        count++;            }            else{                        if(count > largest){                                    largest = count;                                    mode = prevNum;                        }                        count = 1;            }

Page 30: C++ Programming Problems

            prevNum = num;}cout<<"The mode is "<<mode<<" and its frequency is "<<largest;getch();return 0;} 50. Write a program that will ask for a positive integer number and will display the octal equivalent of the given number.

#include<iostream.h>#include<conio.h> int main(){                int num;                        int total[10],i;            clrscr();                        cout << "Please enter a decimal: ";                        cin >> num;                        for(i = 0; num!=0;i++){                                    total[i] = num % 8;                                    num /= 8;                        }                        i--;                        cout<<"Octal Equivalent: ";                        for(;i>=0;i--)                                    cout<<total[i];                         getch();                        return 0;   }