Reputation: 3
How to write a program that calculates the multiples of 7 between 21 and 210 inclusive? This is an incorrect example:
finding multiples of 7
#include<stdio.h>
int main(){
int multiple;
int sum;
int i=0;
int j=0;
int sum1,sum2;
for (i=1;i>=21;i++){
printf(i);
sum1=i+sum1;
for(j=1;j<=210;j++){
printf(j);
sum2=j+sum2;
}
}
sum=sum1+sum2;
printf("sum",sum);
}
Upvotes: 0
Views: 6333
Reputation: 70
Define lower and upper limit as n1
and n2
with 21 and 210 respectively.
#include <iostream>
#include <stdio.h>
int main()
{
int n1,n2;
n1=21;
n2=210;
while(n1<=n2)
{
if(n1%7==0)
printf("%d ",n1);
n1=n1+1;
}
}
Tested code on Code Chef - https://www.codechef.com/ide
Output:
21 28 35 42 49 56 63 70 77 84 91 98 105 112 119 126 133 140 147 154 161 168 175 182 189 196 203 210
Upvotes: 2
Reputation: 493
Try this:
#include<stdio.h>
int main(void)
{
int i, j, sum = 0;
//set your lower bound and upper bound
for (i = 21 ; i <= 210 ; i++)
{
if ( (i % 7) == 0 )
{
sum += i;
}
}
printf("total sum = %d \n",sum);
}
Upvotes: 0