Showing posts with label C Program Interview Questions. Show all posts
Showing posts with label C Program Interview Questions. Show all posts

Sunday, May 29, 2016

Print * based on occurrence of a Number in given Input

Program #38


Print * based on occurrence of a Number in Given Input. 
Note: This program was asked in Interview.  May-2016.

Example:

1600514367

Output:

0: **
1: **
2:
3: *
4: *
5: *
6: **
7: *
8:
9:


Source Code:

#include <stdio.h>
#include <string.h>
int main()
{
    int i, j;
   
    /* declare a string variable to get Input */
    char str[20];
   
    /* static array of all number to compare */
    char staticArray[]= {'0', '1','2', '3', '4', '5', '6', '7', '8', '9'};
   
    /* Get Input String */
    printf("Please enter a numbers to Parse!\n");
    scanf("%s", str);
   
    /* Iterate the 1st loop 9 times (0 to 9) */
    for (i=0; i<= 9; i++){
       
        /* print every number from 1 to 9 */
        printf("%d: ", i);
       
        /* Iterate the 2nd loop upto string length of Input */
        for (j=0; j<strlen(str); j++){
           
            /* Compare both the characters
               Note: Both are characters, So we can directly compare it using ==
            */
            if (staticArray[i] == str[j]){
               
                /* Print the * */
                printf("*");
            }
        }
       
        /* Move cursor to next line */
        printf("\n");
       
    }
   
    return 0;
}

Popular Posts