C Program To Count Each Digit In A Number using Arrays

Lets write a C program to count repetition of each digit in a positive integer number using array.

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

Example: Expected Output

Enter a positive number
11201

0 has appeared 1 times.
1 has appeared 3 times.
2 has appeared 1 times.

Digit Count Using Array

Video Tutorial: C Program To Count Each Digit In A Number using Arrays



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

Source Code: C Program To Count Each Digit In A Number using Arrays

Method 1

#include<stdio.h>

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

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

    while(num)
    {
        rem = num % 10;
        a[rem] = a[rem] + 1;
        num = num / 10;
    }

    printf("\n");

    for(i = 0; i < 10; i++)
    {
        if(a[i] != 0)
            printf("%d has appeared %d times.\n", i, a[i]);
    }

    return 0;
}

Output 1:
Enter a positive number
1105135

0 has appeared 1 times.
1 has appeared 3 times.
3 has appeared 1 times.
5 has appeared 2 times.

Output 2:
Enter a positive number
12345

1 has appeared 1 times.
2 has appeared 1 times.
3 has appeared 1 times.
4 has appeared 1 times.
5 has appeared 1 times.

Logic To Count Each Digit In A Number

We ask the user to enter a positive integer number. We then fetch individual digits from the number using while loop.

Important Note:
1. Initially all the array elements are initialized to zero.
2. We take array size as 10. Because we need to accommodate 10 digits i.e., 0 to 9. Using digits 0 to 9 we could formulate any positive integer number.

Array with zeros initialized

Inside While loop
While loop keeps executing until num is 0. Once num is 0, control exits while loop. First we fetch the last digit of the user input number by modulo dividing it by 10, and store it inside variable rem. Next we use this rem value as index of array variable a. i.e., a[rem] and add 1 to the previous value of a[rem]. Next we reduce the user input number by 1 digit from the end by dividing the number by 10. i.e., num = num / 10. This line of code shifts the decimal point from right to left by 1 place. But since num is integer variable and 10 is also integer(by which we divide user input number), we only get the integer part of the number and the number after decimal point gets discarded.

Printing / Displaying The Result or The Count
We iterate through the entire array and display the non-zero values along with the index number. Index number is nothing but the individual digits of the user input number.

Explanation With Example

If num = 112021;
So individual digits are 1, 1, 2, 0, 2, 1

    while(num)
    {
        rem = num % 10;
        a[rem] = a[rem] + 1;
        num = num / 10;
    }
num = num / 10rem = num % 10a[rem] = a[rem] + 1
1120211a[1] = 1
112022a[2] = 1
11200a[0] = 1
1122a[2] = 2
111a[1] = 2
11a[1] = 3
0

When the control exits while loop (once num is 0), a[0] has value 1, a[1] has value 3, a[2] has value 2. That simply means, digit 0 has appeared 1 time in the user input number. Digit 1 has appeared 3 times in the user input number. Digit 2 has appeared 2 times in the user input number.

Source Code: C Program To Count Each Digit In A Number using Arrays

Another method: Method 2

#include<stdio.h>

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

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

    temp = num;

    while(num)
    {
        rem = num % 10;
        a[rem] = a[rem] + 1;
        num = num / 10;
    }

    printf("\n");

    while(temp)
    {
        rem = temp % 10;

        if(a[rem] != 0)
            printf("%d appeared %d time.\n", rem, a[rem]);

        a[rem] = 0;
        temp = temp / 10;
    }

    return 0;
}

Output 1:
Enter a positive number
11253001

0 has appeared 2 times.
1 has appeared 3 times.
2 has appeared 1 times.
3 has appeared 1 times.
5 has appeared 1 times.

Output 2:
Enter a positive number
112021

0 has appeared 1 times.
1 has appeared 3 times.
2 has appeared 2 times.

Logic To Count Each Digit In A Number: Method 2

Here the program is same except the displaying part logic.

    while(temp)
    {
        rem = temp % 10;

        if(a[rem] != 0)
            printf("%d appeared %d time.\n", rem, a[rem]);

        a[rem] = 0;
        temp = temp / 10;
    }

variable temp has user input number. Here we fetch individual digit of the user input number, and if it is non-zero, then we print whatever value is present at that index. Once we print the value, we over-write the value at that index by 0. Next we reduce the number by one digit from left/end by dividing the number by 10.

This way we only iterate through the while loop by limited number of times. i.e., The number of iteration is equal to the number of unique digits the user input number has.

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

Positive or Negative or Zero Using Macros: C Program

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

Related Read:
Positive or Negative or Zero Using Ternary Operator: C Program

Logic

If user input number is greater than 0, then the number is positive. If user input number is less than 0, then the number is negative. If neither of the above 2 conditions are true, then the number is zero.

Video Tutorial: Positive or Negative or Zero Using Macros: C Program



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

