0% found this document useful (0 votes)
7 views

Java Programs

Uploaded by

Suvidha Reddy
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Java Programs

Uploaded by

Suvidha Reddy
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 11

// Write a program to find whether a number is odd or even

import java.util.*;

class EvenOrOdd {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();

if (num % 2 == 0)
System.out.println(num + " is Even");

else
System.out.println(num + " is Odd");
}
}

___________________________________________________________________________________
___________________________________

// Write a program to find whether a number is a prime number or not

import java.util.*;

class EvenOrOdd {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();

boolean isPrime = true;


for(int i = 2; i < num / 2 + 1; i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}

if (isPrime)
System.out.println(num + " is prime");
else
System.out.println(num + " is not prime");
}
}

___________________________________________________________________________________
___________________________________

// Write a program to print the day according to the number.


// For example if n = 1 then print monday, 2 for tuesday and so on till 7 for
Sunday.
// If the user enters 2, then output should be tuesday.

import java.util.*;

class EvenOrOdd {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();

String day = "";


switch(num) {
case 1: day = "Monday"; break;
case 2: day = "Tuesday"; break;
case 3: day = "Wednesday"; break;
case 4: day = "Thursday"; break;
case 5: day = "Friday"; break;
case 6: day = "Saturday"; break;
case 7: day = "Sunday"; break;
default: day = "Invalid";
}

System.out.println(day);
}
}

___________________________________________________________________________________
___________________________________

// A perfect number is a number that is equal to the sum of its factors other than
the number itself.
// Write a function that takes n as a parameter and returns 1 if the number is a
perfect number or else returns 0.

import java.util.*;

class Perfect {
public static int isPerfect(int num) {
int sum = 1;
for (int i = 2; i < num / 2 + 1; i++) {
if (num % i == 0)
sum += i;
}
return (sum == num) ? 1 : 0;
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
int num = sc.nextInt();

System.out.println(isPerfect(num));
}
}

___________________________________________________________________________________
___________________________________

// Given x and n as inputs. Write a function to return the sum of the following
series.
// sum = x^2 / 1! + x^4 / 3! + x^6 / 5! + x^n / (x - 1)!
import java.util.*;

class Series {
public static int factorial(int num) {
int fact = 1;
for(int i = num; i > 1; i--)
fact *= i;
return fact;
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

int x = sc.nextInt();
int n = sc.nextInt();

int sum = 0;

for(int i = 2; i <= n; i += 2) {
System.out.println(i + " " + x);
sum += ((Math.pow(x, i)) / factorial(i - 1));
}

System.out.println(sum);
}
}

___________________________________________________________________________________
___________________________________

// Given n as input. Write a program to print the fibonacci sequence of n numbers.

import java.util.*;

class Fibonacci {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

int num = sc.nextInt();

int a = 0;
int b = 1;

System.out.print(a + " " + b + " ");


for(int i = 2; i < num; i++) {
int c = a + b;
System.out.print(c + " ");
a = b;
b = c;
}
}
}

___________________________________________________________________________________
___________________________________
// Write a program to print the sum of digits of a number.

import java.util.*;

class SumOfDigits {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

int num = sc.nextInt();

int sum = 0;
while(num != 0) {
int d = num % 10;
sum += d;
num = num / 10;
}

System.out.println(sum);
}
}

___________________________________________________________________________________
___________________________________

// Write a program to print the reverse of a string.

import java.util.*;

class Reverse {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

String s = sc.nextLine();

String rev = "";


for(int i = 0; i < s.length(); i++) {
rev = s.charAt(i) + rev;
}

System.out.println(rev);
}
}

___________________________________________________________________________________
___________________________________

// Largest of 3 numbers

import java.util.*;

class LargestOf3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();

int greatest;
if (a > b && a > c)
greatest = a;
else if (b > c)
greatest = b;
else
greatest = c;

System.out.println(greatest + " is greatest");

}
}

___________________________________________________________________________________
___________________________________

// Count vowels in a string

import java.util.*;

class Vowels {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

String s = sc.nextLine();

s = s.toLowerCase();
int vc = 0;
for(int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'a' || s.charAt(i) == 'e' || s.charAt(i) == 'i' ||
s.charAt(i) == 'o' || s.charAt(i) == 'u') {
vc += 1;
}
}

System.out.println(vc);

}
}

___________________________________________________________________________________
___________________________________

// Sum of elements in an array

import java.util.*;

public class Main {


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

for(int i = 0; i < n; i++) {


arr[i] = sc.nextInt();
}

int sum = 0;
for(int i = 0; i < n; i++) {
sum += arr[i];
}

System.out.println(sum);
}
}

___________________________________________________________________________________
___________________________________

// Convert lowercase to upper case and vice versa

