C Program to Generate Even Numbers Between Two Integers

C program to generate even numbers between 2 integer values input by the user.

Related Read:
Even or Odd Number: C Program
Even or Odd Number without using Modular Division: C Program

Note 1: An even number is an integer that is exactly divisible by 2.

Note 2: Even numbers are of the form 2 * n;

Note 3: Modular division( % ) returns remainder of division. For example, 10 / 2 = 5. But 10 % 2 = 0.

C Program to Generate Even Numbers Between Two Integers

 
#include<stdio.h>

int main()
{
    int count, limit;

    printf("Enter start value and end value to generate Even no's\n");
    scanf("%d%d", &count, &limit);

    printf("\nEven numbers between %d and %d are:\n", count, limit);

    while(count <= limit)
    {
        if(count % 2 == 0)
        {
            printf("%d\n", count);
        }
        count++;
    }

    return 0;
}

Output 1
Enter start value and end value to generate Even no’s
10
30

Even numbers between 10 and 30 are:
10
12
14
16
18
20
22
24
26
28
30

Output 2
Enter start value and end value to generate Even no’s
1
10

Even numbers between 1 and 10 are:
2
4
6
8
10

C Program to Generate Even Numbers Between Two Integers


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

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


In above c program, we ask the user to input 2 integer value and store it in variables count and limit. While loop keeps executing until the start value i.e., count is less than or equal to the end value i.e., limit.

Inside while loop, for every value of count, we check if its perfectly divisible by 2. If true, it’s a Even number and we output that number to the console window. On every iteration of the loop we increment the value of count by 1.

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

Update:
In the above video I’ve said: “count should be exactly divisible by 0”.

I’m sorry about that. It should be – “Value inside variable count should be exactly divisible by 2.”

Find Area of a Triangle Using Its Base and Height: C Program

Find the area of the Triangle when it’s base and height are input by the user.

Related Read:
Find Area of a Triangle Using Its Sides: C Program

Formula To Calculate Area of Triangle when its Base and Height are given

Area = (Base * Height) / 2.0;

Note: If we divide an expression or number by 2, it’ll return only the integer part and the decimal part will be discarded. So we are dividing the expression by 2.0 (which is of type double).

Find Area of a Triangle Using Its Base and Height: C Program


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

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


Find Area of a Triangle Using Its Base and Height: C Program

#include < stdio.h >

int main()
{
    float base, height, area;

    printf("Enter length of base of Triangle\n");
    scanf("%f", &base);

    printf("Enter length of height of Triangle\n");
    scanf("%f", &height);

    area = (base * height) / 2.0;

    printf("Area of Triangle is %0.2f\n", area);

    return 0;
}

Output:
Enter length of base of Triangle
15
Enter length of height of Triangle
25
Area of Triangle is 187.50

Validate the Input and Find Area of Triangle: C Program

#include < stdio.h >

int main()
{
    float base, height, area;

    printf("Enter length of base of Triangle\n");
    scanf("%f", &base);

    printf("Enter length of height of Triangle\n");
    scanf("%f", &height);

    if(base == 0 || height == 0)
    {
        printf("Invalid Input\n");
    }
    else
    {
        area = (base * height) / 2.0;
        printf("Area of Triangle is %0.2f\n", area);
    }

    return 0;
}

Output 1:
Enter length of base of Triangle
0
Enter length of height of Triangle
25
Invalid Input

Output 2:
Enter length of base of Triangle
15
Enter length of height of Triangle
30
Area of Triangle is 225.00

Note: Note that the area of Triangle has only 2 digits after the decimal point. That is because we have %0.2f as format specifier in the printf method where we are printing out the area of Triangle.

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

Find Missing Angle in a Triangle if two angles are given: C Program

We ask the user to input 2 angles of the Triangle. And using C Program we calculate and output the 3rd angle.

Related Read:
Basic Arithmetic Operations In C
Triangle Valid or Not based On Sides: C Program.

Formula To Calculate Valid Triangle

a + b + c = 180;

Formula To Find Missing Angle of Triangle

c = 180 – (a + b);

where a, b and c are angles of Triangle.

