Addition of 2 Numbers: C

In this video tutorial you can learn the procedure followed in C programming to add two numbers.

Related Read:

 
#include < stdio.h >

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

    printf("Enter value of a and b\n");
    scanf("%d%d", &a, &b);

    c = a + b;
    
    printf("Addition of %d and %d is %d\n", a, b, c);

    return 0;
}

Output:
Enter value of a and b
30
20
Addition of 30 and 20 is 50

You can write same program without using third variable to calculate addition of 2 numbers, as below:

 
#include < stdio.h >

int main()
{
    int a, b;

    printf("Enter value of a and b\n");
    scanf("%d%d", &a, &b);

    printf("Addition of %d and %d is %d\n", a, b, (a+b));

    return 0;
}

Output:
Enter value of a and b
30
20
Addition of 30 and 20 is 50

Note: Instead of int you can take float variables too. That would help in taking both integer as well as real values from the user as input.

Scanf(): For user input

In above c program we are asking user to enter the values for variable a and b. You can know more about scanf() method/function in this video tutorial: Using Scanf in C Program

Addition of Two Numbers: C Programming


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

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


This video was produced as building block for our simple calculator application.

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

C Program to Calculate the Compound Interest

In this video tutorial lets learn how we can calculate Compound Interest by making use of simple arithmetic operations.

 
#include < stdio.h >
#include < math.h >

int main()
{
    int p, t;
    float r, ci;

    printf("Enter principal amount\n");
    scanf("%d", &p);

    printf("Enter rate of interest\n");
    scanf("%f", &r);

    printf("Enter time period\n");
    scanf("%d", &t);


    ci = p * pow( (1 + r / 100), t );

    printf("Compound Interest is %f\n", ci);

    return 0;
}

Output:
Enter principal amount
1000
Enter rate of interest
8.5
Enter time period
3
Compound Interest is 1277.289185

Note: Since we’re using pow() method, we need to include math.h library file.

Scanf(): For user input

In above c program we are asking user to enter the values for variable a and b. You can know more about scanf() method/function in this video tutorial: Using Scanf in C Program

C Program to Calculate the Compound Interest


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

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


In this program we take input for Principal amount, rate of interest and time period from the user, and then calculate Compound Interest for those values.

Compound Interest Logic




We make use of arithmetic operations available in C programming language and convert this formula to calculate Compound Interest.

Compound_Interest = Principal_amount * pow( (1 + r / 100), t );

This gives us Compound Interest and we output the result to the console.

Formula for calculating Compound Interest

p – Principal amount.
r – Rate of interest.
t – Time period.
ci – Compound Interest.

Yearly Compound Interest Formula
Compound_Interest = Principal_amount * pow( (1 + r / 100), t );

Half-Yearly Compound Interest Formula
Compound_Interest = Principal_amount * pow( (1 + (r/2) / 100), 2 * t );

Quarterly Compound Interest Formula
Compound_Interest = Principal_amount * pow( (1 + (r/4) / 100), 4 * t );

Monthly Compound Interest Formula
Compound_Interest = Principal_amount * pow( (1 + (r/12) / 100), 12 * t );

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

C Program to Calculate the Simple Interest

In this video tutorial lets learn how we can calculate Simple Interest by making use of simple arithmetic operations.

 
#include<stdio.h>

int main()
{
    int p, t;
    float r, si;

    printf("Enter principal amount\n");
    scanf("%d", &p);

    printf("Enter Rate of interest\n");
    scanf("%f", &r);

    printf("Enter time period\n");
    scanf("%d", &t);

    si = (p * t * r) / 100.0;

    printf("Simple Interest is %f\n", si);

    return 0;
}

Output:
Enter principal amount
1000
Enter Rate of interest
14
Enter time period
2
Simple Interest is 280.000000

Scanf(): For user input

In above c program we are asking user to enter the values for variable a and b. You can know more about scanf() method/function in this video tutorial: Using Scanf in C Program

C Program to Calculate the Simple Interest


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

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


In this program we take input for Principal amount, rate of interest and time period from the user, and then calculate Simple Interest for those values.

Simple Interest Logic




We make use of arithmetic operations available in C programming language and convert this formula to calculate Simple Interest.

Simple_Interest = ( Principal_amount * Rate_of_interest * Time) / 100.0;

This gives us Simple Interest and we output the result to the console.

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

Calculate Area of a Circle using math.h library: C

We can calculate area of circle if we know the value of its radius. So using that, we shall write the logic in c program to calculate the area of a circle, and we will us math.h library functions in this program.

Related Read:
Basic Arithmetic Operations In C

 
#include < stdio.h >
#include < math.h >

int main()
{
    float radius;

    printf("Enter radius of circle\n");
    scanf("%f", &radius);

    printf("Area of circle is %f \n", (M_PI * pow(radius, 2)));

    return 0;
}

Output:
Enter radius of circle
2.0
Area of circle is 12.566371

Scanf(): For user input

In above c program we are asking user to enter the values for variable a and b. You can know more about scanf() method/function in this video tutorial: Using Scanf in C Program

Calculate Area of a Circle using math.h library: C


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

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


math.h library file has a constant M_PI which has the value of PI. Since it’s a constant variable we can not change its value in our program. We’re also using pow() method/function present in math.h header file.

Formula for calculating Area of a circle


area of circle

Formula for calculating Area of a circle is PI * radius * radius. We take value of radius from user and using above formula calculate area of the circle and output result to the console window. Since we’re using math.h library file we use M_PI and pow() methods for the expression.

i.e., area = M_PI * pow(radius, 2);

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

Swap 2 numbers using Multiplication and Division: C

In this video tutorial we shall learn how to swap two integer numbers without using a temporary variable and by simply making use of multiplication and division.

Related Read:
Basic Arithmetic Operations In C
Swap 2 Numbers Using a Temporary Variable: C
Swap 2 Numbers Without Using a Temporary Variable: C
Swap 2 numbers using Addition and Subtraction: C

 
#include < stdio.h >

int main()
{
    int a, b;

    printf("Enter 2 integer numbers\n");
    scanf("%d %d", &a, &b);

    printf("You entered a = %d and b = %d\n", a, b);

    a = a * b;
    b = a / b;
    a = a / b;

    printf("After swapping a = %d and b = %d\n", a ,b);

    return 0;
}

Output:
Enter 2 integer numbers
3
5
You entered a = 3 and b = 5
After swapping a = 5 and b = 3

Scanf(): For user input

In above c program we are asking user to enter the values for variable a and b. You can know more about scanf() method/function in this video tutorial: Using Scanf in C Program

Swap 2 numbers using only Multiplication and Division: C


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

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


Swapping 2 Numbers In C using only Multiplication and Division: Logic

In our above program we are asking user to enter integer value for a and b.
If the user enters 10 and 5 for a and b respectively. Then our program executes below logic to swap the values of variable a and b.

Step 1: Multiply values of a and b and store it in variable a.
i.e.,
a = a * b;
50= 10 * 5;
So a = 50;

Step 2: Now divide value of b from value of a and store it in variable b.
i.e.,
b = a / b;
10= 50 / 5; (value of a = 50 according to Step 1)
So value of variable b is now 10.

Step 3: Now divide value of b from value of a and store it in variable a.
i.e.,
a = a / b;
5= 50 / 10; (Value of a = 50 from Step 1, and Value of b = 10 from Step 2)
Now the value of a = 5;

Finally value of a = 5(from step 3) and value of b = 10(from step 2).

This is how we swap the value of variable a and b by just making use of multiplication and division in C programming language.

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