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

Area of Rectangle: C Program

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

Rectangle is a quadrilateral(i.e., it has 4 sides). And all the 4 angles of the rectangle are of 90 degree. To calculate area of a rectangle we need its length and width/breadth.

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

Note: Area of a Rectangle is calculated using the formula
Area = Length * Width;


area 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 Area of a Rectangle: C Program


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

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


Source Code: Find Area of a Rectangle: C Program

#include < stdio.h >

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

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

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

    area = length * width;

    printf("Area of Rectangle is %f\n", area);

    return 0;
}

Output:
Enter length of rectangle
12
Enter width of rectangle
6
Area of Rectangle is 72.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

Convert Fahrenheit To Degree Celsius: C Program

Temperature of a city in Fahrenheit degree is input through the keyboard. Write a program to convert this temperature into Degree Celsius / Centigrade.

Related Read:
Basic Arithmetic Operations In C
Convert Degree Celsius To Fahrenheit: C Program

Formula to Convert Fahrenheit To Celsius.

From our previous video tutorial Convert Degree Celsius To Fahrenheit: C Program we already know the formula to convert temperature from Celsius to Fahrenheit. We will use the same formula and convert it to calculate Degree celsius from Fahrenheit.

Fahrenheit = (Centigrade * 1.8) + 32;
(Fahrenheit – 31) = (Centigrade * 1.8);
(Fahrenheit – 31) / 1.8 = (Centigrade);

So we’ll use this formula Centigrade = (Fahrenheit – 31) / 1.8; to calculate Degree Centigrade by getting Fahrenheit value from the user.

Convert Fahrenheit To Degree Celsius: C Program


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

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


Source Code: Convert Fahrenheit To Degree Celsius: C Program

#include < stdio.h >

int main()
{
    float c, fh;

    printf("Enter temperature in Fahrenheit\n");
    scanf("%f", &fh);

    c = (fh - 32) / 1.8;

    printf("Temperature in Degree Celius is %f\n", c);

    return 0;
}

Output 1:
Enter temperature in Fahrenheit
212
Temperature in Degree Celius is 100.000000

Output 2:
Enter temperature in Fahrenheit
113
Temperature in Degree Celius is 45.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