0% found this document useful (0 votes)
33 views41 pages

PROJ

The document contains 11 Java programs that perform various calculations and operations: 1. A calculator program that takes two numbers as input and performs addition, subtraction, multiplication and division 2. A program that evaluates mathematical expressions 3. A program that calculates the area of different shapes like circle, rectangle, triangle, square based on user input 4. A program to convert temperature from Fahrenheit to Celsius 5. A program to convert inches to meters 6. A program to convert minutes to years and days 7. A program that prints the current time in GMT 8. A program to find the greatest of three numbers 9. A program to determine the number of days in a given month 10.

Uploaded by

Tamero Ka
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)
33 views41 pages

PROJ

The document contains 11 Java programs that perform various calculations and operations: 1. A calculator program that takes two numbers as input and performs addition, subtraction, multiplication and division 2. A program that evaluates mathematical expressions 3. A program that calculates the area of different shapes like circle, rectangle, triangle, square based on user input 4. A program to convert temperature from Fahrenheit to Celsius 5. A program to convert inches to meters 6. A program to convert minutes to years and days 7. A program that prints the current time in GMT 8. A program to find the greatest of three numbers 9. A program to determine the number of days in a given month 10.

Uploaded by

Tamero Ka
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/ 41

PROGRAM-1

Write a program to perform following operations on two numbers input by the user:
1) Addition 2) subtraction 3) multiplication 4) division
PROGRAM-1
Write a program to perform following operations on two numbers input by the user:
1) Addition 2) subtraction 3) multiplication 4) division

import java.util.Scanner;
public class prg1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Simple Calculator");
System.out.print("Enter the first number: ");
int num1 = scanner.nextInt();
System.out.print("Enter the second number: ");
int num2 = scanner.nextInt();
int sum = num1 + num2;
System.out.println("Addition: " + sum);
int difference = num1 - num2;
System.out.println("Subtraction: " + difference);
int product = num1 * num2;
System.out.println("Multiplication: " + product);
if (num2 != 0) {
double quotient = (double) num1 / num2;
System.out.println("Division: " + quotient);
} else {
System.out.println("Division by zero is not allowed.");
}
}
}
PROGRAM-2
Write a Java program to print result of the following operations.
1. -15 +58 * 45
2. (35+8) % 6
3. 24 + -5*3 / 7
4. 15 + 18 / 3 * 2 - 9 % 3
PROGRAM-2
Write a Java program to print result of the following operations.
1. -15 +58 * 45
2. (35+8) % 6
3. 24 + -5*3 / 7
4. 15 + 18 / 3 * 2 - 9 % 3

public class prg2 {


public static void main(String[] args) {
double result1 = -15 + 58 * 45;
int result2 = (35 + 8) % 6;
double result3 = 24 + -5 * 3 / 7;
int result4 = 15 + 18 / 3 * 2 - 9 % 3;
System.out.println("Result 1: " + result1);
System.out.println("Result 2: " + result2);
System.out.println("Result 3: " + result3);
System.out.println("Result 4: " + result4);
}
}
PROGRAM-3
Write a Java program to compute area of:
1) Circle
2) rectangle
3) triangle
4) square
PROGRAM-3
Write a Java program to compute area of:
1) Circle
2) rectangle
3) triangle
4) square

import java.util.Scanner;

public class prg3{


public static void main(String[] args) {
Scanner scanner= new Scanner(System.in);
System.out.println("Choose a shape to calculate its area:");
System.out.println("1. Circle");
System.out.println("2. Rectangle");
System.out.println("3. Triangle");
System.out.println("4. Square");
System.out.print("Enter your choice (1/2/3/4): ");
int choice = scanner.nextInt();
double area = 0.0;
switch (choice) {
case 1:
System.out.print("Enter the radius of the circle: ");
double radius = scanner.nextDouble();
area = Math.PI * radius * radius;
break;
case 2:
System.out.print("Enter the length of the rectangle: ");
double length = scanner.nextDouble();
System.out.print("Enter the width of the rectangle: ");
double width = scanner.nextDouble();
area = length * width;
break;
case 3:
System.out.print("Enter the base of the triangle: ");
double base = scanner.nextDouble();
System.out.print("Enter the height of the triangle: ");
double height = scanner.nextDouble();
area = 0.5 * base * height;
break;
case 4: // Square
System.out.print("Enter the side length of the square: ");
double sideLength = scanner.nextDouble();
area = sideLength * sideLength;
break;
default:
System.out.println("Invalid choice.");
return;
}
System.out.println("The area is: " + area);
}
}
PROGRAM-4
Write a program to convert temperature from Fahrenheit to Celsius degree using Java.
PROGRAM-4
Write a program to convert temperature from Fahrenheit to Celsius degree using Java.

