In: Computer Science
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.
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!