C Program to print Armstrong Numbers between 1 and 500

Write a program to print out all Armstrong numbers or Narcissistic number between 1 and 500. If sum of cubes of each digit of the number is equal to the number itself, then the number is called an Armstrong number.

Related Read:
Nested While Loop: C Program
C Program to Check Armstrong Number

For Example:
407 = (4*4*4)+(0*0*0)+(7*7*7)
407 = (64) + (0) + (343)
407 = 407
Hence, 407 is a Armstrong number.

Nested While Loop
In this program we are using nested while loop to check for Armstrong numbers from 1 to 500.

Outer while loop loops from 1 to 500, by incrementing value of count by 1. Inner while loop checks every value of count to determine if the value is a Armstrong number or not.

Logic for Finding Armstrong Number

We’ve explained the logic to find Armstrong number in detail, in our previous video tutorial. Kindly visit C Program to Check Armstrong Number

C Program to print Armstrong Numbers between 1 and 500


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

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


Source Code: C Program to print Armstrong Numbers between 1 and 500

#include < stdio.h >

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

    while(count <= 500)
    {
        num = count;
        sum = 0;

        while(num)
        {
            rem = num % 10;
            sum = sum + (rem * rem * rem);
            num = num / 10;
        }

        if(count == sum)
        {
            printf("%d is a Armstrong number\n", count);
        }

        count++;
    }

     return 0;
}

Output:
1 is a Armstrong number
153 is a Armstrong number
370 is a Armstrong number
371 is a Armstrong number
407 is a Armstrong number

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

Nested While Loop: C Program

In this video tutorial we’ll demonstrate the use of nested while loop in C programming.

Related Read:
C Program to print Armstrong Numbers between 1 and 500

Number of Iterations In Nested Loops
Number of iterations will be equal to the number of iterations in the outer loop multiplied by the number of iterations in the inner loop.

Nested While Loop: C Program


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

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


Source Code: Single While Loop: C Program

#include < stdio.h >

int main()
{
    int count1 = 1;

    while(count1 <= 5)
    {
        printf("%d\n", count1);
        count1++;
    }

    return 0;
}

Output:
1
2
3
4
5

Source Code: Nested While Loop: C Program

#include < stdio.h >

int main()
{
    int count1 = 1, count2;

    while(count1 <= 5)
    {
        printf("%d\n", count1);
        count2 = 2;

        while(count2)
        {
            printf("   %d\n", count2);
            count2--;
        }

        count1++;
    }

    return 0;
}

Output:

1
   2
   1
2
   2
   1
3
   2
   1
4
   2
   1
5
   2
   1

For every single iteration of the outer while loop, the inner while loop completes its iterations.

A loop inside another loop is called a nested loop. Consider a nested loop where the outer loop runs x times and consists of another loop inside it. The inner loop runs y times. Then, the total number of times the inner loop runs during the program execution is x*y times.

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 Check Armstrong Number

Lets write a C program to check whether user entered number is Armstrong or not.

Armstrong number: is a number that is equal to the sum of cubes of its individual digits.

Example: If user input the number 371. It’s individual digits are 3, 7 and 1. Lets cube each digit: 33 + 73 + 13 = 27 + 343 + 1 = 371.

The user entered number 371 is equal to the sum of cube of its individual digits. So 371 is a Armstrong number.

Logic

If user enters number as 371.
1. Lets use modular division on that number.
371 % 10 = 1;
2. Lets cube the result(reminder) and store it inside a variable sum.
(1*1*1) = 1. So sum = 1.
3. Divide the number(371) by 10.
371 / 10 = 37. (Remember, when you divide a number by integer number, it returns only the integer part of the result).

Now, sum = 1, number = 37.

Lets repeat step 1, 2 and 3.
1. Lets use modular division on that number.
37 % 10 = 7;
2. Lets cube the result(reminder) and store it inside a variable sum.
(7*7*7) = 343. So sum = 1 + 343.
3. Divide the number(37) by 10.
37 / 10 = 3.

Now, sum = 344, number = 3.

Lets repeat the steps 1,2 and 3 one more time.
1. Lets use modular division on that number.
3 % 10 = 3;
2. Lets cube the result(reminder) and store it inside a variable sum.
(3*3*3) = 27. So sum = 344 + 27.
3. Divide the number(3) by 10.
3 / 10 = 0.

Now, sum = 371, number = 0;

Since number is 0, which means false, the while loop exits.

Next using if-else we check if the user entered number is equal to the sum of cubes of its individual digits.

Related Read:
C Program To Reverse a Number
Basic Arithmetic Operations In C
while loop in C programming
if else statement in C
Calculate Sum of Digits: C Program
C Program to print Armstrong Numbers between 1 and 500