import java.util.Scanner;

public class prg4{


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter temperature in Fahrenheit: ");
double fahrenheit = scanner.nextDouble();
double celsius = (fahrenheit - 32) * 5 / 9;
System.out.println("Temperature in Celsius: " + celsius);
}
}
PROGRAM-5
Write a program through Java that reads a number in inches, converts it to meters.
PROGRAM-5
Write a program through Java that reads a number in inches, converts it to meters.

import java.util.Scanner;

public class prg5 {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter length in inches: ");
double inches = scanner.nextDouble();
double meters = inches * 0.0254;
System.out.println("Length in meters: " + meters);
}
}
PROGRAM-6
Write a program to convert minutes into a number of years and days.
PROGRAM-6
Write a program to convert minutes into a number of years and days.
import java.util.Scanner;

public class prg6{


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of minutes: ");
long minutes = scanner.nextLong();
long minutesInDay = 24 * 60;
long minutesInYear = 365 * minutesInDay;
long years = minutes / minutesInYear;
long remainingMinutes = minutes % minutesInYear;
long days = remainingMinutes / minutesInDay;

// Display the result


System.out.println(minutes + " minutes is approximately " + years + " years and " + days +
" days.");
}
}
PROGRAM-7
Write a Java program that prints current time in GMT.
PROGRAM-7
Write a Java program that prints current time in GMT.

import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
class prg7 {
public static void main(String[] args) {
Date localTime = new Date();
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println("Local Time: " + localTime);
System.out.println("Time in GMT: " + dateFormat.format(localTime));
}
}
PROGRAM-8

Write a Java Program to Determine Greatest Number of three numbers.


PROGRAM-8

Write a Java Program to Determine Greatest Number of three numbers.

import java.util.Scanner;
public class prg8
{
public static void main(String[] args)
{
int a,b,c,temp,greater;
Scanner n =new Scanner(System.in);
System.out.println("Enter the first number:");
a=n.nextInt();
System.out.println("Enter the Second number:");
b=n.nextInt();
System.out.println("Enter the Third number:");
c=n.nextInt();
temp=a>b?a:b;
greater=c>temp?c:temp;
System.out.println("The Greatest number is:"+greater);
}
}
PROGRAM-9

Write a Java Program to Find Number of Days in a Month.


PROGRAM-9

Write a Java Program to Find Number of Days in a Month.

import java.util.Scanner;

public class prg9 {


private static Scanner sc;
public static void main(String[] args)
{
int month;
sc = new Scanner(System.in);

System.out.print(" Please Enter Month Number from 1 to 12 (1 = Jan, and 12 = Dec) : ");
month = sc.nextInt();

if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month


== 12 )
{
System.out.println("\n 31 Days in this Month");
}
else if ( month == 4 || month == 6 || month == 9 || month == 11 )
{
System.out.println("\n 30 Days in this Month");
}
else if ( month == 2 )
{
System.out.println("\n Either 28 or 29 Days in this Month");
}
else
System.out.println("\n Please enter Valid Number between 1 to 12");
}
}
PROGRAM-10

Write a program to sum values of an Single Dimensional array.


PROGRAM-10
Write a program to sum values of an Single Dimensional array.

import java.util.Scanner;
public class prg10
{
public static void main(String[] args)
{

int sum=0,n,j;
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number of elements you want to store: ");
n=sc.nextInt();
int[] array = new int[n];
System.out.println("Enter the elements of the array: ");
for(int i=0; i<n; i++)
{
array[i]=sc.nextInt();
}

for(j=0;j<array.length;j++)
{
sum=sum+array[j];
}
System.out.println("Sum of array element:"+sum);
}
}
PROGRAM-11
Calculate the average value of array elements through Java Program.
PROGRAM-11
Calculate the average value of array elements through Java Program.

import java.util.Scanner;
public class prg11
{
public static void main(String[] args)
{
float sum=0,avg;
int n,j;
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number of elements you want to store: ");
n=sc.nextInt();
int[] array = new int[n];
System.out.println("Enter the elements of the array: ");
for(int i=0; i<n; i++)
{
array[i]=sc.nextInt();
}
for(j=0;j<array.length;j++)
{
sum=sum+array[j];
}
System.out.println("Sum of array element:"+sum);
avg=(sum)/n;
System.out.println("Average of array element:"+avg);
}
}
PROGRAM-12
Find the index of an array element by writing a program in Java.
PROGRAM-12
Find the index of an array element by writing a program in Java.

