Click here to Skip to main content
15,912,837 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Program that finds sum between 100 to 200 which are divisible by 7

Thank You!
Posted
Comments
Samresh.ss 26-Sep-13 2:36am    
Iterate from 100 to 200. Check each number if divisible by 7. If yes add to result.

C#
for (int i = 100; i <= 200; i++)
    if (i % 7 == 0)  sum+= i;
 
Share this answer
 
float sum=0;
for(int i=100;i<200;i++)
{
if(i%7==0)
{
sum=sum+i;
}
}
Console.WriteLine("Final ans = "+sum.toString());
 
Share this answer
 
C#
class Program1
    {
        private int number = 100;
        private int sum = 0;

        public void Generate()
        {
            for (; number < 200; number++)
            {
                //first check if number is divisible by 7
                if (number % 7 == 0)
                {
                    sum = sum + number;
                }
            }

            Console.WriteLine("Total: {0}", sum);
        }
    }
 
Share this answer
 
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Having said that, this isn't difficult - but I'll give you no code: it's your grade at the end of this, so you should do the work! :laugh:

1) Set up a loop (a for loop is best) to run a variable (call it testValue) from 100 to 200 inclusive.
2) Inside the loop, use an if statement to see if testValue is divisible by your divisor - in this case 7, but I would put that in a variable to make it more flexible.
2.1) Hint: look at the Modulus operator '%' - it might be rather useful here!
3) If it is divisible, print the value of textValue using Console.WriteLine
4) If it isn't, do nothing.
5) Go round the loop, incrementing testValue by one each time.

There is a more efficient version which uses the Modulus operator to offset from the initial value (100) to the first value divisible by the divisor (in your case that would be 105 / 7 = 15) and then loops increasing by the divisor each time so it doesn't need a test of any form, and only goes round the loop the exact number of times it has to. When you get the "primitive" version above working, you might want to save it, and see if you can work out how to do the more efficient version - it's got to be worth extra marks! :laugh:
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900