C Program To Draw Pyramid of Alphabets, using While Loop

Lets write a C program to draw / print / display a pyramid / triangle formed from uppercase / capital letter English Alphabets, using while loop.

Related Read:
Nested While Loop: C Program
C Program To Draw Pyramid of Stars, using While Loop

Expected Output for the Input

User Input:
Enter the number of rows for Pyramid
5

Output:

    
     A
    BCD
   EFGHI
  JKLMNOP
 QRSTUVWXY

Pyramid With 20 Rows
pyramid of alphabets

Video Tutorial: C Program To Draw Pyramid of Alphabets, using While Loop


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

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

Logic To Draw Pyramid of Alphabets, using While Loop

We ask the user to enter the maximum number of rows for the pyramid and store it inside the address of variable num. We declare and initialize a variable count to 1(indicating first row of the pyramid).

In the outer while loop we check if count is less than or equal to num. For each iteration of the while loop we increment the value of count by 1. So the count value will have the row number i.e., for each iteration of the outer while loop we select row one by one (to print the alphabets).

Inside first inner while loop we print the adequate number of space for each row. Inside the second inner while loop we actually print the alphabets needed for each row.

At the end the result will be a pyramid with the number of rows as input by the user.

ASCII Value of Capital Letter 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

Reference: C Program To Print All ASCII Characters and Code

Source Code: C Program To Draw Pyramid of Alphabets, using While Loop

#include < stdio.h >

int main()
{
    int num, count = 1, i, alphabet = 65;

    printf("Enter the number of rows for Pyramid\n");
    scanf("%d", &num);

    while(count <= num)
    {
        i = 0;
        while(i <= (num-count))
        {
            printf(" ");
            i++;
        }

        i = 0;
        while(i < (2*count-1))
        {
            printf("%c", alphabet);

            if(alphabet == 90)
            {
                alphabet = 64;
            }

            alphabet++;
            i++;
        }
        printf("\n");
        count++;
    }

    return 0;
}

Output
Enter the number of rows for Pyramid
14

              A
             BCD
            EFGHI
           JKLMNOP
          QRSTUVWXY
         ZABCDEFGHIJ
        KLMNOPQRSTUVW
       XYZABCDEFGHIJKL
      MNOPQRSTUVWXYZABC
     DEFGHIJKLMNOPQRSTUV
    WXYZABCDEFGHIJKLMNOPQ
   RSTUVWXYZABCDEFGHIJKLMN
  OPQRSTUVWXYZABCDEFGHIJKLM
 NOPQRSTUVWXYZABCDEFGHIJKLMN

Note: Inside second nested while loop we are checking for the condition – if alphabet is equal to 90(ASCII value of Z), and then reinitializing it to 64(but ASCII value of A is 65), that is because after the if condition is executed we have alphabet++; statement which increments the value of variable alphabet from 64 to 65(which is the ASCII value of A).

Alphabets and ASCII Value
We can even write the code in if else block as shown below:

        i = 0;
        while(i < (2*count-1))
        {
            printf("%c", alphabet);

            if(alphabet == 90)
            {
                alphabet = 65;
            }
            else
            {
                alphabet++;
            }
            i++;
        }

In this case we assign the value of variable alphabet to 65(ASCII value of A) once it has reached a value of 90(ASCII value of Z).

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 Draw Pyramid of Stars, using While Loop

Lets write a C program to draw / print / display a pyramid / triangle formed from stars, using while loop.

Related Read:
while loop in C programming
Nested While Loop: C Program

Expected Output for the Input

User Input:
Enter no of rows of Pyramid
5

Output:

    
     *
    ***
   *****
  *******
 *********

Pyramid With 20 Rows
pyramid of stars

Video Tutorial: C Program To Draw Pyramid of Stars, using While Loop


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

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

Logic To Draw Pyramid of Stars, using While Loop

We ask the user to enter the maximum number of rows for the pyramid and store it inside the address of variable num. We declare and initialize a variable count to 1(indicating first row of the pyramid).

In the outer while loop we check if count is less than or equal to num. For each iteration of the while loop we increment the value of count by 1. So the count value will have the row number i.e., for each iteration of the outer while loop we select row one by one (to print the stars).

Inside first inner while loop we print the adequate number of space for each row. Inside the second inner while loop we actually print the stars needed for each row.

At the end the result will be a pyramid with the number of rows as input by the user.

Source Code: C Program To Draw Pyramid of Stars, using While Loop

#include<stdio.h>

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

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

    while(count <= num)
    {
        i = 0;
        while( i <= (num - count) )
        {
            printf(" ");
            i++;
        }

        i = 0;
        while(i < (2*count-1))
        {
            printf("*");
            i++;
        }

        printf("\n");
        count++;
    }

    return 0;
}

Output 1:
Enter no of rows of Pyramid
5

     *
    ***
   *****
  *******
 *********

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 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 Find Prime Numbers Between Two Intervals, using While Loop

Lets write a C program to find and print/display all the prime numbers between 2 integer values input by the user, using nested while loop.

Prime Number: Any natural number which is greater than 1 and has only two factors i.e., 1 and the number itself is called a prime number.

Related Read:
Decision Control Instruction In C: IF
Nested While Loop: C Program
C Program To Find Prime Number or Not using While Loop
C Program To Find Prime Numbers From 2 To N, using While Loop