C Program to Check Armstrong Number


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

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


Source Code: C Program to Check Armstrong Number

#include < stdio.h >

int main()
{
    int num, rem, sum = 0, temp;

    printf("Enter an integer number\n");
    scanf("%d", &num);

    temp = num;

    while(num)
    {
        rem = num % 10;
        sum = sum + (rem * rem * rem);
        num = num / 10;
    }

    if(temp == sum)
    {
        printf("%d is armstrong number\n", temp);
    }
    else
    {
        printf("%d is not armstrong number\n", temp);
    }

    return 0;
}

Output 1:
Enter an integer number
371
371 is armstrong number

Output 2:
Enter an integer number
563
563 is not armstrong number

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

Check whether a Number is Palindrome or Not: C Program

User inputs a integer number and we write a program to reverse that number and if the user input value and the reversed number are equal then its a palindrome number, if not, its not a palindrome number.

Related Read:
C Program To Reverse a Number
Basic Arithmetic Operations In C
while loop in C programming
if else statement in C
Calculate Sum of Digits: C Program

Palindrome Number:
If a number remains same even if we reverse its digits then the number is known as palindrome number.

In other words, a number when read in both forward and backward way is same, then such a number is called a palindrome number.

Note: We assign variable reverse = 0 to avoid garbage values in variable reverse.

Reversing a Number Logic

If user enters 456, we apply the modulo division to get the individual values.
Ex:
456 % 10 = 6
45 % 10 = 5
4 % 10 = 4

After each iteration of while loop we add the reminder to the previous value of variable reverse. We also multiply the variable reverse with 10, to increment the decimal place.

We get 456, 45 and 4 by dividing the original value by 10.
Ex:
456 user entered value.
456 / 10 = 45
45 / 10 = 4

Now the reverse:

reverse = (reverse * 10 ) + rem;

06 = (0*10) + 6
65 = (6*10) + 5
654 = (65*10) + 4

So finally the reversed number is 654.

Check For Palindrome Logic

If the user entered value(original value) is equal to the revered number, then its palindrome number or else its not a palindrome.

For Example:
user entered number is 456.
Reversed number is 654.

So, 456 is not equal to 654, so the number 456 is not a palindrome.

Check whether a Number is Palindrome or Not: C Program


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

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


Source Code: Check whether a Number is Palindrome or Not: C Program

#include < stdio.h >

int main()
{
    int num, reverse = 0, rem, temp;

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

    temp = num;

    while(num)
    {
        rem = num % 10;
        reverse = reverse * 10 + rem;
        num     = num / 10;
    }

    printf("Reversed number is %d\n", reverse);

    if(temp == reverse)
    {
        printf("Entered number is palindrome\n");
    }
    else
    {
        printf("Entered number is not palindrome\n");
    }

    return 0;
}

Output 1:
Enter a integer number
12345
Reversed number is 54321
Entered number is not palindrome

Output 2:
Enter a integer number
123321
Reversed number is 123321
Entered number is palindrome

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 Reverse a Number

If a integer number is input through the keyboard, write a program to reverse the input number and display it on the console window.

For Example: If user enters 135, reversed number must be 531.

Related Read:
Basic Arithmetic Operations In C
while loop in C programming
Calculate Sum of Digits: C Program
Check whether a Number is Palindrome or Not: C Program

Note: We assign variable reverse = 0 to avoid garbage values in variable reverse.

Reversing a Number Logic

If user enters 456, we apply the modulo division to get the individual values.
Ex:
456 % 10 = 6
45 % 10 = 5
4 % 10 = 4

After each iteration of while loop we add the reminder to the previous value of variable reverse. We also multiply the variable reverse with 10, to increment the decimal place.

We get 456, 45 and 4 by dividing the original value by 10.
Ex:
456 user entered value.
456 / 10 = 45
45 / 10 = 4

Now the reverse:

reverse = (reverse * 10 ) + rem;

06 = (010) + 6
65 = (6
10) + 5
654 = (65*10) + 4

So finally the reversed number is 654.

Video Tutorial: C Program To Reverse a Number


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

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


Source Code: C Program To Reverse a Number

#include<stdio.h>

int main()
{
    int num, reverse = 0, rem;

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

    while(num)
    {
        rem = num % 10;
        reverse = (reverse * 10) + rem;
        num     = num / 10;
    }

    printf("Reversed number is %d\n", reverse);

    return 0;
}

Output 1:
Enter a integer number
1023
Reversed number is 3201

Output 2:
Enter a integer number
159
Reversed number is 951

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