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

Recommended Answers

All 5 Replies

Post what you've got.

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

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

nevermind got it
THanx all for your help :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.