Answer to Question #163595 in C++ for sakshi

Question #163595

Write a menu driven program for the following operations using loop concept without array: 

a) series of prime numbers from 1 to 1000

b) series of perfect numbers from 1 to 1000

c) series of Armstrong numbers from 1 to 10000

d) range is given by user, after that display count of digits of each number

e) range is given by user, after that display sum of digits of each number

f) range is given by user, after that display max perfect number from that range


1
Expert's answer
2021-02-15T00:02:17-0500
#include <iostream>
using namespace std;
bool checkPrimeNumber(int n) {
    bool isPrime = true;

    // 0 and 1 are not prime numbers
    if (n == 0 || n == 1) {
        isPrime = false;
    }
    else {
        for (int i = 2; i <= n / 2; ++i) {
            if (n % i == 0) {
                isPrime = false;
                break;
            }
        }
    }
    return isPrime;
}
bool isPerfect(int n) {
    int sum = 0;
    for (int i = 1; i < n; i++) {

        if (n % i == 0)
            sum += i;
    }
    if (sum == n) {
        return true;
    }
    else {
        return false;
    }
}
bool checkArmstrongNumber(int num) {
   int temp, sum=0, digit;
   bool isArm;

   temp = num;
   while(temp != 0) {
      digit = temp % 10;
      sum = sum +(digit * digit * digit);
      temp = temp/10;
   }
   if (sum==num)
      isArm = true;
   else
      isArm = false;

   return isArm;
}
int main () {
    cout << "Choose one:";
    cout << "\na) series of prime numbers from 1 to 1000";
    cout << "\nb) series of perfect numbers from 1 to 1000";
    cout << "\nc) series of Armstrong numbers from 1 to 10000";
    cout << "\nd) range is given by user, after that display count of digits of each number";
    cout << "\ne) range is given by user, after that display sum of digits of each number";
    cout << "\nf) range is given by user, after that display max perfect number from that range";
    
    char option;
    
    while (true) {
        cout << "\nEnter your otion: "; cin >> option;
        if (option == 'a') {
            int i = 0;
            cout << "Prime numbers [1, 1000]: ";
            while (i <= 1000) {
                if(checkPrimeNumber(i)) {
                    cout << i << " ";
                }
                i++;
            }
        }
        else if (option == 'b') {
            int i = 0;
            cout << "Perfect numbers [1, 1000]: ";
            while (i <= 1000) {
                if(isPerfect(i)) {
                    cout << i << " ";
                }
                i++;
            }
        }
        else if (option == 'c') {
            int i = 0;
            cout << "Armstrong numbers [1, 10000]: ";
            while (i <= 10000) {
                if(checkArmstrongNumber(i)) {
                    cout << i << " ";
                }
                i++;
            }
        }
        else if (option == 'd') {
            int a, b;
            cout << "Enter range: [a, b]\n";
            cout << "a: "; cin >> a;
            cout << "b: "; cin >> b;
            while (a++ <= b) {
                int count = 0;
                while (a) {
                    a = a / 10;
                    count++;
                }
                cout << "the number of digits in a " << count;
            }
        }
        else {
            break;
        }
    }
    
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog