C Program to Find First and Last Digit of a Number

Write a C program to find first and last digit of the user input number, without using looping.

Related Read:
Basic Arithmetic Operations In C

Source Code: C Program to Find First and Last Digit of a Number

 
#include < stdio.h >
#include < math.h >

int main()
{
    int num, first, last, count;

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

    count = log10(num);

    first = num / pow(10, count);
    last  = num % 10;

    printf("First Digit = %d\nLast Digit = %d\n", first, last);

    return 0;
}

Output 1:
Enter an integer number
123
First Digit = 1
Last Digit = 3

Output 2:
Enter an integer number
123456
First Digit = 1
Last Digit = 6

Output 3:
Enter an integer number
15937
First Digit = 1
Last Digit = 7

Output 4:
Enter an integer number
5986
First Digit = 5
Last Digit = 6

Output 5:
Enter an integer number
964801
First Digit = 9
Last Digit = 1

C Program to Find First and Last Digit of a Number


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

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


Logic To Find First and Last Digits of a Number

If user enters a number 123. i.e., num = 123; Then num % 10 would give the last digit of the number i.e., 3. To get first digit, we need to know the number of digits present in the number. To get that we make use of a built-in method called log10(). log10() returns the number of digits present in the number minus 1. That is because, it starts the count from 0. So if num = 123, log10(num) will return 2 and not 3. We store the number of digits inside variable count.

Now we use pow() method and calculate 10 to the power of count(value present in variable count) i.e., pow(10, count); We divide the user entered number by pow(10, count); to get the first digit of the number.
i.e., First_Digit = num / pow(10, count);

Note: Both pow() and log10() are built-in methods present in header file math.h So we include that library file at the beginning of our C program source code.

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 Factorial of a Number

Write a C program to find Factorial of a user input number, using while loop.

Related Read:
while loop in C programming
Assignment Operators in C

Factorial of a number is the product of all the numbers preceding it. For example, Factorial of 5 is 120 (1 x 2 x 3 x 4 x 5 = 120).

In general, n objects can be arranged in n(n – 1)(n – 2) … (3)(2)(1) ways. This product is represented by the symbol n!, which is called n factorial. By convention, 0! = 1.

Source Code: C Program To Find Factorial of a Number

 
#include<stdio.h>

int main()
{
    long int num, count = 1, fact = 1;

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

    while(count <= num)
    {
        fact = fact * count; // fact *= count;
        count++;
    }

    printf("Factorial of %d is %d\n", num, fact);

    return 0;
}

Output 1:
Enter a number to find factorial
4
Factorial of 4 is 24

Output 2:
Enter a number to find factorial
5
Factorial of 5 is 120

Output 3:
Enter a number to find factorial
6
Factorial of 6 is 720

Output 4:
Enter a number to find factorial
8
Factorial of 8 is 40320

Output 5:
Enter a number to find factorial
10
Factorial of 10 is 3628800

C Program To Find Factorial of a Number using While Loop


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

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


Logic To Find Factorial of a Number

If user enters num as 4. Then here are the values of variable count and fact for each iteration of while loop.

Iteration 1
count = 1;
fact = 1;

fact = fact * count;
fact = 1 * 1;

count = 2; // value of count increments by 1 for each iteration.
fact = 1;

Iteration 2
count = 2;
fact = 1;

fact = fact * count;
fact = 1 * 2;

count = 3; // value of count increments by 1 for each iteration.
fact = 2;

Iteration 3
count = 3;
fact = 2;

fact = fact * count;
fact = 2 * 3;

count = 4; // value of count increments by 1 for each iteration.
fact = 6;

Iteration 4
count = 4;
fact = 6;

fact = fact * count;
fact = 6 * 4;

count = 5; // value of count increments by 1 for each iteration.
fact = 24;

Now the value of count is 5, which is greater than the user input number 4. So the control exits while loop. We print the value present inside variable fact as the Factorial of the number. So in this case, 24 is the Factorial of number 4.

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 Factors of a Number

Write a C program to display Factors of user entered number. All the numbers which perfectly divide a given number are called as Factors of that number.

For Example, if user enters integer number 500. All the numbers which perfectly divide the number 500 are called Factors of number 500.

Related Read:
Basic Arithmetic Operations In C
while loop in C programming

Logic To Find Factors of a Number

We ask the user to enter a integer number. Next we iterate through the while loop until the count is less than or equal to the user entered number. Example, if user entered number is 50, then we iterate through the loop for 50 times. Each time we check if the user entered number is perfectly divisible by the value of count. Initial value of count is 1 and after each iteration of the loop count value increments by 1. Whenever a number perfectly divides the user entered number, we display it as factor of the user entered number.

Source Code: C Program to Find Factors of a Number