public class prg12 {

public static int findIndex(int[] array, int element) {


for (int i = 0; i < array.length; i++) {
if (array[i] == element) {
return i;
}
}
return -1;
}

public static void main(String[] args) {


int[] array = {1, 2, 3, 4, 5};
int element = 4;
int index = findIndex(array, element);
System.out.println("Index of element " + element + ": " + index);
}
}
PROGRAM-13
Write a Java program to remove a specific element from un array.
PROGRAM-13
Write a Java program to remove a specific element from un array.

import java.util.Arrays;
public class prg14 {

public static void main(String[] args) {


int[] array = {15, 24, 56, 75, 86, 56, 87, 48, 49, 69};

System.out.println("Original Array : "+Arrays.toString(array));

int removeIndex = 1;

for(int i = removeIndex; i < array.length -1; i++){


array[i] = array[i + 1];
}

System.out.println("After removing the second element: "+Arrays.toString(array));


}
}
PROGRAM-14

Write a program to perform following operations on strings:

1) Compare two strings.

2) Count string length


PROGRAM-14

Write a program to perform following operations on strings:

1) Compare two strings.

class prg14a{
public static void main(String args[]){
String s1="jack";
String s2="JACK";

System.out.println(s1.equals(s2)); //false
System.out.println(s1.equalsIgnoreCase(s2)); //true
}
}

2) Count string length

public class prg14b {


public static void main(String args[]) {
String s1 = "jane";
String s2 = "raven";
System.out.println("string length is: " + s1.length());
System.out.println("string length is: " + s2.length());
}
}
3) Convert upper case to lower case

4) Convert lower case to upper case


3) Convert upper case to lower case & vice versa

public class prg14c {


public static void main(String args[]) {
String S1 = new String("UPPERCASE CONVERTED TO LOWERCASE");
System.out.println(S1.toLowerCase());
}
}

4) Convert lower case to upper case

public class prg14d{


public static void main(String args[]) {
String S1 = new String("lowercase converted to uppercase");
System.out.println(S1.toUpperCase());
}
}
5) Concatenate two strings

6) Print a substring
5) Concatenate two strings
public class prg14e{
public static void main(String[] args){
String str1 = "Java";
String str2 = "Programming";
String str3 = str1.concat(str2);
System.out.println(str3);
}
}

6) Print a substring

public class prg14f{


public static void main(String args[]){
String s="JAVA PROGRAM";
System.out.println("Original String: " + s);
System.out.println("Substring starting from index 4: " +s.substring(5));
System.out.println("Substring starting from index 0 to 4: "+s.substring(0,4));
}
}
PROGRAM-15
Developed Program & design a method to find the smallest number among three numbers.
PROGRAM-15
Developed Program & design a method to find the smallest number among three numbers.

import java.io.*;

class Smallest {
public static void main(String[] args) {
int a = 3, b = 5, c = 7;
if (a <= b && a <= c)
System.out.println(a + " is the smallest");
else if (b <= a && b <= c)
System.out.println(b + " is the smallest");
else
System.out.println(c + " is the smallest");
}
}
PROGRAM-16

Compute the average of three numbers through a Java Program.


PROGRAM-16

Compute the average of three numbers through a Java Program.

import java.util.Scanner;

public class Avg {

public static void main(String[] args) {

int a, b, c;

float sum, average;

Scanner n = new Scanner(System.in);

System.out.println("Enter the first number:");

a = n.nextInt();

System.out.println("Enter the Second number:");

b = n.nextInt();

System.out.println("Enter the Third number:");

c = n.nextInt();

sum = a + b + c;

System.out.println("Sum of array element:" + sum);

average = (sum) / 3;

System.out.println("Average of array element:" + average);

}
PROGRAM-17

Write a Program & design a method to count all vowels in a string.


PROGRAM-17

Write a Program & design a method to count all vowels in a string.

import java.util.Scanner;

public class Vowels {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

System.out.print("Input the string: ");

String str = in.nextLine();

System.out.print("Number of Vowels in the string: " + count_Vowels(str) + "\n");

public static int count_Vowels(String str) {

int count = 0;

for (int i = 0; i < str.length(); i++) {

if (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i'

|| str.charAt(i) == 'o' || str.charAt(i) == 'u') {

count++;

return count;

}
PROGRAM-18

To represent the concept of all types of inheritance supported by Java, design a program.
PROGRAM-18

To represent the concept of all types of inheritance supported by Java, design a program.

class Doctor {
void Doctor_Details() {
System.out.println("Doctor Details...");
}
}

class Surgeon extends Doctor {


void Surgeon_Details() {
System.out.println("Surgen Detail...");
}
}

public class Inheritance {


public static void main(String args[]) {
Surgeon s = new Surgeon();
s.Doctor_Details();
s.Surgeon_Details();
}
}

You might also like