Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

About closing the loop, I have some issues

+10 votes
asked Mar 21 by Đorđe Ćorić (250 points)
#include <stdio.h>
#include <math.h>
int main()
{
    int sum=0, i;
for (i=1; i<=100; i++)
    {
    if (i%6==0)

        printf("\nNumber %d can be divided with 6 ", i); /*when only here nothing works as required*/
        {
        printf("\nNumber %d can be divided with 6 ", i); /*when only here it works fine*/
        sum+=i;
        }

    }
printf("\nSum of all numbers which can be divided with 6 is: %d", sum);
return 0;
}

1 Answer

+3 votes
answered Apr 2 by Peter Minarik (86,240 points)
selected Apr 10 by Admin
 
Best answer

The curly braces ({ and }) define the scope. So

if (i % 6==0)
{
    printf("\nNumber %d can be divided with 6 ", i);
    sum += i;
}

is the right code, as it says, when i can be divided by 6 without any remainders then we want to print and increase the sum. Without the curly braces, only the printing happens under this condition and the summation happens even if the condition is false (as it would be outside of the scope of the if statement).

I hope this helps.

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...