C Program To Convert Decimal Number To Binary Number, using While Loop

Lets write a C program to convert a number from Decimal number system(base 10) to Binary number system(base 2), using while loop.

Related Read:
while loop in C programming
Calculate Sum of Digits: C Program
C Program To Reverse a Number

Note: Binary number system can be derived by base 2 to the power of whole numbers.

Example:
etc .. 24, 23, 22, 21, 20

etc .. 24 = 16, 23 = 8, 22 = 4, 21 = 2, 20 = 1.

binary decimal number system

Expected Output for the Input

User Input:
Enter a decimal number
14

Output:
Binary equivalent of 14 is 1110

Explanation:
If user enters num = 14

We keep on dividing the number 14 by 2.

14 / 2 = 7, reminder 0.
07 / 2 = 3, reminder 1.
03 / 2 = 1, reminder 1.

Decimal to binary

So Binary equivalent of 14 is 1110.

OR

(23 x 1) + (22 x 1) + (21 x 1) + (20 x 0 )
= 8 + 4 + 2
= 14.

Video Tutorial: C Program To Convert Decimal Number To Binary Number, using While Loop


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

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

Logic To Convert Decimal Number To Binary Number, using While Loop

In this C program we ask the user to enter / input a decimal number. Using while loop we calculate the reminder and add it to variable bin. We make use of variable place to position the reminder based on number system – unit, ten, hundred, thousand, ten thousand etc.

Source Code: C Program To Convert Decimal Number To Binary Number, using While Loop

#include < stdio.h >

int main()
{
    int num, bin = 0, rem = 0, place = 1;

    printf("Enter a decimal number\n");
    scanf("%d", &num);

    printf("\nBinary equivalent of %d is ", num);
    while(num)
    {
        rem   = num % 2;
        num   = num / 2;
        bin   = bin + (rem * place);
        place = place * 10;
    }
    printf("%d\n", bin);

    return 0;
}

Output 1:
Enter a decimal number
14

Binary equivalent of 14 is 1110

Output 2:
Enter a decimal number
15

Binary equivalent of 15 is 1111

Output 3:
Enter a decimal number
19

Binary equivalent of 19 is 10011

Output 4:
Enter a decimal number
8

Binary equivalent of 8 is 1000

Output 5:
Enter a decimal number
41

Binary equivalent of 41 is 101001

Number Systems

number systems

1. Binary Number System uses base 2 and digits 01.
2. Octal Number System uses base 8 and digits 01234567.
3. Decimal Number System uses base 10 and digits 0123456789.
4. Hexadecimal Number System uses base 16 and digits 0123456789ABCDEF.

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