+ 1
Write a program to print numbers from 1 to 100 in which multiples of 4 not included (1,2,3,5,6,7,9,...) and then find their sum?
Use C++
12 odpowiedzi
+ 11
#include <iostream>
using namespace std;
int main()
{
int sum = 0;
for(int a = 0 ; a <= 100; a++)
if(a % 4 != 0)
{
cout << "The number is " << a << endl ;
sum = sum + a;
}
cout << "Sum is: " << sum << endl;
return 0;
}
+ 11
Have you done it by yourself?
Show us your try.
+ 9
To sum all these numbers initialize a variable to zero and put the sum statement inside the if condition
int sum = 0 ;
sum += a; //Put it inside if statement
+ 2
It's working very thankful to you
+ 1
MBZH31 your code is little wrong
+ 1
My code isn't the final result. It's just to help you to the final result
I didn't code the sum
Note :
if (i%4) is the same code if (i%4 !=0)
+ 1
To sum numbers, create a var and add it i (or a in your case)
+ 1
Thanks for helping
+ 1
https://code.sololearn.com/cui9rimKzojm/?ref=app
Cheack this code it cannot giving me sum
0
for (i=1; i <=100;i++)
if (i%4)
cout<< i;
0
Ahmad yes i try many times but not find correct answer
0
Finally I find a code
#include <iostream>
using namespace std;
int main() {
for(int a = 0 ; a<=100 ;a++)
if(a%4 != 0)
cout << a;
return 0;
}
But how to add these numbers?