is it loop, remainder or what ?

hello, I'm having issues understanding what to do in this exercise.
it"s a C++ using netbeans

Write a program that asks the user to enter 2 integers of 4 digits
and displays the different digits as X and * if they are the same. If
the user enters a number that is not of four digits, then a
convenient message is displayed.

For example, if the user enters
1234
1432
Then the output should be: *X*X
Last edited on
The easiest way to do this would be to input the data as strings, then check to make sure there aren't non-digits in the string, then check to make sure the length of each string is 4.

Then, it's a simple loop where you output '*' if strA[i] == strB[i], else output 'X'.

isdigit checks if a character is a digit, use it in a loop: https://www.cplusplus.com/reference/cctype/isdigit/

Edit: s/look/loop
Last edited on
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
void shambles( int a, int b )
{
   if ( a >= 10 ) shambles( a / 10, b / 10 );
   std::cout << "X*"[ a % 10 == b % 10 ];
}

int main()
{
   shambles( 1234, 1432 );
}
thank you for answering me, but I didn't understand, I'm at the lowest beginner level
The easiest way, then, is to compare the integers as strings (Ganado's suggestion).

Otherwise you can peel digits off using / and % operators (there are actually quite a few ways of doing that if you know the integers have a specific number of digits) and compare them one by one.
ok then, I know how to peel off digits, but how to compare them after that, I mean how to make them variables to insert them in the (while, if, else) operation.
Amin96 wrote:
how to compare them after that


if ( a == b ) ....
ah okay okay, thank you, I'll come back with the results to check with you.
If I succeded for sure.

package javaapplication74;

import java.util.Scanner;

public class JavaApplication74 {



public static void main(String[] args){


int num1, num2, t1, t2, div=1000;


Scanner sc = new Scanner(System.in);

System.out.print("Enter first four digit integer : ");


num1 = sc.nextInt();


System.out.print("Enter second four digit integer : ");


num2 = sc.nextInt();


if(!numCount(num1) || !numCount(num2))


System.out.println("Sorry please enter only 4 digit integer .");


else{


while( num1 > 0 && num2 > 0)

{


t1 = num1 / div;


t2 = num2 / div;


num1 = num1 % div;


num2 = num2 % div;


div = div / 10;


if(t1 == t2)


System.out.print("*");


else


System.out.print("X");

}

}

}


public static boolean numCount(int num){


int count = 0;


while(num > 0)

{


num = num / 10;

count++;

}


if(count == 4)


return true;


return false;

}

}
thank you both of you
Topic archived. No new replies allowed.