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

Calculate Sum of Digits: C Program

Lets ask the user to input a integer number through keyboard. Lets write a c program to calculate sum of its digits.

Related Read:
Find Sum of Digits In A Given Number: C

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

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

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

sum = sum + rem;

06 = 0 + 6
11 = 6 + 5
15 = 11 + 4

So the sum of digits in the given integer number is 15.

Calculate Sum of Digits: C Program


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

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


Source Code: Calculate Sum of Digits: C Program

#include < stdio.h >

int main()
{
    int num, reminder, sum = 0;

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

    while(num != 0)
    {
     reminder = num % 10;
     sum      = sum + reminder;
     num      = num / 10;
    }

    printf("Sum of digit is %d\n", sum);

    return 0;
}

Output 1:
Enter a integer number
456
Sum of digit is 15

Output 2:
Enter a integer number
8910
Sum of digit is 18

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

Convert Kilometer To Meter, Centimeter, Millimeter: C Program

The distance between two cities (in Kilometer) is input through the keyboard. Write a program to convert and print this distance in meters, centimeter, millimeter.

Note:
1 Kilometer(km) = 1000 Meters(m).
1 Kilometer(km) = 100000 Centimeters(cm).
1 Kilometer(km) = 1000000 Millimeters(mm).

For n Kilometers(KM)
n Kilometer = n * 1000 Meters
n Kilometer = n * 100000 Centimeters
n Kilometer = n * 1000000 Millimeters

Convert Kilometer To Meter, Centimeter, Millimeter: C Program


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

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


Source Code: Convert Kilometer To Meter, Centimeter, Millimeter: C Program

#include < stdio.h >

int main()
{
    float km, cm, mm, m;

    printf("Enter distance in Kelometer\n");
    scanf("%f", &km);

    m  = km * 1000.0;
    cm = km * 100000.0;
    mm = km * 1000000.0;

    printf("Distance in Meter is %f\n", m);
    printf("Distance in Centimeter is %f\n", cm);
    printf("Distance in Milimeter is %f\n", mm);

    return 0;
}

Output 1:
Enter distance in Kelometer
10
Distance in Meter is 10000.000000
Distance in Centimeter is 1000000.000000
Distance in Milimeter is 10000000.000000

Output 2:
Enter distance in Kelometer
5
Distance in Meter is 5000.000000
Distance in Centimeter is 500000.000000
Distance in Milimeter is 5000000.000000

PrefixSymbolMultiply by
yottaY1024
zettaZ1021
exaE1018
petaP1015
teraT1012
gigaG109
megaM106
hectokilohk105
myriama104
kilok103
hectoh102
dekada101
UNIT1100
decid10-1
centic10-2
millim10-3
decimillidm10-4
centimillicm10-5
microµ10-6
nanon10-9
picop10-12
femtof10-15
attoa10-18
zeptoz10-21
yoctoy10-24

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

Perimeter of Rectangle: C Program

The length and breadth / width of a rectangle are entered through the keyboard. Write a program to calculate the perimeter of the rectangle.

To get perimeter of a rectangle, we add the length and width of rectangle and then multiply it with 2. In other words, perimeter is the addition of length of all the sides of a rectangle.

Related Read:
Basic Arithmetic Operations In C
Area of Rectangle: C Program

Note: Perimeter of a Rectangle is calculated using the formula
Perimeter = 2 * (Length + Width);


perimeter of rectangle

Length: is the length of the longest side of the rectangle.
width: is the length of the smallest side of the rectangle.

Find Perimeter of a Rectangle: C Program


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

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


Source Code: Find Perimeter of a Rectangle: C Program

#include < stdio.h >

int main()
{
    float length, width, perimeter;

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

    printf("Enter width of Rectangle\n");
    scanf("%f", &width);

    perimeter = 2 * (length + width);

    printf("Perimeter of Rectangle is %f\n", perimeter);

    return 0;
}

Output:
Enter length of Rectangle
12
Enter width of Rectangle
6
Perimeter of Rectangle is 36.000000

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