hey,
um i was wondering if anyone could help me with a program where if the user inputs a number no more than 5 digits and the program tells you how many times the number 2 appears in that number.
i know that the modulus and division have to be used but i dont know how to set that part up
can someone help??
thanxxxx

get it from the keyboard as a string instead of an integer, then create a loop to check each digit in the string. You don't need modules, division, shifts, or any other math operations, unless, of course, that is a requirement of the problem.

#include <iostream>
using namespace std;
int main()
{
int num1;
int count;

cout<<" Enter an integer :"<<endl;
cin>>num1;

if(num1%10 )==2)
 count+=1

this gave me the last digit it worked but i duno how to get the rest of the digits ( it cant be more than 5 digits so i have to do it 5 times

Edited 3 Years Ago by happygeek: fixed formatting

Try this bit of code:

// Loop until the number becomes 0
while(num1 != 0)
{
	// Check for a 2
	if((num1 % 10) == 2) count++;
	
	// Chop off the last digit
	num1 /= 10;
}
This question has already been answered. Start a new discussion instead.