C Program To Find GCD using Repeated Subtraction

Lets write a C program to find Greatest Common Divisor of two positive integer numbers using repeated subtraction.

Related Read:
C Program to Find GCD or HCF of Two Numbers
C Program To Find GCD and LCM of Two Numbers using Euclidean algorithm
C Program To Find GCD using Pointers and Functions

Video Tutorial: C Program To Find GCD using Repeated Subtraction


[youtube https://www.youtube.com/watch?v=sv8Jehsydrs]

YouTube Link: https://www.youtube.com/watch?v=sv8Jehsydrs [Watch the Video In Full Screen.]


Source Code: C Program To Find GCD using Repeated Subtraction

#include<stdio.h>

int main()
{
    int num1, num2;

    printf("Enter 2 positive integer numbers\n");
    scanf("%d%d", &num1, &num2);

    num1 = (num1 < 0) ? -num1 : num1;
    num2 = (num2 < 0) ? -num2 : num2;

    printf("\nGreatest Common Divisor of %d and %d is ", num1, num2);

    while(num1 != num2)
    {
        if(num1 > num2)
        {
            num1 = num1 - num2;
        }
        else
        {
            num2 = num2 - num1;
        }
    }

    printf("%d.\n", num1);

    return 0;
}

Output 1:
Enter 2 positive integer numbers
20
30

Greatest Common Divisor of 20 and 30 is 10.

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.

num1num2Greater NoSubtract NumbersResult
1520num2 = 20num2 =
num2 – num1
num2:
20 – 15 = 5
155num1 = 15num1 =
num1 – num2
num1:
15- 5 = 10
105num1 = 10num1 =
num1 – num2
num1:
10- 5 = 5
55Both Are Equalnum1 == num2GCD 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.

    num1 = (num1 < 0) ? -num1 : num1;  
    num2 = (num2 < 0) ? -num2 : num2;  

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.

Ternary Operator / Conditional Operator In C

You can make use of simple if else statement like below, to convert negative number into positive:

#include<stdio.h>
if(num1 < 0)
   num1 = num1 * -1;

if(num2 < 0)
   num2 = num2 * -1;

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.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Subtract Two Numbers without using Minus Operator

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

#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


[youtube https://www.youtube.com/watch?v=YG1bDNtO5X8]

YouTube Link: https://www.youtube.com/watch?v=YG1bDNtO5X8 [Watch the Video In Full Screen.]

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.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Assignment Operators in C

In this video tutorial we show Compound Assignment Operators in C programming language.

Related Read:
Simple Calculator Application In C

Assignment Operators

If a and c are the operands.

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;

Assignment Operator +=

 
#include < stdio.h >

int main()
{
    int a = 20, c = 30;

    c += a ; // c = c + a;

    printf("%d\n\n", c);

    return 0;
}

Output:
50

Assignment Operator -=

 
#include < stdio.h >

int main()
{
    int a = 20, c = 30;

    c -= a ; // c = c - a;

    printf("%d\n\n", c);

    return 0;
}

Output:
10

Assignment Operator *=

 
#include < stdio.h >

int main()
{
    int a = 20, c = 30;

    c *= a ; // c = c * a;

    printf("%d\n\n", c);

    return 0;
}

Output:
600

Assignment Operator /=

 
#include < stdio.h >

int main()
{
    int a = 2, c = 20;

    c /= a ; // c = c / a;

    printf("%d\n\n", c);

    return 0;
}

Output:
10

Assignment Operator %=

 
#include < stdio.h >

int main()
{
    int a = 2, c = 20;

    c %= a ; // c = c % a;

    printf("%d\n\n", c);

    return 0;
}

Output:
0

Compound Assignment Operators in C Programming Language


[youtube https://www.youtube.com/watch?v=-_ZBdT2Yxqo]

YouTube Link: https://www.youtube.com/watch?v=-_ZBdT2Yxqo [Watch the Video In Full Screen.]


You can also learn about Logical and Relational Operators in C:
Relational Operators In C
Logical Operators In C

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Simple Calculator Program using Switch Case: C

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).

Related Read:
Basic Arithmetic Operations In C
Addition of 2 Numbers: C
Subtraction of 2 Numbers: C
Multiplication of 2 Numbers: C
Division of 2 Numbers: C
else if statement in C

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

Simple Calculator using Switch Case: C Program


[youtube https://www.youtube.com/watch?v=HHuaYKKoMR4]

YouTube Link: https://www.youtube.com/watch?v=HHuaYKKoMR4 [Watch the Video In Full Screen.]


Working of Calculator Program
We display list of operation one can perform. That is,

1. Addition
2. Subtraction
3. Multiplication
4. Division

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.

Calculator Program using Switch Case: C Program

#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.

#include < stdio.h >

int main()
{
    int a, b, choice;

    printf("Enter your choice\n");
    printf("1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n");
    scanf("%d", &choice);

    if( choice > 4 )
    {
        printf("Select with in the range!\n");
    }
    else
    {
        printf("Enter 2 integer numbers\n");
        scanf("%d %d", &a, &b);
    }


    switch(choice)
    {
        case 1: printf("%d + %d = %d\n", a, b, (a+b));
                break;

        case 2: printf("%d - %d = %d\n", a, b, (a-b));
                break;

        case 3: printf("%d x %d = %d\n", a, b, (a*b));
                break;

        case 4: 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
4
Enter 2 integer numbers
10
2
10 / 2 = 5

Here we are asking user to enter 1 or 2 or 3 or 4 to choose the arithmetic operation.

#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.

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Simple Calculator Application In C

In this video tutorial we shall perform Addition, Subtraction, Multiplication and Division of numbers based on user input.

Related Read:
Basic Arithmetic Operations In C
Addition of 2 Numbers: C
Subtraction of 2 Numbers: C
Multiplication of 2 Numbers: C
Division of 2 Numbers: C
else if statement in C

Simple Calculator Program In C


[youtube https://www.youtube.com/watch?v=QO6pCvM3xSQ]

YouTube Link: https://www.youtube.com/watch?v=QO6pCvM3xSQ [Watch the Video In Full Screen.]


Working of Calculator Program
We display list of operation one can perform. That is,

1. Addition
2. Subtraction
3. Multiplication
4. Division

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.

Student Grade Calculation using else-if clause

 
#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.

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert