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 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 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
Write a program in C++ that computes the sum of odd numbers between 1 and 117....
Write a program in C++ that computes the sum of odd numbers between 1 and 117. Execute the program and submit a screen capture of the program and its results.
Write a c++ program that prints the count of all prime numbers between A and B...
Write a c++ program that prints the count of all prime numbers between A and B (inclusive), where A and B are defined as follows: A = Any 5 digit unique number B = A + 1000 Just a recap on prime numbers: A prime number is any number, greater or equal to 2, that is divisible ONLY by 1 and itself. Here are the first 10 prime numbers: 2, 5, 7, 11, 13, 17, 19, 23, and 29. Rules:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT