In: Computer Science
CODE MUST BE IN C++ (please use for loop) write a program that loops a number from 1 to 10 thousand and keeps updating a count variable (count variable starts at 0 ) according to these rules: n1 = 14 n2 = 54 n3 = 123 if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if none of the above conditions match for the number, increase count by the number.
The program must produce one numeric output (it should be the value of count)
LOOK AT THE CODE AND COMMENTING FOR BETTER UNDERSTANDING.
SCREENSHOT OF THE CODE IS:
The output of above program is:
code for copy:
#include<iostream>
using namespace std;
int main()
{
//declare all the variables
int n,count=0,n1=14,n2=54,n3=123,i;
//for loop ranges from 1 to 10000
for( i=1;i<=10000;i++)
{
// modulo operator used to check whether the number(i) is divisible by n1 or not.if divisible increment count by 1
if (i%n1==0)
{
count+=1;
}
// modulo operator used to check whether the number(i) is divisible by n2 or not.if divisible increment count by 2
if(i%n2==0)
{
count+=2;
}
// modulo operator used to check whether the number(i) is divisible by n3 or not.if divisible increment count by 3
if(i%n3==0)
{
count+=3;
}
// if all the above cases fail then increment the count by number(i)
if(i%n1!=0 && i%n2!=0 && i%n3!=0)
{
count+=i;
}
}
//print the count to output screen
cout<<"The value of count is : "<< count <<endl;
}
I HOPE THIS WILL HELP YOU... IF YOU HAVE ANY DOUBTS REGARDING IT PLEASE LET ME KNOW IN COMMENT SECTION