Note: Also not that if any of the angle is 0, then those 3 angles can’t form a Triangle, so it’s a invalid Triangle.

Find Missing Angle in a Triangle if two angles are given: C Program


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

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


Find Missing Angle in a Triangle if two angles are given: C Program

#include < stdio.h >

int main()
{
    int a1, a2, a3;

    printf("Enter 2 angles of Triangle\n");
    scanf("%d%d", &a1, &a2);

    a3 = 180 - ( a1 + a2 );

    if(a1 != 0 && a2 != 0 && a3 != 0)
    {
        printf("The 3rd angle is %d\n", a3);
    }
    else
    {
        printf("Invalid angles of Triangle\n");
    }

    return 0;
}

Output 1:
Enter 2 angles of Triangle
0
90
Invalid angles of Triangle

Output 2:
Enter 2 angles of Triangle
90
90
Invalid angles of Triangle

Output 3:
Enter 2 angles of Triangle
90
80
The 3rd angle is 10

Output 3 outputs valid Triangle as addition of all 3 angles gives 180.
90 + 80 + 10 = 180.

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

Triangle Valid or Not based On Angles: C Program

Three angles of a Triangle are entered through the keyboard, write a C program to check whether the Triangle is valid or not. The Triangle is valid if the sum of all the angles is exactly equal to 180.

Related Read:
Basic Arithmetic Operations In C
Triangle Valid or Not based On Sides: C Program.

Logic To Find Valid Triangle or Not

We ask the user to enter all 3 angles of a Triangle. Then we add all these angles and if the result is 180 then its a valid Triangle, if not, its not a valid Triangle.

Formula To Calculate Valid Triangle
a + b + c = 180;

where a, b and c are 3 angles of a Triangle.

Note: Also not that if any of the angle is 0, then those 3 angles can’t form a Triangle, so it’s a invalid Triangle.

Triangle Valid or Not based On Angles: C Program


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

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


Triangle Valid or Not based On Angles: C Program

#include < stdio.h >

int main()
{
    int a1, a2, a3, sum;

    printf("Enter 3 angles of a Triangle\n");
    scanf("%d%d%d", &a1, &a2, &a3);

    sum = a1 + a2 + a3;

    if(sum == 180 && a1 != 0 && a2 != 0 && a3 != 0)
    {
        printf("It's a valid Triangle\n");
    }
    else
    {
        printf("It's not a valid Triangle\n");
    }

    return 0;
}

Output 1:
Enter 3 angles of a Triangle
90
80
0
It’s not a valid Triangle

Output 2:
Enter 3 angles of a Triangle
90
90
90
It’s not a valid Triangle

Output 3:
Enter 3 angles of a Triangle
90
80
10
It’s a valid Triangle

Output 3 outputs valid Triangle as addition of all 3 angles gives 180.
90 + 80 + 10 = 180.

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

Find Area of an Equilateral Triangle: C Program

Given the sides of the Triangle, write a C program to calculate Perimeter and Area of a equilateral Triangle.

Related Read:
Find Area of a Triangle Using Its Sides: C Program

Note:
Equilateral Triangle: A Triangle is called equilateral triangle if length of 3 sides of it are equal.
Example: a = 10, b = 10, c = 10;

So we ask the user to input value of the side only once.

Formula To Calculate Perimeter and Area of Equilateral Triangle

Perimeter = a + a + a;
i.e., Perimeter = 3 * a;

Area = ( ( sqrt(3) / 4 ) * pow(a, 2));

where a is the value of side of the Equilateral Triangle.

Find Area of an Equilateral Triangle: C Program


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

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


Find Area of an Equilateral Triangle: C Program

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

int main()
{
    float sides, area, p;

    printf("Enter value for side of Equilateral Triangle\n");
    scanf("%f", &sides);

    p    = 3 * sides;
    area = ( (sqrt(3)/4) * pow(sides, 2) );

    printf("Perimeter is %f\n", p);
    printf("Area is %f\n", area);

    return 0;
}

Output:
Enter value for side of Equilateral Triangle
75
Perimeter is 225.000000
Area is 2435.696533

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