Declare k , d , and s so that they can store an integer , a real number, and a s
ID: 640113 • Letter: D
Question
Declare k, d, and s so that they can store an integer , a real number, and a small word (under 10 characters ).
Use these variables to first read in an integer , a real number, and a small word and print them out in reverse order (i.e., the word, the real, and then the integer ) all on the same line, separated by EXACTLY one space from each other.
Then, on a second line, print them out in the original order (the integer , the real, and the word), separated again by EXACTLY one space from each other.
Explanation / Answer
import java.util.*;
import java.util.Scanner;
class Reverse
{
public static void main(String args[])
{
String s,k, reverse = "";
int no,k,rev=0,n, reverse_number = 0;
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to reverse");
k = in.nextLine();
System.out.println("Enter the number to reverse");
n = in.nextInt();
System.out.print("Enter real Number you want to reverse : ");
s=in.readLine();
int length = k.length();
for ( int i = length - 1 ; i >= 0 ; i-- )
reverse = reverse + k.charAt(i);
while( n != 0 )
{
reverse_number = reverse_number * 10;
reverse_number = reverse_number + n%10;
n = n/10;
}
no=Integer.parseInt(s);
while(no > 0)
{
k = no % 10;
rev = rev * 10 + k;
no=no/10;
}
System.out.println("Reverse Number is : "+rev);
System.out.println("Reverse of entered number is "+reverse_number);
System.out.println("Reverse of entered string is: "+reverse);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.