Write a program that prompt user to enter an integer value and display the number with its digits reversed and also the largest and the smallest digit. Program should have the following:
You should have two classes ‘Reverse’ and ‘reverseTest’.
The class ‘reverseTest’ should accept integer value from the user and pass this value to a method ‘reverseOrder’ of ‘Reverse’ class.
The method ‘ReverseOrder’ should display value in reverse order and store each and every digit in an array.
The ‘reverseOrder’ method should call two other methods ‘reverseLargest’ and ‘ReverseSmallest’ and pass array to these methods.
The method ‘reverseLargest’ should find the largest digit from the array and display the result.
The method ‘reverseSmallest’ should find the smallest digit from the array and display the result.
This seems to be an assignment question… and in here I believe it’s not something we usually answer directly to…
What isssues are you finding when designing this program? Maybe if you can point out the exact places where you are having trouble and ask general questions about them, maybe we can help… Can’t you reverse a number? Put it into an array?..
public class Reverse
{
public static int num,newnum,count,countDigit,rightDigit,largest,smallest,number;
static String reverseOrder()
{
for(count=1; count <=countDigit; count++)
{
rightDigit = num % 10;
newnum = newnum * 10 + rightDigit;
num = num / 10;
}
System.out.println("Reversed is : " + newnum);
}
static String reverseLargest()
{
for (counter=1; counter<10; counter++)
{
System.out.print( "Enter number: " );
number = input.nextInt();
if ( number > largest )
largest = number;
System.out.printf( "Largest number is %d\n", largest );
}
static String reverseSmallest()
{
System.out.print( "Enter number: " );
number = input.nextInt();
if ( number < largest )
smallest = number;
System.out.printf( "Smallest number is %d\n", smallest );
}
}
ReverseTest.java
import java.util.Scanner;
public class ReverseTest
{
public static int num,newnum,count,countDigit,rightDigit;
public static void main(String args[])
{
Reverse test = new reverseOrder();
Scanner input = new Scanner( System.in );
System.out.println( "Enter integer : " );
num = input.nextInt();
System.out.println( "How many digits does it has : " );
countDigit = input.nextInt();
System.out.println("Integer value : " + num);
System.out.println("/n");
System.out.println("Reverse order : " + test.reverseOrder() );
System.out.println("/n");
System.out.println("Largest value : " + test.reverseLargest() );
System.out.println("/n");
System.out.println("Smallest value : " + test.reverseSmallest() );
}
}
This is just a note, not a bug - when your methods are static, you do not need instance to call it.
Reverse.reverseOrder();
Also for a beginner it’s better to use local variables. So try to comment this line
public static int num,newnum,count,countDigit,rightDigit,largest,smallest,number;
and use variables you need in methods only (so called local variables).
Finally try to change your methods is this way: use method parameters to get inputs and return value calculated in method as return value.
Example:
In problem statement, there is not written, that you have to ask for integer length (number of digits), try to write the method, that accepts integer and returns numbers of digits in it.
static int getDigitsCount( int n ) {
...
}
There is several ways how to do this, but in maths it’s something like: “how many times you can remove last digit (substract last digit and divide by 10)”.
Let’s do this and when you are done, we will move to the next step, ok? Let me know when you are finished or there is a problem…
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
long int s,n;
printf("\nEnter the value of n: “);
scanf(”%ld",&n);
s=0;
while(n>0)
{
s=s*10+n%10;
n=n/10;
}
printf("\n%ld",s);
getch();
}