Question

In: Computer Science

Write a C++ program that displays the numbers between 1000 and 9999, which are divisible by...

Write a C++ program that displays the numbers between 1000 and 9999, which are divisible by sum of the digits in them. For example, 2924 is divisible by (2+9+2+4 = 17). Your program should display all these possible numbers in the given range, and each number should be separated from the other by a space.

Solutions

Expert Solution

According to the problem statement above,

I have coded the function using C++

Below are the Snippets:

Code Snippet:

Output Snippet:

Code in Text format:

#include <iostream>
using namespace std;

int main()                                     // main starts from here
{
  
    for(int i = 1001; i<9999; i++)               // loop starts from here
    {
        int n = i, sum = 0, m;                   // variables to achieve logic
      
        while(n > 0)                           // inner loop to find sum of digits of i
        {  
            m = n % 10;  
            sum = sum + m;  
            n = n / 10;  
        }
        if(i % sum == 0)                       // if sum is divisible by i
        {
            cout << i << " ";                   // printing i         
        }
    }
    return 0;
}

I hope the above snippets, information, and the explanation will help you out!

Please comment if you have any queries/errors!

Thank you!


Related Solutions

Java Write a program that displays all the numbers from 100 to 200 that are divisible...
Java Write a program that displays all the numbers from 100 to 200 that are divisible by 5 or 6, but not both Make sure all instructions and outputs for the user are explicit
Write an algorithm that print all numbers that are divisible by 3 between 1000 and 2000
Write an algorithm that print all numbers that are divisible by 3 between 1000 and 2000
Write a C++ program that asks the user to enter in three numbers and displays the...
Write a C++ program that asks the user to enter in three numbers and displays the numbers in ascending order. If the three numbers are all the same the program should tell the user that all the numbers are equal and exits the program. Be sure to think about all the possible cases of three numbers. Be sure to test all possible paths. Sample Runs: NOTE: not all possible runs are shown below. Sample Run 1 Welcome to the order...
Write a program in C++ that generates and displays the first N three digit odd numbers....
Write a program in C++ that generates and displays the first N three digit odd numbers. Whereas the number N is provided by the user.
JAVA Write a program to sum the numbers from 1 to 100 that are divisible by...
JAVA Write a program to sum the numbers from 1 to 100 that are divisible by 7, and compute the average of those numbers, print both the sum and the average with appropriate messages to the screen. Run the program. Capture the console output. Put the program code and console output at the end of your text file,
using python 3 2. Write a python program that finds the numbers that are divisible by...
using python 3 2. Write a python program that finds the numbers that are divisible by both 2 and 7 but not 70, or that are divisible by 57 between 1 and 1000. 3. Write a function called YesNo that receives as input each of the numbers between 1 and 1000 and returns True if the number is divisible by both 2 and 7 but not 70, or it is divisible by 57. Otherwise it returns False. 4. In your...
using python 3 2. Write a python program that finds the numbers that are divisible by...
using python 3 2. Write a python program that finds the numbers that are divisible by both 2 and 7 but not 70, or that are divisible by 57 between 1 and 1000. 3. Write a function called YesNo that receives as input each of the numbers between 1 and 1000 and returns True if the number is divisible by both 2 and 7 but not 70, or it is divisible by 57. Otherwise it returns False. 4. In your...
C++ program Overloaded Hospital Write a c++ program that computes and displays the charges for a...
C++ program Overloaded Hospital Write a c++ program that computes and displays the charges for a patient’s hospital stay. First, the program should ask if the patient was admitted as an inpatient or an outpatient. If the patient was an inpatient, the following data should be entered: The number of days spent in the hospital The daily rate Hospital medication charges Charges for hospital services (lab tests, etc.) The program should ask for the following data if the patient was...
who to write a program c++ that reads in an integer between 0 and 1000 and...
who to write a program c++ that reads in an integer between 0 and 1000 and adds all the digits in the integer?
Write a program that generates all prime numbers between 2 and 1000, using the Sieve of...
Write a program that generates all prime numbers between 2 and 1000, using the Sieve of Eratosthenes method. You can find many articles that describe the method for finding primes in this manner on the Internet. Display all the prime values. This program should be in assembly language.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT