Question

In: Computer Science

Dam and peter both like to count things. Adam likes yo count numbers devisible by 7....

Dam and peter both like to count things. Adam likes yo count numbers devisible by 7. Peter likes to count numbers that add to 7 (e.g. 7, 16, 25 etc). Output all the bunbers that both adam and peter like tk count between 1 and 1000

c++

Solutions

Expert Solution

Required program in C++ -->

#include<iostream>
using namespace std;
int main(){
int count1=0,count2=0; //declare variables count1 and count2 with 0
cout<<"Numbers divisible by 7: "; //print numbers divisible by 7 :
for(int i=1; i<=1000; i++){ //for loop to count numbers divisible by 7 from 1 to 1000
if(i%7==0){ //if i is perfectly divisible by 7
cout<<i<<", "; //then print i
count1++; //increase count1 by 1
}
}
cout<<endl; //change the line
cout<<"Total count of numbers divisible by 7 are: "<<count1<<endl; //print value of count1
cout<<"Numbers with total of 7 are: "; //print numbers with total of 7 are
for(int j=1;j<=1000;j++){ //for loop to find the numbers whose sum is 7 from 1 to 1000
int sum=0,m; //declare variables in integer data type
int n=j; //assign the value of j to n
while(n>0){ //while loop will run till n is greater than 0
m=n%10; //remainder when n is divided by 10 is stored in m
sum=sum+m; // m is then added in sum
n=n/10; // n is then divided by 10, and its integer value is stored in n
}
if(sum==7){ //if sum value is equal to 7
cout<<j<<", "; //print value of j
count2++; //increase count2 by 1
}
}
cout<<endl; //change the line
cout<<"Total count of numbers whose sum is 7 are: "<<count2<<endl; //print the value of count2
}


Related Solutions

Peter and John are great friends. They like to play games with numbers. This time, Peter...
Peter and John are great friends. They like to play games with numbers. This time, Peter has given John a list of numbers and given him the task of determining if it is possible to choose a subset of them such that they sum is equal to another given number. Build an algorithm in Python using brute force to help John with his problem. INPUT The first line corresponds to N, the amount of numbers given by Peter The next...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT