# Write a program to take two integers from user, subtract the smaller number from the greater and print the result.

Please anyone explain me the theory and how can i do this program

5/4/2021 5:43:14 PM

Numan Sayyed14 Answers

New AnswerNuman Sayyed It's very simple. Just do this int a, b = 0; scanf("%d", &a); scanf("%d", &b); int c = a >= b ? a - b : b - a; printf("%d", c);

Daniel Briceño Shorter and readable code comes from practice. When you do code then just don't think one way, think in many ways to do same code in different different ways. I am doing Coding since 6 years so I have experience on it but still trying to write better code.

It's easy There are two variables named a and b. #include <math.h> #include <stdlib.h> c = fabs(a-b); this evaluate the deduction of smaller value from bigger value.

A 3-liner: int a, b; scanf("%d%d", &a, &b); printf("%d", (a-b)*(2*(b<a)-1)) // Hope this helps

You could do it this way. It doesn't technically subtract the smaller number from the greater number, but it does give the same result. #include <stdio.h> int main() { int a,b;//initializing two integers int c;//result scanf("%d %d",&a,&b);//user input c=a-b; if(c<0) { //subtracting the smaller number from the greater means that the result would always be positive // a-b=-(b-a) ,if c is negative means the lager number got subtracted from the smaller one c=-c; //just take away the - sign to get the result.(absolute value). } printf("The result is: %d\n",c); return 0; }

Daniel Briceño You have also did the same but you have to do (b - a) instead of - (a - b) And also just check a >= b not (a - b) <= 0

First declare two variables and then take input from the user.Then check for greater number among the two.Then substract the smaller number from the larger.

int a,b; scanf ("%d %d",&a,&b); if(a>b){ printf("%d",a-b); } else{ printf("%d",b-a); }

If the first number is greater than the second then: int number_1=2; int number_2=1; printf("The result is: %d",number_1-number_2); // 2 - // 1 //---- // 1. If the second number is greater than the first then: int number_1=1; int number_2=2; //1<2: or number_1<number_2: printf("The result is: %d", number_2-number_1); // 2 - // 1 //---- // 1 For this exercise I recommend using an if and an else: if (number_1>number_2){ .....//You codes if number_1 is Greater than number_2 }else{ .....//You codes if number_2 is Greater than number_2 } But there is also a case that the user enters two equal numbers: if (number_1==number_2){ //You codes } For this case I recommend using the or operator: if (number_1>number_2 || number_1==number_2){ //You codes if number_1 is Greater number_2 or number_1 is equal to number_2 }else{ //You codes if number_1 is less number_2 }

Mihail But you can summarize it to one line: c=(a-b<0)?-(a-b):a-b; var= (var logic var)? if true : if else; Update: A bug (a-b==0)? X / A correct (a-b<0)? \/

🅰🅹 🅐🅝🅐🅝🅣 What amazes me is the imagination of people what do make the code shorter and more readable than it was. If it were up to me, the code would look like a spaghetti: if () else if else