Patrick and Johnny started to play a card game-XI. In this game, Patrick and Johnny have to pick one card each. The game rule is as follows
i) If both card numbers are positive, reverse the digits in two card numbers and display their sum
ii) If one card number is negative, reverse the digits in positive card number and reverse the sign of negative card number, finally display their sum
iii) If both card numbers are negative, reverse the sign the two negative card number and display their sum. They approach you and ask for your help.
Can you please help them?
Write a C program to reverse a given two number as per above game rules and display their sum using while loop.

Input Format:
Input consists of 2 integers.
The first input consists of an integer, which corresponds to the card number picked by Patrick.
The first input consists of an integer, which corresponds to the card number picked by Johnny.

Output Format:
Output consists of a single integer, which corresponds to the sum. The leading 0's are omitted.

[All text in bold corresponds to the input and the rest corresponds to output]

Sample Input and Output 1:
Enter the card number picked by Patrick:
456
Enter the card number picked by Johnny:
32
Sum is 677
 
Sample Input and Output 2:
Enter the card number picked by Patrick:
-56
Enter the card number picked by Johnny:
450
Sum is 110

Sample Input and Output 3:
Enter the card number picked by Patrick:
-56
Enter the card number picked by Johnny:
-789
Sum is 845



Program Code:
#include <stdio.h>
#include<stdlib.h>
int reversDigits(int num)
{
    int rev_num = 0;
    while(num > 0)
    {
        rev_num = rev_num*10 + num%10;
        num = num/10;
    }
    return rev_num;
}

int main()
{
    int num1, num2, sum;
    printf("Enter the card number picked by Patrick:\n");
    scanf("%d", &num1);
    printf("Enter the card number picked by Johnny:\n");
    scanf("%d", &num2);
    if(num1<0)
    num1=abs(num1);
    else
    num1= reversDigits(num1);
    if(num2<0)
    num2=abs(num2);
    else
    num2= reversDigits(num2);
    sum = num1+num2;
    printf("Sum is %d", sum);
    return 0;

}

Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: