0

What is wrong wirh this code .

#include <stdio.h> void main() { int z , x , y , a = 0 , b = 0 ; for ( x = 1 ; x <= 49 ; x++ ) { for ( y = 0 ; y < x ; y++ ) { a = a + y ; } for ( z = x + 1 ; z <= 49 ; z++) { b = b + z ; } if ( a == b ) { printf ( "%d" , x ) ; break ; } } return 0; } ------------------------------------------------- This code should calculate the sum of numbers before and after any number between 1 - 49 .. if the sum of the numbers before equal to the numbers after .. print that number .

17th Oct 2019, 8:52 AM
Let Name = Abdulrahman;
7 Respuestas
+ 2
Try this #include <stdio.h> void main() { int z , x , y , a = 0 , b = 0 ; int n=49; for ( x = 1 ; x <= n ; x++ ) { a=0; for ( y = 0 ; y < x ; y++ ) { a = a + y ; } b=0; for ( z = x + 1 ; z <=n ; z++) { b = b + z ; } if ( a == b ) { printf ( "%d" , x ) ; break ; } } }
18th Oct 2019, 2:12 AM
Sai Ram
+ 7
Firstly you should change return type "void" to "int", because the function will return an integer, 0 in "return 0;".
17th Oct 2019, 9:33 AM
Seb TheS
Seb TheS - avatar
+ 4
Re-initialize value of <a> and <b> to zero on each iteration of the <x> for-loop: for (x = 1; x <= 49; x++) { a = 0; b = 0; // start over for (y = 0; y < x; y++) You didn't get a match because value of <a> and <b> isn't starting by zero when you go and check the next number in sequence.
17th Oct 2019, 12:25 PM
Ipang
+ 1
I did not get what you wanted to achieve with it. Can you explain better or give some examples?
17th Oct 2019, 11:53 AM
Seb TheS
Seb TheS - avatar
+ 1
Sorry my english is weak .. I ment for example if the summation of the numbers 1,2,3,4 which is before "5" is equal to the summation of the numbers 6,7,8 which is after "5" But here it's between 1 and 49
17th Oct 2019, 12:08 PM
Let Name = Abdulrahman;
0
Do you mean if sum of 1, 2, 3 and 1, 2, 3, 4 are equal, print their sum?
17th Oct 2019, 11:52 AM
Seb TheS
Seb TheS - avatar
0
Sai Ram it's working .. thanks ❤
18th Oct 2019, 9:16 AM
Let Name = Abdulrahman;