Source Code: Positive or Negative or Zero Using Macros: C Program

#include<stdio.h>

#define SIGN(num) ( (num > 0) ? \
                   printf("POSITIVE") : \
                   (num < 0) ? printf("NEGATIVE") : \
                   printf("ZERO"))

int main()
{
    int num;

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

    SIGN(num);

    return 0;
}

Output 1:
Enter a number
5
POSITIVE

Output 2:
Enter a number
-2
NEGATIVE

Output 3:
Enter a number
0
ZERO

Here the macro template SIGN(num) will be replaced by its corresponding macro expansion ( (num > 0) printf(“POSITIVE”) : (num < 0) ? printf(“NEGATIVE”) : printf(“ZERO”)) by preprocessor. And if user input number is greater than 0, it’ll print POSITIVE else it’ll print NEGATIVE. If neither of those 2 conditions are true, then it’ll print ZERO.

In our program we’re using Macro Continuation (\) Preprocessor Operator: C Program in macro expansion to break from the line and continue the logic in new/next line.

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 Draw Pyramid of Numbers, using For Loop

Lets write a C program to draw / display / print a four row pyramid formed from numbers 1 to 10.

Related Read:
Nested For Loop In C Programming Language
C Program To Draw Pyramid of Numbers, using While Loop

Video Tutorial: C Program To Draw Pyramid of Numbers, using For Loop



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

Source Code: C Program To Draw Pyramid of Numbers, using For Loop

#include<stdio.h>
int main()
{
    int num = 4, row, col, space, count = 1;
    float i = -7;


    for(row = 1; row <= num; row++)
    {
        for(space = i; space <= (num-row); space++)
        {
            printf(" ");
        }
        for(col = 1; col <= row; col++)
        {
            printf(" %d ", count++);
        }
        i += 0.8;

        printf("\n");
    }

    return 0;
}

Output

                    1
                  2  3
                 4  5  6
               7  8  9  10

Logic To Draw Pyramid of Numbers, using For Loop

Here we already know that the Pyramid we need to print has 4 rows and is formed of numbers 1 to 10.

Outer for loop selects the row number and the number of elements to be printed for that particular selected row. For example, 1st row has 1 element. 2nd row has 2 elements. 3rd row has 3 elements and so on. So the row number and the number of elements in that particular row are always the same.

First inner for loop prints the adequate spacing required for the pyramid. Second inner for loop prints the actual numbers.

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 For Alphabet, Number and Special Symbol

Any character is entered through the keyboard, write a C program to determine whether the character entered is a capital letter, a small case letter, a digit or a special symbol.

The following table shows the range of ASCII values for various characters:
Character A – Z : ASCII Value 65 – 90
Character a – z : ASCII Value 97 – 122
Character 0 – 9 : ASCII Value 48 – 57
Special Symbol : ASCII Value 0 – 47, 58 – 64, 91 – 96, 123 – 127

ascii codes

Related Read:
else if statement in C
Relational Operators In C
C Program To Print All ASCII Characters and Code

Expected Output for the Input

User Input:
Enter a Character
$

Output:
$ is a Special Character

Video Tutorial: C Program To Check For Alphabet, Number or Special Symbol



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

Source Code: C Program To Check For Alphabet, Number and Special Symbol

#include<stdio.h>

int main()
{
    char ch;

    printf("Enter a Character\n");
    scanf("%c", &ch);

    if(ch >= 65 && ch <= 90)
    {
        printf("%c is an Uppercase Alphabet\n", ch);
    }
    else if(ch >= 97 && ch <= 122)
    {
        printf("%c is an lowercase Alphabet\n", ch);
    }
    else if(ch >= 48 && ch <= 57)
    {
        printf("%c is a Number\n", ch);
    }
    else if( (ch >= 0  && ch <= 47) ||
             (ch >= 58 && ch <= 64) ||
             (ch >= 91 && ch <= 96) ||
             (ch >= 123 && ch <= 127))
    {
        printf("%c is a Special Character\n", ch);
    }

    return 0;
}

Output 1:
Enter a Character
A
A is an Uppercase Alphabet

Output 2:
Enter a Character
i
i is an lowercase Alphabet

Output 3:
Enter a Character
8
8 is a Number

Output 4:
Enter a Character
$
$ is a Special Character

Logic To Check For Alphabet, Number and Special Symbol

We use &&(AND) operator check check for range. i.e., for number, we check from the range 48 to 57. To check if the user entered character lies in this range we use (ch >= 48 && ch <= 57). To check for multiple ranges we use ||(OR) operator. For example, for special symbol:


(ch >= 0 && ch <= 47) ||
(ch >= 58 && ch <= 64) ||
(ch >= 91 && ch <= 96) ||
(ch >= 123 && ch <= 127)

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