C Program To Display Right Angled Triangle With Alphabets, using While Loop

Lets write a C program to display/print/output a right angled triangle pattern formed with English Alphabets, using nested while loop.

Related Read:
Nested While Loop: C Program
C Program To Print Floyd’s Triangle

Expected Output for the Input

If user enters number of rows as 5. Then our C program prints a Right angled Triangle with 5 rows of English Alphabets in it.

User Input:
num = 5;

Output:

A
B B
C C C
D D D D
E E E E E

Video Tutorial: C Program To Print Right Angled Triangle With Alphabets, using While Loop


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

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

Logic To Display Right Angled Triangle With Alphabets, using While Loop

If we take a closer look at the right angled triangle, we can see that the number of rows and the number of elements in that row are same. Example, row number 5 has 5 elements. Row number 6 has 6 elements etc. So we store this number in a variable called row. Since row starts from 1, we initialize the variable row to 1.

We ask the user to enter the maximum number of rows for the right angled triangle, and store it in address of variable num. In outer while loop we check if value of row is less than or equal to num. For each iteration of the outer while loop we increment the value of row by 1.

Inside the outer while loop we initialze the variable start to 1 – to print the alphabets from position 1 for that particular row number present in variable row. We increment the value of start by 1 for each iteration of inner while loop. Inside inner while loop we print the alphabets using ASCII value of upper case English Alphabets.

Related Read:
C Program To Print All ASCII Characters and Code

ASCII Values of uppercase English Alphabets

ASCII value of A is 65

ASCII value of B is 66

ASCII value of C is 67

ASCII value of D is 68

ASCII value of E is 69

ASCII value of F is 70

ASCII value of G is 71

ASCII value of H is 72

ASCII value of I is 73

ASCII value of J is 74

ASCII value of K is 75

ASCII value of L is 76

ASCII value of M is 77

ASCII value of N is 78

ASCII value of O is 79

ASCII value of P is 80

ASCII value of Q is 81

ASCII value of R is 82

ASCII value of S is 83

ASCII value of T is 84

ASCII value of U is 85

ASCII value of V is 86

ASCII value of W is 87

ASCII value of X is 88

ASCII value of Y is 89

ASCII value of Z is 90

Source Code: C Program To Display Right Angled Triangle With Alphabets, using While Loop

#include<stdio.h>

int main()
{
    int num, row = 1, start;

    printf("Enter no of rows\n");
    scanf("%d", &num);

    printf("\n");
    while(row <= num)
    {
        start = 1;
        while(start <= row)
        {
            printf("%c  ", (row+64));
            start++;
        }
        printf("\n");
        row++;
    }

    return 0;
}

Output 1:
Enter no of rows
5

A
B B
C C C
D D D D
E E E E E

Output 2:
Enter no of rows
10

A
B  B
C  C  C
D  D  D  D
E  E  E  E  E
F  F  F  F  F  F
G  G  G  G  G  G  G
H  H  H  H  H  H  H  H
I  I  I  I  I  I  I  I  I
J  J  J  J  J  J  J  J  J  J

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 Print 1+4+9+16 Series, using While Loop

Lets write C program to print/display number series 1 + 4 + 9 + 16 + 25 + using while loop.

Logic To Print 1+4+9+16 number series using while loop

If we analyze the number series, its just addition of square of natural numbers. i.e., (1 x 1) + (2 x 2) + (3 x 3) + (4 x 4) + (5 x 5) + .. etc

We ask the user to enter a number. If user enters num = 5, then we display the first 5 numbers in the series i.e., 1 + 4 + 9 + 16 + 25 +

If user enters num = 10, then we display the first 10 numbers in the series i.e., 1 + 4 + 9 + 16 + 25 + 36 + 49 + 64 + 81 + 100 +

Related Read:
while loop in C programming

Video Tutorial: C Program To Print 1+4+9+16+25 Series, using While Loop