import java.util.*;

public class Main {


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

String s = sc.nextLine();

String ns = "";
for(int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if(Character.isUpperCase(ch))
ns += Character.toLowerCase(ch);
else
ns += Character.toUpperCase(ch);
}

System.out.println(ns);
}
}

-----------------------------------------------------------------------------------
----------------------------------

// Convert lowercase to upper case and vice versa

import java.util.*;

public class Main {


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

String s = sc.nextLine();

System.out.println(s.toLowerCase());
}
}

-----------------------------------------------------------------------------------
----------------------------------

// Convert lowercase to upper case and vice versa

import java.util.*;

public class Main {


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

String s = sc.nextLine();

System.out.println(s.toUpperCase());
}
}

-----------------------------------------------------------------------------------
----------------------------------

// Convert lowercase to upper case and vice versa

import java.util.*;

public class Main {


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

String s = sc.nextLine();

String ns = "";
for(int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);

if (ch >= 'a' && ch <= 'z')


ns += (char) (ch - 32);
else if (ch >= 'A' && ch <= 'Z')
ns += (char) (ch + 32);
}

System.out.println(ns);
}
}

-----------------------------------------------------------------------------------
----------------------------------

// Convert lowercase to upper case and vice versa

import java.util.*;

public class Main {


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

String s = sc.nextLine();
String ns = "";
for(int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);

if (ch >= 97 && ch <= 122)


ns += (char) (ch - 32);
else if (ch >= 65 && ch <= 90)
ns += (char) (ch + 32);
else
ns += ch;
}

System.out.println(ns);
}
}

___________________________________________________________________________________
___________________________________

// Write a program in Java to display the square and cube of first n natural
numbers, taking the value ot n as an input.

import java.util.*;

public class Main {


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

int n = sc.nextInt();

for(int i = 1; i <= n; i++) {


System.out.printf("%5d %5d \n", (i * i), (i * i * i));
}

}
}

___________________________________________________________________________________
___________________________________

/*
Write a program in java to enter a number and check whether the number is Armstrong
or not.
A number is said to be an Armstrong if the sum of cubes of the digits is equal to
the original number.

For example:
1³ + 5³ + 3³ = 153

*/
import java.util.*;

public class Main {


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

int n = sc.nextInt();
int num = n;
int sum = 0;

while(num != 0) {
int d = num % 10;
sum += (d * d * d);
num = num / 10;
}

if (n == sum)
System.out.println(n + " is an Armstrong number");
else
System.out.println(n + " is not an Armstrong number");
}
}

___________________________________________________________________________________
___________________________________

// Write a program to find the factorial of a number.

import java.util.*;

public class Main {

public static int factorial(int n) {


if(n == 0 || n == 1)
return 1;
return n * factorial(n - 1);
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

int n = sc.nextInt();
int fact = factorial(n);

System.out.println(fact);
}
}

___________________________________________________________________________________
___________________________________

/*

The program must print an electronic watch screen output for a given value of
seconds since midnight.

It is guaranteed, that input number is non-negative.

Output format is h:mm:ss (possible values: [0:00:00; 23:59:59]).

Examples:

Input: 60
Output: 0:01:00

Input: 3599
Output: 0:59:59

Input: 86229
Output: 23:57:09

Input: 86400
Output: 0:00:00

Input: 89999
Output: 0:59:59

Input: 86460
Output: 0:01:00

*/

import java.util.Scanner;

class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int secs = sc.nextInt();
int s = 0, m = 0, h = 0;

if (secs != 0) {
h = secs / 3600;
m = (secs % 3600) / 60;
s = secs % 60;

if (h >= 24) {
h = h % 24;
}
}

System.out.printf("%d:%02d:%02d\n", h, m, s);
}
}

___________________________________________________________________________________
___________________________________

// Write a program in java to accept a character and check whether it is a letter


or not.
// If it is a letter then change the case of the letter otherwise check either it
is a digit or a special character.

import java.util.Scanner;

class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

char ch = sc.next().charAt(0);
if(Character.isLetter(ch)) {
if(Character.isUpperCase(ch)) {
System.out.println(Character.toLowerCase(ch));
} else {
System.out.println(Character.toUpperCase(ch));
}
} else if (Character.isDigit(ch)) {
System.out.println(ch + " is a digit");
} else {
System.out.println(ch + " is a special character");
}
}
}

___________________________________________________________________________________
___________________________________

/*
Write a program in Java to print a square
n = 4

Output:
****
****
****
****
*/

import java.util.Scanner;

class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();

for(int i = 0; i < n; i++) {


for(int j = 0; j < n; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}

___________________________________________________________________________________
___________________________________

// Write a program in Java to input 2 numbers. Display it's HCF and LCM.

You might also like