0% found this document useful (0 votes)
25 views6 pages

Assignment_1_Complete

The document contains a series of Java programming assignments, each with a specific task such as checking for prime numbers, displaying perfect numbers, reversing a string, and counting character frequencies. Each assignment includes a complete Java program demonstrating the required functionality. The assignments are organized into three sets, with varying complexity and focus on different programming concepts.

Uploaded by

weralij635
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views6 pages

Assignment_1_Complete

The document contains a series of Java programming assignments, each with a specific task such as checking for prime numbers, displaying perfect numbers, reversing a string, and counting character frequencies. Each assignment includes a complete Java program demonstrating the required functionality. The assignments are organized into three sets, with varying complexity and focus on different programming concepts.

Uploaded by

weralij635
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Assignment No.

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)

public class EvenNumbersFromArray {


public static void main(String[] args) {
System.out.println("Even numbers:");
for (String arg : args) {
int num = Integer.parseInt(arg);
if (num % 2 == 0)
System.out.println(num);
}
}
}

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

array and display it.

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));
}
}

You might also like