1. Write a Java program that reads an integer value n, and it reverses its digit
ID: 3666336 • Letter: 1
Question
1. Write a Java program that reads an integer value n, and it reverses its digits. Use either a for or a while loop. D not use arrays.
2. Write a Java program that takes an unput a snumber base 10 (example 51966) and it outputs the corresponding hexadecimal number (CAFE). A hexadecimal number is a number written in base 16 using as sixteen digits, 0 to 9 for the first 10, and A for 10, B for 11, C for 12, D for 13, E for 14, F for 15. For instance: AB3 = 10x16^2+11x16+3X16^0=2739 and CAFE=12x16^3+10x16^2+15x16^1+14x16^0
3. Write a Java program that reads an integer value n, it reads n integer values and it outputs the largest and the smallest of the n integer values. Integers and it outputs their values in increasing order. Use either a for a while loop. Example: How many numbers? 10 Give me the 10 numbers: 7, 21, 5, 12, 14, 2, 1, 34, 9, 6. Smallest number is 1. Largest number is 34
Explanation / Answer
import java.util.Scanner;
/**
* @author Srinivas Palli
*
*/
public class ReverseNumber {
/**
* @param args
*/
public static void main(String[] args) {
System.out.print("Enter a number: ");
Scanner in = new Scanner(System.in);
int reversenum = 0;
int num = in.nextInt();
for (; num != 0;) {
reversenum = reversenum * 10;
reversenum = reversenum + num % 10;
num = num / 10;
}
System.out.println("Reverse of specified number is: " + reversenum);
}
}
OUTPUT:
Enter a number: 453
Reverse of specified number is: 354
import java.util.Scanner;
/**
* @author Srinivas Palli
*
*/
class DecimalToHex {
/**
* @param args
*/
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a decimal number : ");
int num = input.nextInt();
// For storing remainder
int rem;
// For storing result
String str2 = "";
// Digits in hexadecimal number system
char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
'B', 'C', 'D', 'E', 'F' };
while (num > 0) {
rem = num % 16;
str2 = hex[rem] + str2;
num = num / 16;
}
System.out.println("Decimal to hexadecimal: " + str2);
}
}
OUTPUT:
Enter a decimal number : 51966
Decimal to hexadecimal: CAFE
import java.util.Scanner;
/**
* @author Srinivas Palli
*
*/
public class SmallestBiggest {
/**
* @param args
*/
public static void main(String[] args) {
int small = 0;
int large = 0;
int num;
System.out.print("enter the n value:");
Scanner input = new Scanner(System.in);
int n = input.nextInt();
for (int i = 0; i < n; i++) {
num = input.nextInt();
// gives the largest number in n numbers
if (num > large) {
large = num;
}
// gives the smallest number in n numbers
if (i == 0 && num > 0)
small = num;
if (num < small)
small = num;
}
System.out.println("Largest number is " + large);
System.out.println("Smallest number is " + small);
}
}
OUTPUT:
enter the n value:10
7
21
5
12
14
2
1
34
9
6
Largest number is 34
Smallest number is 1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.