Video Tutorial: C Program To Find Prime Numbers Between Range, using While Loop


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

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

Inner While loop Logic

All the numbers are perfectly divisible by number 1, so we initialize the variable count to 2, instead of 1. So our c program starts checking for divisibility from number 2.

While loop Logic

Outer while loop selects a number for each iteration and stores inside variable start. Inner while loop checks if the selected value(present in variable start) is prime or not. If its a prime number then the variable prime will have value of 1 or else it’ll have value 0 inside it(after completion of inner while loop iteration). If the value present in variable prime is 1, then we print the value present in variable start on to the console window.

Source Code: C Program To Find Prime Numbers Between Two Intervals, using While Loop

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

int main()
{
    int start, end, count, prime, inum;

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

    printf("\n\nPrime Numbers from %d to %d are:\n", start, end);
    while(start <= end)
    {
        inum  = sqrt(start);
        count = 2;
        prime = 1;

        while(count <= inum)
        {
            if(start%count == 0)
            {
                prime = 0;
                break;
            }
            count++;
        }

        if(prime) printf("%d, ", start);
        start++;
    }
    printf("\n\n");

    return 0;
}

Output 1:
Enter start and end value
10
50

Prime Numbers from 10 to 50 are:
11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,

Output 2:
Enter start and end value
50
100

Prime Numbers from 50 to 100 are:
53, 59, 61, 67, 71, 73, 79, 83, 89, 97,

Output 3:
Enter start and end value
100
200

Prime Numbers from 100 to 200 are:
101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199,

Output 4:
Enter start and end value
25
50

Prime Numbers from 25 to 50 are:
29, 31, 37, 41, 43, 47,

Output 5:
Enter start and end value
75
150

Prime Numbers from 75 to 150 are:
79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149,

Logic To Find Prime Number, using While Loop

In this method, we apply square root to the user entered number and store it inside variable inum. This reduces the number of iterations of inner while loop.

For example,
If num = 100;
inum = sqrt(num);
inum = sqrt(100);
inum = 10;

User entered number 100 is perfectly divisible by 5 and 10, so number 100 is not a prime number.

So its enough if we iterate through the while loop sqrt(num) times to check if the selected number is divisible by any number other than 1 and itself.

Table of all prime numbers up to 1,000:

prime number or not

Note: We are not using curly braces around if statement because we only have 1 line of code after if – so curly braces are optional. If we have multiple lines of code, then we must use curly braces to wrap around the block of code.

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 Prime Numbers From 2 To N, using While Loop

Lets write a C program to find and print / display all the prime numbers from 2 to N. Here N is the user entered number / limit.

Prime Number: Any natural number which is greater than 1 and has only two factors i.e., 1 and the number itself is called a prime number.

Related Read:
Decision Control Instruction In C: IF
Nested While Loop: C Program
C Program To Find Prime Number or Not using While Loop

Video Tutorial: C Program To Find Prime Numbers From 2 To N, using While Loop


[youtube https://www.youtube.com/watch?v=iYHo-9rClAs]

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

While loop Logic

All the numbers are perfectly divisible by number 1, so we initialize the variables start and count to 2, instead of 1. So our c program starts checking for divisibility from number 2.

Source Code: C Program To Find Prime Numbers From 2 To N, using Nested While Loop

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

int main()
{
    int start = 2, num, count, flag, inum;

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

    printf("\n\nPrime numbers from 2 to %d are:\n", num);

    while(start <= num)
    {
        inum = sqrt(start);
        count= 2;
        flag = 1;

        while(count <= inum)
        {
            if(start%count == 0)
            {
                flag = 0;
                break;
            }
            count++;
        }

        if(flag) printf("%d, ", start);

        start++;
    }

    printf("\n\n");

    return 0;
}

Output 1:
Enter the limit
50

Prime numbers from 2 to 50 are:
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,

Output 2:
Enter the limit
75

Prime numbers from 2 to 75 are:
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73,

Output 3:
Enter the limit
100

Prime numbers from 2 to 100 are:
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97,

Output 4:
Enter the limit
500

Prime numbers from 2 to 500 are:
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499,

Output 5:
Enter the limit
1000

Prime numbers from 2 to 1000 are:
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997,

Logic To Find Prime Numbers From 2 To N, using While Loop

In this method, we apply square root to the user entered number and store it inside variable inum. This reduces the number of iterations of inner while loop.

For example,
If num = 100;
inum = sqrt(num);
inum = sqrt(100);
inum = 10;

User entered number 100 is perfectly divisible by 5 and 10, so number 100 is not a prime number.

So its enough if we iterate through the while loop sqrt(num) times to check if the user entered number is divisible by any number other than 1 and itself.

We start checking for prime number from number 2 till the user entered number. Variable start is assigned an initial value of 2. For each iteration of outer while loop value of start increments by 1. Inside the inner while loop we check if the value present in variable start is prime number or not. If its prime number then we print that number on to the console window.

Outer while loop selects the number and stores it in variable start on each iteration. Inner while loop checks if the selected number(present in variable start) is prime number or not. You can know the complete logic to check whether a selected number is prime or not: C Program To Find Prime Number or Not using While Loop

Table of all prime numbers up to 1,000:

prime number or not

Note: We are not using curly braces around if statement because we only have 1 line of code after if – so curly braces are optional. If we have multiple lines of code, then we must use curly braces to wrap around the block of code.

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