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

Fibonacci Series using While loop: C Program

Today lets see how to generate Fibonacci Series using while loop in C programming.

First Thing First: What Is Fibonacci Series ?
Fibonacci Series is a series of numbers where the first two Fibonacci numbers are 0 and 1, and each subsequent number is the sum of the previous two. Its recurrence relation is given by Fn = Fn-1 + Fn-2.

Below are a series of Fibonacci numbers(10 numbers):
0
1
1
2
3
5
8
13
21
34

How Its Formed:
0 <– First Number (n1)
1 <– Second Number (n2)
1 <– = 1 + 0
2 <– = 1 + 1
3 <– = 2 + 1
5 <– = 3 + 2
8 <– = 5 + 3
13 <– = 8 + 5
21 <– = 13 + 8
34 <– = 21 + 13

Generate Fibonacci Series using While loop: C Program


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

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


Source Code: Fibonacci Series using While loop: C Program

#include < stdio.h >

int main()
{
    int n1 = 0, n2 = 1, n3, count;

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

    printf("\n%d\n%d\n", n1, n2);

    count = count - 2;

    while(count)
    {
        n3 = n1 + n2;
        printf("%d\n", n3);
        n1 = n2;
        n2 = n3;
        count = count - 1;
    }


    return 0;
}

Output:
Enter the limit
10

0
1
1
2
3
5
8
13
21
34

Logic to Generate Fibonacci Series in C Programming Language

We know that the first 2 digits in fibonacci series are 0 and 1. So we directly initialize n1 and n2 to 0 and 1 respectively and print that out before getting into while loop logic. Since we’ve already printed two fibonacci numbers we decrement the value of variable count by 2.

Inside while loop
We already know that C program treat any non-zero number as true and zero as false. So once the value of variable count is zero, the execution of while loop stops. So the condition while(count) is equal to writing while(count == 0). We decrement the value of count by 1 each time the loop executes. So once count is 0 the loop execution stops.

As per definition of Fibonacci series: “..each subsequent number is the sum of the previous two.” So we add n1 and n2 and assign the result to n3 and display the value of n3 to the console.

Next, we step forward to get next Fibonacci number in the series, so we step forward by assigning n2 value to n1 and n3 value to n2.

While loop keeps repeating above steps until the count is zero. If the user entered limit/count as 10, then the loop executes 8 times(as we already printed the first two numbers in the fibonacci series, that is, 0 and 1).

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

Programming Interview / Viva Q&A: 5 (while loop)

In this video tutorial lets understand the while loop a little better. We’ve posted source code of a C program below which has while loop. You need to guess the output of the program.

Related Read:
while loop in C programming

Guess output of the program

 

int main()
{
    int count = 0;

    printf("Start of program\n");

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

    printf("End of program\n");

    return 0;
}

Output:
Start of program

Since above C source code has semicolon(;) immediately after while(count <= 5) the control enters infinite loop or indefinite loop, and no other statements after the semicolon gets executed. And the program doesn’t get terminated with return 0.

C Programming Interview / Viva Q&A: 5 (While Loop)


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

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


If you remove the semicolon(;) after while(count <= 5) then the program will output numbers from 0 to 5 and the “End of program” statement.

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

while loop in C programming

So far we’ve seen sequential flow of our programs. Next we saw decision control statements like if, if else, else if etc. Now lets learn about loop control statements present in C programming language.

In this video tutorial lets learn about while loop in particular.

Related Read:
Programming Interview / Viva Q&A: 5 (Infinite or Indefinite while loop)

General Syntax of while loop

 
#include < stdio.h >

int main()
{
    while(pre_test_condition)
      statement1;

    return 0;
}

Here while is the keyword. Inside parenthesis we need to write the condition. These conditions must evaluate to Boolean value. i.e., true(non-zero number) or false(0);

The ‘body of while’ loop keeps executing until the condition is true. Control exits while loop once the condition is false.

 
#include < stdio.h >

int main()
{
    while(pre_test_condition)
   {
      statement1;
      statement2;
   }
    return 0;
}

Enclose the statements with curly braces if there are multiple statements within while loop or body of while loop.

while loop in C programming Language


[youtube https://www.youtube.com/watch?v=2Mkc80-uqrs]

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


Example Source Code: while loop

 
#include < stdio.h >

int main()
{
    int count = 1;

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

    printf("End of loop\n");

    return 0;
}

Output:
1
2
3
4
5
End of loop

In above C source code, variable count is initialized to 1. Inside while condition we check if variable count is less than or equal to 5. Until variable count is less than or equal to 5, the while loop keeps executing. Inside body of the while loop we increment the value of count by 1 upon each execution of the loop. The condition is checked on execution of the loop. Once variable count is more than 5, the condition becomes false count <= 5 (once count value is 6, the condition becomes false), the execution control exits while loop.

Note: Here the loop execution counter is called ‘loop counter’ or ‘index variable’.

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