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,
in java Write a program that reads in ten numbers and displays the number of distinct...
in java Write a program that reads in ten numbers and displays the number of distinct numbers and the distinct numbers separated by exactly one space (i.e., if a number appears multiple times, it is displayed only once). (Hint: Read a number and store it to an array if it is new. If the number is already in the array, ignore it.) After the input, the array contains the distinct numbers. Here is the sample run of the program: Enter...
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.
{ /* Write a program in C++ to find the perfect numbers between 1 and 500....
{ /* Write a program in C++ to find the perfect numbers between 1 and 500. The perfect numbers between 1 to 500 are: 6 28 496*/    int sum = 0;    int i = 1;    int j = 1;    for (i ; i <= 100; i++)    {        for (j; j <= 100; j++)        {            if (j < i)//Line 55            {                  ...
Write a program that counts how many Fibonacci numbers are divisible by 3 and smaller than...
Write a program that counts how many Fibonacci numbers are divisible by 3 and smaller than 1000. The program prints the resulting number. You may only use while loops. Use python language
Write a program that counts how many Fibonacci numbers are divisible by 3 and smaller than...
Write a program that counts how many Fibonacci numbers are divisible by 3 and smaller than 1000. The program prints the resulting number. You may only use while loops. Use python language
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT