C Program To Check Repetition of Digit In A Number using Arrays

Lets write a C program to check if any digit in a user input number appears more than once.

Note: Any positive integer number can be formed using only 0-9 digits. So we take an array with length 10. i.e., 0 to 9

array of size 10

Video Tutorial: C Program To Check Repetition of Digit In A Number using Arrays


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

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

Source Code: C Program To Check Repetition of Digit In A Number using Arrays

#include<stdio.h>

int main()
{
    int a[10] = {0}, num, rem;

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

    while(num)
    {
        rem = num % 10;

        if(a[rem] == 1)
            break;
        else
            a[rem] = 1;

        num = num / 10;
    }

    if(num)
        printf("There are repetition of digits in the number\n");
    else
        printf("There are no repetition of digits in the number\n");

    return 0;
}

Output 1:
Enter a positive number
123
There are no repetition of digits in the number

array of size 10

Output 2:
Enter a positive number
156
There are no repetition of digits in the number

array of size 10

Output 3:
Enter a positive number
1232
There are repetition of digits in the number

Logic To Check if any digit in user input number repeats or not

1. Since there are 10 digits i.e., 0 to 9 to form any number, we take array size as 10. We initialize all the elements of array to 0.

Related Read:
Basics of Arrays: C Program

2. We ask the user to input a positive number.

3. We iterate through the while loop until num is zero.

Related Read:
while loop in C programming

4. By modulo dividing user input number by 10, we fetch individual digits of number. We make use of this individual digit as index of the array. We over-write the initial value(which is zero) and assign 1 at that index position.

So presence of value 1 at an index specifies that the digit already exists in the number.

Related Read:
Modulus or Modulo Division In C Programming Language

5. So based on the presence of value 1 or 0 at particular index, our program decides if the digit is present more than once in a number or not.

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 Check whether a Number is Strong Number or Not

Lets write a C program to check whether user entered number is strong number or not, using nested while loop.

Strong Number: Sum of factorial of a number’s individual digits should be equal to the number itself. Such a number is called Strong Number.

For Example: If user entered number is 145. We find factorial of individual digits of 145 and add it.
i.e., !1 + !4 + !5

(1) + (24) + (120) = 145

So the original number and the sum of factorials of individual digits are both 145. So number 145 is considered as a Strong Number.

Related Read:
Basic Arithmetic Operations In C
while loop in C programming
Nested While Loop: C Program

Important Topics
Calculate Sum of Digits: C Program
C Program To Find Factorial of a Number

Source Code: C program To Check whether a Number is Strong Number or Not

#include < stdio.h >

int main()
{
    int num, temp, rem, count, fact, sum = 0;

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

    temp = num;

    while(num)
    {
        rem = num % 10;

        count = 1;
        fact  = 1;
        while(count <= rem)
        {
            fact = fact * count;
            count++;
        }

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

        sum = sum + fact;

        num = num / 10;
    }

    if(temp == sum)
    {
        printf("%d is a strong number\n", temp);
    }
    else
    {
        printf("%d is not a strong number\n", temp);
    }

    return 0;
}

Output 1
Enter a number
145
Factorial of 5 is 120
Factorial of 4 is 24
Factorial of 1 is 1
145 is a strong number

Output 2
Enter a number
140
Factorial of 0 is 1
Factorial of 4 is 24
Factorial of 1 is 1
140 is not a strong number

Video Tutorial: C Program To Find Strong Number


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

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

Logic To Check whether a Number is Strong Number or Not

If user entered number is 145. i.e., num = 145. Using outer while loop we keep fetching digits one by one by using below code.

rem = num % 10;
num = num / 10;

For each iteration the outer loop fetches individual digits of the number. The inner while loop calculates the factorial for that fetched individual digit. Next we keep adding the factorial of each individual digit.

Once the control exits outer while loop we check if the user entered number is equal to the value present in variable sum. If true, then the user entered number is a strong number, if not, its not a strong number.

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 Calculate Generic Root of a Number using Ternary Operator

Lets write a C program to calculate Generic Root of a Number using Ternary Operator or Conditional Operator.

Related Read:
C Program to Find Generic Root of a Number
Ternary Operator / Conditional Operator In C