#include<stdio.h>

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

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

    printf("Factors of %d are:\n", num);

    while(count <= num)
    {
        if(num % count == 0)
        {
            printf("%d\n", count);
        }
        count++;
    }

    return 0;
}

Output 1:
Enter a number
50
Factors of 50 are:
1
2
5
10
25
50

Output 2:
Enter a number
200
Factors of 200 are:
1
2
4
5
8
10
20
25
40
50
100
200

Output 3:
Enter a number
400
Factors of 400 are:
1
2
4
5
8
10
16
20
25
40
50
80
100
200
400

Output 4:
Enter a number
500
Factors of 500 are:
1
2
4
5
10
20
25
50
100
125
250
500

C Program to Find Factors of a Number using While Loop


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

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


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

Find First and Second Biggest in N Numbers, without using Arrays: C Program

Write a C program to find first and second biggest numbers in list of N numbers without using arrays and without sorting the list and by using while loop.

Related Read:
while loop in C programming
Find Biggest of N Numbers, without using Arrays: C Program

Source Code: Find First and Second Biggest in N Numbers, without using Arrays: C Program

 
#include<stdio.h>

int main()
{
    int limit, num, fbig = 0, sbig = 0;

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

    printf("Enter %d positive numbers\n", limit);
 

    while(limit > 0)
    {
        scanf("%d", &num);

        if(num > fbig)
        {
            sbig = fbig;
            fbig = num;
        }
        if(num > sbig && num < fbig)
        {
            sbig = num;
        }

        limit--;
    }

    printf("First Big is %d\n", fbig);
    printf("Second Big is %d\n", sbig);

    return 0;
}

Output 1:
Enter the limit
5
Enter 5 numbers
1
2
3
4
5
First Big is 5
Second Big is 4

Output 2:
Enter the limit
5
Enter 5 numbers
5
4
3
2
1
First Big is 5
Second Big is 4

Output 3:
Enter the limit
5
Enter 5 numbers
1
4
3
5
2
First Big is 5
Second Big is 4

Output 4:
Enter the limit
8
Enter 8 numbers
1
5
9
3
7
4
6
8
First Big is 9
Second Big is 8

Find First and Second Biggest in N Numbers, without using Arrays: C Program


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

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


Logic To Find First and Second Biggest Number in N Numbers, without using Arrays

First we ask the user to enter length of numbers list. If user enters limit value as 5, then we ask the user to enter 5 numbers. Once the user enters limit value, we iterate the while loop until limit value is 0. For each iteration of while loop we ask the user to enter a integer number. We check if the new number entered by the user is greater than fbig. If true, we swap the value of fbig to sbig and value of num to fbig. If the new number entered by the user is not greater than fbig, then we check if its greater than sbig. If true, we transfer the value of num to sbig.

Once the value of limit is 0, control exits the while loop, and inside fbig we have first biggest number from the list of numbers entered by the user and sbig will have the second biggest number from the list of numbers entered by the user.

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

Find Biggest of N Numbers, without using Arrays: C Program

Write a C program to find biggest of N numbers without using Arrays and using while loop.

Related Read:
while loop in C programming

Logic To Find Biggest Number, without using Arrays

We ask the user to enter the limit. i.e., the length of the list of numbers. For Example, if user enters limit value as 5, then we accept 5 numbers from the user and then find the biggest and output that number on to the console window.

First we ask the user to enter the limit. If user enters limit value as 5, we iterate the while loop 5 times i.e., until value of limit is 0. Inside the while loop, for each iteration we ask the user to enter a number. Next we check if that user entered number is greater than the value present in variable big. If the new number entered by the user is greater than value present in variable big, then we copy assign the new number entered by the user to the variable big. We keep doing this for each number entered by the user. Simultaneously we keep decrementing the value of variable limit by one for each iteration of while loop. Once limit is 0, control exits the while loop and we print the value present inside the variable big.

Source Code: Find Biggest of N Numbers, without using Arrays: C Program

 
#include<stdio.h>

int main()
{
    int limit, num, big = 0;

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

    printf("Enter %d numbers\n", limit);

    scanf("%d", &num);
    big = num;
    limit = limit - 1;

    while(limit > 0)
    {
        scanf("%d", &num);
        if(num > big)
        {
            big = num;
        }
        limit--;
    }

    printf("Biggest number is %d\n", big);

    return 0;
}

Output 1:
Enter the limit
5
Enter 5 numbers
1
2
3
4
5
Biggest number is 5

Output 2:
Enter the limit
5
Enter 5 numbers
5
4
3
2
1
Biggest number is 5

Output 3:
Enter the limit
5
Enter 5 numbers
1
2
5
3
4
Biggest number is 5

Output 4:
Enter the limit
10
Enter 10 numbers
1
5
9
3
7
5
6
14
1
1
Biggest number is 14

Find Biggest of N Numbers, without using Arrays: C Program


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

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


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