# Thread: C programming Beginner Question (More Helpful if provide me with the code)

1. ## C programming Beginner Question (More Helpful if provide me with the code)

Problem : Who is the winner? (Level 1)

• Problem description
Alice and Bob are playing a game. Both of them write down an integer number. If the sum oftwo integers is a square of an integer, Alice wins the game. If the reversed number of the sumis a square of an integer, Bob wins. If neither of them wins, or both of them win, it is a tie. Forexample, Alice wrote an 8, Bob wrote a 10. 18 is not a square of any integer. But 81 is a squareof 9. So Bob is the winner.
Write two functions int is_square(int num) and int reverse(int num) to check whether num is asquare number or not and reverse num, respectively. Then the program should call bothfunctions and output the winner information.
• Input & output requirements
Input two positive integer numbers. The output should follow the format as shown in sample
results.
• Sample resultsSample 1
8 10Bobs winsSample 22 2
Tie

2. So what have you managed to do so far?

Can you for example input two numbers?
Can you add them?

3. Originally Posted by Salem
So what have you managed to do so far?

Can you for example input two numbers?
Can you add them?
insert
Code:
```#include<stdio.h>
int main(){
int a,b,sum,rev=0,remainder,i;
printf("Please enter the two integers:");
scanf("%d%d",&a,&b);

sum=a+b;
while(sum!=0){
remainder=sum%10;
rev=rev*10+remainder;
sum/=10;
}

for(i=0;i<=sum;i++){
if(sum==i*i){
printf("Alice wins");
}
else if(rev==i*i)
{printf("Bob wins");
}
return 0;
}
}```
I am struggling here...

4. But I don't know why when I input 8 and 10, it still output Alice wins. It should be Bob wins.

5. A couple of points.

1. You need to move bits of code into functions.
> "Write two functions int is_square(int num) and int reverse(int num)"

2. Indentation matters - yes, really!
Code:
```#include<stdio.h>
int main()
{
int a, b, sum, rev = 0, remainder, i;
printf("Please enter the two integers:");
scanf("%d%d", &a, &b);

sum = a + b;
while (sum != 0) {
remainder = sum % 10;
rev = rev * 10 + remainder;
sum /= 10;
}

for (i = 0; i <= sum; i++) {
if (sum == i * i) {
printf("Alice wins");
} else if (rev == i * i) {
printf("Bob wins");
}
return 0;
}
}```
So for example
- you mess up the sum value, trying to compute the rev value.
- your return 0; is inside the for loop, so your loop runs only once anyway.
- Alice always wins, because you managed to make sum = 0 trying to compute rev.

6. And change your scanf to
Code:
`  scanf( "%d %d", &a, &b );`

Popular pages Recent additions