For Example: If user input number is 65987, then we add all the individual digits of the number i.e., 6 + 5 + 9 + 8 + 7 = 35. We got 35. Now we add individual digits of number 35 i.e., 3 + 5 = 8. So Generic Root of number 65987 is 8.

Source Code: C Program to Calculate Generic Root of a Number using Ternary Operator

 
#include < stdio.h >

int main()
{
    int num, res;

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

    printf("Generic Root of %d is %d\n", num, (res = num % 9) ? res : 9 );

    return 0;
}

Output 1:
Enter a number above 10
65987
Generic Root of 65987 is 8

Output 2:
Enter a number above 10
8
Generic Root of 8 is 8

Output 3:
Enter a number above 10
456
Generic Root of 456 is 6

Output 4:
Enter a number above 10
78910
Generic Root of 78910 is 7

Output 5:
Enter a number above 10
5555
Generic Root of 5555 is 2

C Program to Calculate Generic Root of a Number using Ternary Operator


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

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


Logic To Find Generic Root of a Number using Ternary Operator

(result = num % 9) ? result : 9;

OR

(num % 9) ? (num % 9) : 9;

We divide the user input number by 9 and store the remainder inside variable result. If the number is perfectly divisible by 9 or if the result is zero then we output 9 orelse we output the value present in variable result. Variable result will have the Generic Root of the user entered number. We are applying modular division on the user entered number. And we are dividing it by 9 because 9 is the biggest single digit number and by modulo division of number by 9 we get the Generic Root of the number.

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 Generic Root of a Number

Lets write a C program to calculate Generic Root of a user input number.

Generic Root: of a number is sum of all the digits of the number until we get a single-digit output.

Related Read:
while loop in C programming
Calculate Sum of Digits: C Program

For Example: If user input number is 12345, then we add all the individual digits of the number i.e., 1 + 2 + 3 + 4 + 5 = 15. We got 15. Now we add individual digits of number 15 i.e., 1 + 5 = 6. So Generic Root of number 12345 is 6.

Source Code: C Program to Find Generic Root of a Number

 
#include < stdio.h >

int main()
{
    int num, temp, rem, sum = 0;

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

    temp = num;

    while(temp > 0)
    {
        rem  = temp % 10;
        sum  = sum + rem;
        temp = temp / 10;

        if(temp == 0)
        {
            if(sum > 9)
            {
                temp = sum;
                sum  = 0;
            }
        }
    }

    printf("Generic Root of %d is %d\n", num, sum);

    return 0;
}

Output 1:
Enter a number
12345
Generic Root of 12345 is 6

Output 2:
Enter a number
123
Generic Root of 123 is 6

Output 3:
Enter a number
15937
Generic Root of 15937 is 7

Output 4:
Enter a number
222222
Generic Root of 222222 is 3

Output 5:
Enter a number
99999
Generic Root of 99999 is 9

C Program to Calculate Generic Root of a Number


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

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


Logic To Find Generic Root of a Number

If user entered number is 1234.
Num = 1234;
temp = 1234;
sum = 0;

Iteration 1
rem = temp % 10;
rem = 1234 % 10;
rem = 4;

sum = sum + rem
sum = 0 + 4;
sum = 4;

temp = temp / 10;
temp = 1234 / 10;
temp = 123;

Iteration 2
rem = temp % 10;
rem = 123 % 10;
rem = 3;

sum = sum + rem
sum = 4 + 3;
sum = 7;

temp = temp / 10;
temp = 123 / 10;
temp = 12;

Iteration 3
rem = temp % 10;
rem = 12 % 10;
rem = 2;

sum = sum + rem
sum = 7 + 2;
sum = 9;

temp = temp / 10;
temp = 12 / 10;
temp = 1;

Iteration 4
rem = temp % 10;
rem = 1 % 10;
rem = 1;

sum = sum + rem
sum = 9 + 1;
sum = 10;

temp = temp / 10;
temp = 1 / 10;
temp = 0;

Now inside while loop we keep checking for the condition temp = 0. Once this condition is true, we check if the value present in sum is greater than 9. i.e., we check if variable sum has double digit number. If that is true, then we copy the number present in variable sum to variable temp and we assign 0 to sum. Now the while loop continues until the variable sum has single digit number.

Once variable sum has single digit number in it, control exits the while loop and we out put the value of sum as Generic Root of the user entered number.

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