Counting Num
by CodeChum Admin
To give you more of a challenge with a number's digits, let's try counting how many of a certain digit is present on a given number.
Let's start coding!
Instructions:
Input two integer values. The first one shall accept any integer from 0-9 and the other one shall take a non-zero positive integer.
Using a while loop, count how many of the first integer (0-9) is present in the digits of the second inputted integer and print the result (see sample input and output for example).
Tip #1: You have to use your knowledge from the previous problems in looping through the digits of a number: % 10 to get the rightmost digit, while / 10 to remove the rightmost digit. Make sure to solve the previous problems first.
Input
A line containing two integers separated by a space.
2·124218
Output
A line containing an integer.
2
#include<stdio.h>
int main(){
int number,digit,count=0;
printf("Enter the integer and a non-zero positive integer: ");
scanf("%d%d",&digit,&number);
while(number>0){
if (number % 10 == digit){
count +=1;
}
number = number/10;
}
printf("%d",count);
getchar();
getchar();
return 0;
}
Comments
Leave a comment