[youtube https://www.youtube.com/watch?v=7WW17-nW-8k]

YouTube Link: https://www.youtube.com/watch?v=7WW17-nW-8k [Watch the Video In Full Screen.]

Source Code: C Program To Print 1+4+9+16 Series, using While Loop

#include<stdio.h>

int main()
{
    int num, count = 1;

    printf("Enter a integer number greater than 1\n");
    scanf("%d", &num);

    while(count <= num)
    {
        printf("%d + ", (count*count));
        count++;
    }

    return 0;
}

Output 1:
Enter a integer number greater than 1
5
1 + 4 + 9 + 16 + 25 +

Output 2:
Enter a integer number greater than 1
10
1 + 4 + 9 + 16 + 25 + 36 + 49 + 64 + 81 + 100 +

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 Find Sum of All Odd Numbers Between Two Integers, using While loop

Lets write a C program to find sum of all odd numbers between range or between 2 integers input by the user.

Odd Number: An odd number is an integer that is not exactly divisible by 2.

For Example: 13 % 2 != 0. When we divide 13 by 2, it does not give a reminder of 0. So number 13 is an odd number.

Note: In this C program we ask the user to input start and end value. We assume that the user enters bigger value for variable end and smaller value for variable start. i.e., start < end

If user enters start = 10 and end = 20. C program finds all the odd numbers between 10 and 20, including 10 and 20. So the odd numbers are 11, 13, 15, 17, 19. We add all these odd numbers and output the sum to the console window. i.e., 11 + 13 + 15 + 17 + 19 = 75. We out put the value 75 as result.

Related Read:
Decision Control Instruction In C: IF
while loop in C programming
Even or Odd Number: C Program
C Program to Generate Odd Numbers Between Two Integers
C Program To Find Sum of All Odd Numbers From 1 To N, using While loop

Video Tutorial: C Program To Find Sum of All Odd Numbers Between Range, using While loop


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

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

Logic To Find Sum of All Odd Numbers Between Two Integers, using While loop

Step 1: We ask the user to enter start and end value.

Step 2: We iterate through the while loop until value of start is less than or equal to value of variable end. Inside while loop we keep incrementing the value of variable start by one for each iteration.

Step 3: For every iteration we check if value present in variable start is an odd number. i.e., start % 2 != 0. If this condition is true, then we add the value present in variable start to the previous value of variable sum.

Step 4: Once the control exits while loop, we print the value present in variable sum – which has the sum of all the odd numbers between the range entered by the user.

Source Code: C Program To Find Sum of All Odd Numbers Between Two Integers, using While loop

#include<stdio.h>

int main()
{
    int start, end, sum = 0;

    printf("Enter start and end value\n");
    scanf("%d%d", &start, &end);

    printf("\nSum of odd numbers from %d to %d is ", start, end);
    while(start <= end)
    {
        if(start % 2 != 0)
        {
            sum = sum + start;
        }
        start++;
    }
    printf("%d\n", sum);

    return 0;
}

Output 1:
Enter start and end value
10
20

Sum of odd numbers from 10 to 20 is 75

Output 2:
Enter start and end value
25
50

Sum of odd numbers from 25 to 50 is 481

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 Find Sum of All Even Numbers Between Two Integers, using While loop

Lets write a C program to find sum of all the even numbers between range or between 2 integers input by the user.

Even Number: An even number is an integer that is exactly divisible by 2.

For Example: 10 % 2 == 0. When we divide 10 by 2, it give a reminder of 0. So number 10 is an even number.

Note: In this C program we ask the user to input start and end value. We assume that the user enters bigger value for variable end and smaller value for variable start. i.e., start < end

If user enters start = 10 and end = 20. C program finds all the even numbers between 10 and 20, including 10 and 20. So the even numbers are 10, 12, 14, 16, 18, 20. We add all these even numbers and output the sum to the console window. i.e., 10 + 12 + 14 + 16 + 18 + 20 = 90. We out put the value 90 as result.

Related Read:
Decision Control Instruction In C: IF
while loop in C programming
Even or Odd Number: C Program
C Program to Generate Even Numbers Between Two Integers
C Program To Find Sum of All Even Numbers From 1 To N, using While loop

Video Tutorial: C Program To Find Sum of All Even Numbers Between Range, using While loop


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

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

Logic To Find Sum of All Even Numbers Between Two Integers, using While loop

Step 1: We ask the user to enter start and end value.

Step 2: We iterate through the while loop until value of start is less than or equal to value of variable end. Inside while loop we keep incrementing the value of variable start by one for each iteration.

Step 3: For every iteration we check if value present in variable start is a even number. i.e., start % 2 == 0. If this condition is true, then we add the value present in variable start to the previous value of variable sum.

Step 4: Once the control exits while loop, we print the value present in variable sum – which has the sum of all the even numbers between the range entered by the user.

Source Code: C Program To Find Sum of All Even Numbers Between Two Integers, using While loop

#include<stdio.h>
int main()
{
    int start, end, sum = 0;

    printf("Enter start and end value\n");
    scanf("%d%d", &start, &end);

    printf("\nSum of even no's from %d to %d is ", start, end);
    while(start <= end)
    {
        if(start % 2 == 0)
        {
            sum = sum + start;
        }
        start++;
    }
    printf("%d\n", sum);

    return 0;
}

Output 1:
Enter start and end value
10
20

Sum of even no’s from 10 to 20 is 90

Output 2:
Enter start and end value
25
50

Sum of even no’s from 25 to 50 is 494

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 Find Sum of All Even Numbers From 1 To N, using While loop

Lets write a C program to find sum of all the even numbers from 1 to N, using while loop.

Even Number: An even number is an integer that is exactly divisible by 2.

For Example: 8 % 2 == 0. When we divide 8 by 2, it give a reminder of 0. So number 8 is an even number.

If user enters num = 5. Even numbers between 1 to 5 are 2, 4. So we add 2 and 4. i.e., 2 + 4 = 6. We display 6 to the console window as result.

Related Read:
Decision Control Instruction In C: IF
while loop in C programming
Even or Odd Number: C Program
C Program to Generate Even Numbers Between Two Integers

Video Tutorial: C Program To Find Sum of All Even Numbers From 1 To N, using While loop


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

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

Logic To Find Sum of All Even Numbers From 1 To N, using While loop

Since we are checking for even numbers from 1 to user entered number, we assign value of variable count to 1. While loop keeps iterating until value of count is less than or equal to value of user input number. For each iteration of while loop we increment the value of count by 1.

Inside while loop we check for the condition, count%2 == 0. If it’s true, then we add the value present inside variable count to previous value present in variable sum.

After the control exits while loop we display the value present in variable sum – which has the sum of all the even numbers from 1 to user entered number.

Source Code: C Program To Find Sum of All Even Numbers From 1 To N, using While loop

#include<stdio.h>

int main()
{
    int num, count = 1, sum = 0;

    printf("Enter the limit\n");
    scanf("%d", &num);

    while(count <= num)
    {
        if(count%2 == 0)
        {
            sum = sum + count;
        }
        count++;
    }
    printf("Sum of Even numbers from 1 to %d is %d\n", num, sum);
    return 0;
}

Output 1:
Enter the limit
5
Sum of Even numbers from 1 to 5 is 6

Output 2:
Enter the limit
20
Sum of Even numbers from 1 to 20 is 110

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