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

  1. #1
    Registered User
    Join Date
    Dec 2021
    Posts
    6

    Talking 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. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So what have you managed to do so far?

    Can you for example input two numbers?
    Can you add them?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2021
    Posts
    6

    Lightbulb

    Quote Originally Posted by Salem View Post
    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...
    Last edited by Kennyjai12345; 12-07-2021 at 11:38 PM.

  4. #4
    Registered User
    Join Date
    Dec 2021
    Posts
    6
    But I don't know why when I input 8 and 10, it still output Alice wins. It should be Bob wins.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    And change your scanf to
    Code:
      scanf( "%d %d", &a, &b );

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. beginner plzz help urgent
    By sara101 in forum C Programming
    Replies: 11
    Last Post: 01-14-2007, 10:38 PM
  2. Replies: 14
    Last Post: 12-06-2006, 04:58 PM
  3. General question from a beginner to Windows C++ programming
    By Kontan in forum Windows Programming
    Replies: 1
    Last Post: 09-29-2006, 08:03 PM
  4. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 11:21 AM
  5. Beginner needs help w/ran. nums. in arrays - URGENT!
    By madhouse199 in forum C++ Programming
    Replies: 3
    Last Post: 12-12-2001, 07:12 AM

Tags for this Thread