Output 2: Enter 2 positive integer numbers 1980 1617
Greatest Common Divisor of 1980 and 1617 is 33.
Logic To Find GCD using Repeated Subtraction
Lets assume that num1 = 15 and num2 = 20. Lets calculate GCD for these 2 numbers.
While loop iterates until num1 is equal to num2.
num1
num2
Greater No
Subtract Numbers
Result
15
20
num2 = 20
num2 = num2 – num1
num2: 20 – 15 = 5
15
5
num1 = 15
num1 = num1 – num2
num1: 15- 5 = 10
10
5
num1 = 10
num1 = num1 – num2
num1: 10- 5 = 5
5
5
Both Are Equal
num1 == num2
GCD is 5.
You can see below code in the C program i.e., converting a negative number into positive. Our source code above works only for positive integer numbers without this code. If the user enters negative value we use ternary operator to make it a positive value.
Here we check if num1 is less than 0. If true, then value of num1 is negative. So we multiply the number by -, which gives us a positive number. For example, if num1 is -15, then -(-15) is +15.
That would convert a negative number to positive number.
While Loop While loop iterates until num1 is not equal to num2. Once num1 is equal to num2, control exits while loop. Whatever is present in num1 or num2 is the GCD of 2 positive integer numbers entered by the user.
Inside while loop, we check if num1 is greater than num2. If true, we subtract num2 from num1 and store it back inside variable num1. Else if num2 is greater than num1, then we subtract num1 from num2 and store it back inside num2. We repeat this until num1 is equal to num2.
Lets write a C program to perform subtraction of 2 numbers without using minus symbol or the subtraction operation(-).
In this video tutorial we are using ~(tilde symbol) bitwise complement operator to perform the operation to get to the anticipated result.
Example: If user enters 2 numbers. a = 15 and b = 5. To subtract 10 from 5 we use result = a – b. And the result will be 10. But in this C program we are not using minus(-) operation, but still we must get the same result 10(for above input).
Logic To Subtract Two Numbers without using Minus Operator
If num = 10; result = ~num; will give 1s complement of 10, that is -11. result = ~num+1; will give 2s complement of 10, that is -10.
Now using this logic, we ask the user to enter 2 numbers. If user enters a = 10 and b = 5. We use the below logic to perform subtraction, but without using minus symbol:
a = 10, b = 5; sub = a + ~b + 1; sub = 10 + (-6) + 1; sub = 10 – 6 + 1; sub = 10 – 5; sub = 5;
This would give proper required result.
Source Code: C Program To Subtract Two Numbers without using Minus Operator
printf("Subtraction of %d and %d is %d\n", a, b, sub);
return 0;
}
#include < stdio.h >
int main()
{
int a, b, sub;
printf("Enter 2 numbers\n");
scanf("%d%d", &a, &b);
sub = a+~b+1;
printf("Subtraction of %d and %d is %d\n", a, b, sub);
return 0;
}
Output 1: Enter 2 numbers 4 10 Subtraction of 4 and 10 is -6
Output 2: Enter 2 numbers 5 10 Subtraction of 5 and 10 is -5
Output 3: Enter 2 numbers 10 5 Subtraction of 10 and 5 is 5
Output 4: Enter 2 numbers 5 5 Subtraction of 5 and 5 is 0
Output 5: Enter 2 numbers 5 -5 Subtraction of 5 and -5 is 10
Video Tutorial: C Program To Subtract Two Numbers without using Minus Operator
Note: These kind of tricky C programming questions can be asked in your viva or company interviews or competitive exams or even in your academic exams. So be prepared.
c += a; is equal to writing c = c + a; c -= a; is equal to writing c = c – a; c *= a; is equal to writing c = c * a; c /= a; is equal to writing c = c / a; c %= a; is equal to writing c = c % a;
In this video tutorial we shall perform Addition, Subtraction, Multiplication and Division of numbers based on user input, using switch case statement(decision control statement).
We had written same calculator program using else-if clause. Same program has been modified to use Switch case in this program. Simple Calculator Application In C
We ask the user to input his / her choice for arithmetic operation. If the user selects 1, then we ask the user to enter 2 integer numbers to perform addition operation. Once the user enters 2 integer numbers we add and display the result on the screen.
If the user enters wrong choice, we ask the user to enter proper choice.
#include < stdio.h >
int main()
{
int a, b;
char choice;
printf("Enter your choice\n");
printf("a. Addition\nb. Subtraction\nc. Multiplication\nd. Division\n");
scanf("%c", &choice);
printf("Enter 2 integer numbers\n");
scanf("%d %d", &a, &b);
switch(choice)
{
case 'a': printf("%d + %d = %d\n", a, b, (a+b));
break;
case 'b': printf("%d - %d = %d\n", a, b, (a-b));
break;
case 'c': printf("%d x %d = %d\n", a, b, (a*b));
break;
case 'd': if( b != 0)
printf("%d / %d = %d\n", a, b, (a/b));
else
printf("Number can't be divided by 0\n");
break;
default: printf("You entered wrong choice\n");
break;
}
return 0;
}
Output Enter your choice a. Addition b. Subtraction c. Multiplication d. Division c Enter 2 integer numbers 5 10 5 x 10 = 50
In above program we are asking user to enter character a or b or c or d to perform addition, subtraction, multiplication and division operations respectively.
printf("Enter 2 integer numbers. Format: a + b\n");
scanf("%d %c %d", &a, &choice, &b);
switch(choice)
{
case'+': printf("%d + %d = %d\n", a, b, (a+b));
break;
case'-': printf("%d - %d = %d\n", a, b, (a-b));
break;
case'*': printf("%d x %d = %d\n", a, b, (a*b));
break;
case'/': if( b != 0)
printf("%d / %d = %d\n", a, b, (a/b));
else
printf("Number can't be divided by 0\n");
break;
default: printf("You entered wrong choice\n");
break;
}
return 0;
}
</stdio.h>
#include
int main()
{
int a, b;
char choice;
printf("Enter your choice\n");
printf("1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n");
scanf("%c", &choice);
printf("Enter 2 integer numbers. Format: a + b\n");
scanf("%d %c %d", &a, &choice, &b);
switch(choice)
{
case '+': printf("%d + %d = %d\n", a, b, (a+b));
break;
case '-': printf("%d - %d = %d\n", a, b, (a-b));
break;
case '*': printf("%d x %d = %d\n", a, b, (a*b));
break;
case '/': if( b != 0)
printf("%d / %d = %d\n", a, b, (a/b));
else
printf("Number can't be divided by 0\n");
break;
default: printf("You entered wrong choice\n");
break;
}
return 0;
}
Output: Enter your choice 1. Addition 2. Subtraction 3. Multiplication 4. Division 1 Enter 2 integer numbers. Format: a + b 50 + 60 50 + 60 = 110
Here we ask the user to enter values as well as the operation to be performed. Operands and operator.
Note 1: There is no need of curly braces inside case. Note 2: If a case doesn’t end with break statement, then the execution continues and the block of code present inside next case will also get executed. Note 3: default case is optional. And the code inside it executes only when non of the cases match. Note 4: Here switch, case, break, default are all keywords / reserve words.
We ask the user to input his / her choice for arithmetic operation. If the user selects 1, then we ask the user to enter 2 integer numbers to perform addition operation. Once the user enters 2 integer numbers we add and display the result on the screen.
If the user enters wrong choice, we ask the user to enter proper choice.
printf("Addition of %d and %d is %d\n", a, b, (a+b));
}
elseif(choice == 2)
{
printf("Subtraction of %d and %d is %d\n", a, b, (a-b));
}
elseif(choice == 3)
{
printf("Multiplication of %d and %d is %d\n", a, b, (a*b));
}
elseif(choice == 4)
{
if(b != 0)
printf("Division of %d and %d is %d\n", a, b, (a/b));
else
printf("Number can not be divided by 0\n");
}
else
{
printf("Please enter any of these no 1, 2, 3, 4\n");
}
return 0;
}
#include < stdio.h >
int main()
{
int a, b, choice;
printf("Enter your choice\n");
printf("1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n\n");
scanf("%d", &choice);
if(choice > 4)
{
printf("You entered wrong choice\n");
}
else
{
printf("Enter 2 integer numbers\n");
scanf("%d %d", &a, &b);
}
if(choice == 1)
{
printf("Addition of %d and %d is %d\n", a, b, (a+b));
}
else if(choice == 2)
{
printf("Subtraction of %d and %d is %d\n", a, b, (a-b));
}
else if(choice == 3)
{
printf("Multiplication of %d and %d is %d\n", a, b, (a*b));
}
else if(choice == 4)
{
if(b != 0)
printf("Division of %d and %d is %d\n", a, b, (a/b));
else
printf("Number can not be divided by 0\n");
}
else
{
printf("Please enter any of these no 1, 2, 3, 4\n");
}
return 0;
}
Output 1: Enter your choice 1. Addition 2. Subtraction 3. Multiplication 4. Division
1 Enter 2 integer numbers 3 2 Addition of 3 and 2 is 5
Output 2: Enter your choice 1. Addition 2. Subtraction 3. Multiplication 4. Division
2 Enter 2 integer numbers 8 3 Subtraction of 8 and 3 is 5
Output 3: Enter your choice 1. Addition 2. Subtraction 3. Multiplication 4. Division
3 Enter 2 integer numbers 5 5 Multiplication of 5 and 5 is 25
Output 4: Enter your choice 1. Addition 2. Subtraction 3. Multiplication 4. Division
4 Enter 2 integer numbers 10 2 Division of 10 and 2 is 5
Output 5: Enter your choice 1. Addition 2. Subtraction 3. Multiplication 4. Division
4 Enter 2 integer numbers 0 12 Division of 0 and 12 is 0
Output 6: Enter your choice 1. Addition 2. Subtraction 3. Multiplication 4. Division
4 Enter 2 integer numbers 10 0 Number can not be divided by 0
Output 7: Enter your choice 1. Addition 2. Subtraction 3. Multiplication 4. Division
50 You entered wrong choice Please enter any of these no 1, 2, 3, 4
Note: In division section(that is, choice 4) you can see nesting of if else statement. You can know more about nested if-else statements in this program: Nested if else Statement In C
Note: We can write the same program within while loop and ask the user if he wants to continue working inside calculator application. This avoids the need to exit and re-run the program. We’ll publish the code and video tutorial for the same in coming days. Stay subscribed to our YouTube Channel and blog.