+ 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++

3rd Dec 2018, 5:17 PM
Saad Saleem
Saad Saleem - avatar
12 Antworten
+ 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; }
4th Dec 2018, 6:29 PM
blACk sh4d0w
blACk sh4d0w - avatar
+ 11
Have you done it by yourself? Show us your try.
3rd Dec 2018, 6:09 PM
blACk sh4d0w
blACk sh4d0w - avatar
+ 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
4th Dec 2018, 4:32 PM
blACk sh4d0w
blACk sh4d0w - avatar
+ 2
It's working very thankful to you
4th Dec 2018, 6:32 PM
Saad Saleem
Saad Saleem - avatar
+ 1
MBZH31 your code is little wrong
4th Dec 2018, 6:44 AM
Saad Saleem
Saad Saleem - avatar
+ 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)
4th Dec 2018, 6:57 AM
MBZH31
MBZH31 - avatar
+ 1
To sum numbers, create a var and add it i (or a in your case)
4th Dec 2018, 6:59 AM
MBZH31
MBZH31 - avatar
+ 1
Thanks for helping
4th Dec 2018, 6:59 AM
Saad Saleem
Saad Saleem - avatar
+ 1
https://code.sololearn.com/cui9rimKzojm/?ref=app Cheack this code it cannot giving me sum
4th Dec 2018, 6:15 PM
Saad Saleem
Saad Saleem - avatar
0
for (i=1; i <=100;i++) if (i%4) cout<< i;
3rd Dec 2018, 5:36 PM
MBZH31
MBZH31 - avatar
0
Ahmad yes i try many times but not find correct answer
4th Dec 2018, 6:31 AM
Saad Saleem
Saad Saleem - avatar
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?
4th Dec 2018, 6:42 AM
Saad Saleem
Saad Saleem - avatar