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

Positive or Negative or Zero Using Ternary Operator: C Program

C Program to check whether the user entered integer number is positive, negative or zero using ternary operator or Conditional operator.

Related Read:
Number is Positive or Negative or Zero: C Program

Check Whether Number Is Positive or Negative or Zero Using Ternary Operator: C Program


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

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


Note:
Any number greater than 0 is positive.
Any number less than 0 is negative.

Check Whether Number is Positive or Negative or Zero: C Program

#include < stdio.h >

int main()
{
    int a;

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

    (a > 0) ?
    (printf("%d is positive\n", a)) :
    ( (a < 0) ?
      (printf("%d is Negative\n", a)) :
      (printf("%d is Zero\n", a))  
    );

    return 0;
}

Output 1:
Enter an integer number
15
15 is positive

Output 2:
Enter an integer number
-2
-2 is negative

Output 3:
Enter an integer number
0
0 is zero

In above C source code, we are using nested ternary / conditional operator. First we check if a is greater than 0, if its true then the user entered number is positive. If its false, then we check if a is less than 0 using nested ternary / conditional operator. If that is true, then a is negative, else the user entered number is 0.

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

Number is Positive or Negative or Zero: C Program

C Program to check whether the user entered integer number is positive, negative or zero using else if construct.

Note:
Any number greater than 0 is positive.
Any number less than 0 is negative.

Check Whether Number is Positive or Negative or Zero: C Program


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

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


Check Whether Number is Positive or Negative or Zero: C Program

#include < stdio.h >

int main()
{
    int a;

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

    if(a > 0)
    {
        printf("%d is positive\n", a);
    }
    else if(a < 0)
    {
        printf("%d is negative\n", a);
    }
    else
    {
        printf("%d is zero\n", a);
    }

    return 0;
}

Output 1:
Enter an integer number
15
15 is positive

Output 2:
Enter an integer number
-2
-2 is negative

Output 3:
Enter an integer number
0
0 is zero

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

Biggest of Two Numbers Using Ternary Operator: C

Lets find biggest of 2 numbers using ternary operator / conditional operator.

Related Read:
Ternary Operator / Conditional Operator In C

To find biggest of Two numbers using if-else control structure:
Biggest of Two Numbers: C

Source Code: Biggest of Two Numbers using ternary operator: C

 
#include < stdio.h >

int main()
{
    int a, b, big;

    printf("Enter 2 numbers\n");
    scanf("%d%d", &a, &b);

    (a > b) ? (big = a) : (big = b);

    printf("Biggest of %d and %d is %d\n", a, b, big);

    return 0;
}

Output 1:
Enter 2 numbers
5
6
Biggest of 5 and 6 is 6

Output 2:
Enter 2 numbers
40
15
Biggest of 40 and 15 is 40

 
#include < stdio.h >

int main()
{
    int a, b, big;

    printf("Enter 2 numbers\n");
    scanf("%d%d", &a, &b);

    big = (a > b) ? (a) : (b);

    printf("Biggest of %d and %d is %d\n", a, b, big);

    return 0;
}

Output 1:
Enter 2 numbers
500
900
Biggest of 500 and 900 is 900

In above source code, if a is bigger than b, then value of a is returned and stored in variable big orelse value of variable b is stored in variable big.

General Form of Ternary Operator

(expression_1) ? (expression_2) : (expression_3);

expression_1 is a comparison/conditional argument. expression_2 is executed/returned if expression_1 results in true, expression_3 gets executed/returned if expression_1 is false.

Biggest of 2 Numbers Using Ternary Operator: C


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

YouTube Link: https://www.youtube.com/watch?v=TB11OWMyiR8 [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

Division of 2 Numbers: C

In this video tutorial you can learn the procedure followed in C programming to divide two numbers.

 
#include < stdio.h >

int main()
{
    int a, b, c;

    printf("Enter 2 numbers for division\n");
    scanf("%d %d", &a, &b);

    c = a / b;

    printf("Division of %d and %d is %d\n", a, b, c);

    return 0;
}

Output:
Enter 2 numbers for division
10
5
Division of 10 and 5 is 2

You can write same program without using third variable to calculate division of 2 numbers, as below:

 
#include < stdio.h >

int main()
{
    int a, b;

    printf("Enter 2 numbers for division\n");
    scanf("%d %d", &a, &b);

    printf("Division of %d and %d is %d\n", a, b, (a/b));

    return 0;
}

Output:
Enter 2 numbers for division
10
5
Division of 10 and 5 is 2

Note: Instead of int you can take float variables too. That would help in taking both integer as well as real values from the user as input.

Division by 0

What if a user enters 0 for variable b. Any number divided by 0 will give undefined return. So lets handle the scenario using Decision Control Instruction In C: IF and inequality operator.

 
#include < stdio.h >

int main()
{
    int a, b;

    printf("Enter value for a and b, for division (a/b)\n");
    scanf("%d%d", &a, &b);

    if( b != 0 )
       printf("Division of %d and %d is %d\n", a, b, a/b);

    if( b == 0 )
       printf("You can't divide a number by 0\n");

    return 0;
}

Output 1:
Enter value for a and b, for division (a/b)
10
2
Division of 10 and 2 is 5

Output 2:
Enter value for a and b, for division (a/b)
10
0
You can’t divide a number by 0

Output 3:
Enter value for a and b, for division (a/b)
0
5
Division of 0 and 5 is 0

In above program, != is inequality operator. It returns true(non-zero number) if the operands are not equal. == is equality operator. It returns true(non-zero number) if the operands are equal.

Scanf(): For user input

In above c program we are asking user to enter the values for variable a and b. You can know more about scanf() method/function in this video tutorial: Using Scanf in C Program

Division of Two Numbers: C Programming


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

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


This video was produced as building block for our simple calculator application.

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert