Assignment_1_Complete
Assignment_1_Complete
1: Introduction to Java
Set A
Q1. Write a java Program to check whether given number is Prime or Not.
import java.util.Scanner;
public class PrimeCheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
boolean isPrime = true;
if (num <= 1) isPrime = false;
else {
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
}
if (isPrime)
System.out.println(num + " is a Prime Number.");
else
System.out.println(num + " is not a Prime Number.");
}
}
Q2. Write a java Program to display all the perfect numbers between 1 to n.
import java.util.Scanner;
public class PerfectNumbers {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter n: ");
int n = sc.nextInt();
for (int i = 1; i <= n; i++) {
int sum = 0;
for (int j = 1; j < i; j++) {
if (i % j == 0)
sum += j;
}
if (sum == i)
System.out.println(i + " is a Perfect Number");
}
}
}
Q3. Write a java Program to accept employee name from a user and display it in reverse order.
import java.util.Scanner;
public class ReverseEmployeeName {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter employee name: ");
String name = sc.nextLine();
StringBuilder reversed = new StringBuilder(name).reverse();
System.out.println("Reversed Name: " + reversed);
}
}
Q4. Write a java program to display all the even numbers from an array. (Use Command Line
arguments)
Q5. Write a java program to display the vowels from a given string.
import java.util.Scanner;
public class DisplayVowels {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = sc.nextLine();
System.out.print("Vowels in the string: ");
for (int i = 0; i < str.length(); i++) {
char ch = Character.toLowerCase(str.charAt(i));
if ("aeiou".indexOf(ch) != -1)
System.out.print(ch + " ");
}
}
}
Set B
Q1. Write a java program to accept n city names and display them in ascending order.
import java.util.Arrays;
import java.util.Scanner;
public class SortCities {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of cities: ");
int n = sc.nextInt();
sc.nextLine();
String[] cities = new String[n];
for (int i = 0; i < n; i++) {
System.out.print("Enter city name: ");
cities[i] = sc.nextLine();
}
Arrays.sort(cities);
System.out.println("Cities in ascending order:");
for (String city : cities)
System.out.println(city);
}
}
Q2. Write a java program to accept n numbers from a user store only Armstrong numbers in an
import java.util.Scanner;
public class ArmstrongNumbers {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter how many numbers: ");
int n = sc.nextInt();
int[] armstrongs = new int[n];
int count = 0;
for (int i = 0; i < n; i++) {
System.out.print("Enter number: ");
int num = sc.nextInt(), temp = num, sum = 0, digits =
String.valueOf(num).length();
while (temp > 0) {
int d = temp % 10;
sum += Math.pow(d, digits);
temp /= 10;
}
if (sum == num)
armstrongs[count++] = num;
}
System.out.println("Armstrong Numbers:");
for (int i = 0; i < count; i++)
System.out.println(armstrongs[i]);
}
}
Set C
Q1. Write a java program to count the frequency of each character in a given string.
import java.util.HashMap;
import java.util.Scanner;
public class CharFrequency {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = sc.nextLine();
HashMap<Character, Integer> freq = new HashMap<>();
for (char ch : str.toCharArray()) {
if (Character.isWhitespace(ch)) continue;
freq.put(ch, freq.getOrDefault(ch, 0) + 1);
}
System.out.println("Character frequencies:");
for (char ch : freq.keySet())
System.out.println(ch + ": " + freq.get(ch));
}
}