Java Program

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

20/8/2024 – Mokesh Nagpal (MCA/10071/24)

Assignment 2

1. Find the largest among 3 user entered nos. using Java.

2. Accept 10 numbers from users and check how many of them are even and how many
odd.

3. Write a program to convert Decimal to Hexadecimal number system.

4. Write Java program to count total number of words and the number of di erent
characters in a string.

5. Write a Program to print Factors of a Positive Integer.

6. WAP to print a user given sentence in reverse order (without using any library function
for that purpose).

7. WAP to print each word of a user given sentence in reverse order (without using any
library function for that purpose).

Q1. Find the largest among 3 user entered nos. using Java.
/*
Find the largest among 3 user entered nos. using Java.
*/
import java.util.*;

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int first = 0;
int second = 0;
int third = 0;
System.out.println("This program finds the largest among 3 user
entered nos. using Java.");
System.out.print("Input the first number: ");
first = sc.nextInt();
System.out.print("Input the second number: ");
second = sc.nextInt();
System.out.print("Input the third number: ");
third = sc.nextInt();
if(first == second && first == third){
System.out.println("All three number, "+first+" are equal");
}
else if(first >= second && first >= third){
System.out.println(first+" is the largest among all inputted
numbers");
}
else if(second >= first && second >= third){
System.out.println(second+" is the largest among all inputted
numbers");
}
else{
System.out.println(third+" is the largest among all inputted
numbers");
}
}
}
OUTPUT:

Q2. Accept 10 numbers from user and check how many of them are even and how many
odd.
/*
Accept 10 numbers from user and check how many of them are even and how
many are odd.
*/
import java.util.*;

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int number = 0;
int even = 0;
int odd = 0;
System.out.println("This program accepts 10 numbers from user and
check how many of them are even and how many are odd.");
for(int i=1; i<=10; i++) {
System.out.print("Input the " + (i) + "th number: ");
number = sc.nextInt();
if (number % 2 == 0) {
even++;
} else {
odd++;
}
}
System.out.println("Number of even inputted numbers are: "+even);
System.out.println("Number of odd inputted numbers are: "+odd);
}
}
OUTPUT:
Q3. Write a program to convert Decimal to Hexadecimal number system.
/*
Write a program to convert decimal to hexadecimal number system.
*/
import java.util.*;

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int number = 0;
System.out.println("This program converts decimal to hexadecimal
number system. ");
System.out.print("Input a number: ");
number = sc.nextInt();
int cpy = number;
int quotient = 0;
String hex = "";
while(cpy > 0){
quotient = cpy%16;
cpy=cpy/16;
if(quotient<10)
hex = Integer.toString(quotient) + hex ;
else {
switch (quotient) {
case 10:
hex = 'A' + hex;
break;
case 11:
hex = 'B' + hex;
break;
case 12:
hex = 'C' + hex;
break;
case 13:
hex = 'D' + hex;
break;
case 14:
hex = 'E' + hex;
break;
case 15:
hex = 'F' + hex;
break;
}
}
}
System.out.println("Decimal Number: "+number);
System.out.println("Hexadecimal Number: "+hex);
}
}
OUTPUT:

Q4. Write Java program to count total number of words and the number of di erent
characters in a string.
/*
Write a Java program to count total number of words and the number of
different characters in a string.
*/
import java.util.*;

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = "";
int count_word = 0;
String str_unique = "";
int space = 0;
System.out.println("This Java program to counts total number of
words and the number of different characters in a string.");
System.out.print("Input a string: ");
str = sc.nextLine();
str = str.trim();
str = str+ ' ';
for(int i = 0; i < str.length(); i++){
if(str_unique.indexOf(str.charAt(i)) == -1){
str_unique = str_unique + str.charAt(i) + ", ";
}
if(str.charAt(i)==' ' && space == 0){
count_word++;
space = 1;
}
if(str.charAt(i)!=' '){
space = 0;
}
}
System.out.println("Inputted String: "+str);
System.out.println("Number of Unique Character: "+str_unique);
System.out.println("Number of Words: "+count_word);
}
}
OUTPUT:

Q5. Write a Program to print Factors of a Positive Integer.


/*
Write a Program to print Factors of a Positive Integer.
*/
import java.util.*;

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int number = 0;
System.out.println("This program print factors of a positive
Integer");
System.out.print("Input a number: ");
number = sc.nextInt();
if(number <0){
System.out.println(number + " is not a positive number");
}
System.out.println("Factors of the number "+number+" is as follows:
");
for(int i=2; i<=number; i++){
if(number%i == 0){
System.out.print(i+"\t");
}
}

}
}
OUTPUT:
Q6. WAP to print a user given sentence in reverse order (without using any library
function for that purpose).
/*
WAP to print a user given sentence in reverse order (without using any
library function for that purpose.)
*/
import java.util.*;

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = "";
System.out.println("This program prints a user given sentence in
reverse order");
System.out.print("Input a string: ");
str = sc.nextLine();
String str_cpy = "";
for(int i=0; i<str.length(); i++){
str_cpy = str.charAt(i) + str_cpy;
}
System.out.println("Inputted string: "+str);
System.out.println("Reversed string: "+str_cpy);
}
}
OUTPUT:

Q7. WAP to print each word of a user given sentence in reverse order (without using any
library function for that purpose).
/*
WAP to print each word of a user given string sentence on reverse order
(without using any library function for that purpose.)
*/
import java.util.*;

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = "";
int flag = 0;
int str_start = 0;
int str_end = 0;
System.out.println("This program prints each word of a user given
string sentence on reverse order");
System.out.print("Input a string: ");
str = sc.nextLine();
str = str.trim();
str = str + ' ';
String str_cpy = "", str_cpy2 ="";
for(int i=0; i<str.length(); i++){
if(str.charAt(i) != ' ' && flag == 0){
str_start = i;
flag = 1;
}
else if(flag == 1 && str.charAt(i) == ' ') {
str_end = i;
flag = 0;
str_cpy2 ="";
for (int j = str_start; j < str_end; j++) {
str_cpy2 = str.charAt(j) + str_cpy2;
}
str_cpy = str_cpy + str_cpy2 + " ";
}
}
System.out.println("Inputted string: "+str);
System.out.println("Reversed word string: "+str_cpy);
}
}
OUTPUT:

You might also like