Java Programs
Java Programs
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");
}
}
___________________________________________________________________________________
___________________________________
import java.util.*;
class EvenOrOdd {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
if (isPrime)
System.out.println(num + " is prime");
else
System.out.println(num + " is not prime");
}
}
___________________________________________________________________________________
___________________________________
import java.util.*;
class EvenOrOdd {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
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;
}
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;
}
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);
}
}
___________________________________________________________________________________
___________________________________
import java.util.*;
class Fibonacci {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = 0;
int b = 1;
___________________________________________________________________________________
___________________________________
// 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 sum = 0;
while(num != 0) {
int d = num % 10;
sum += d;
num = num / 10;
}
System.out.println(sum);
}
}
___________________________________________________________________________________
___________________________________
import java.util.*;
class Reverse {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
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;
}
}
___________________________________________________________________________________
___________________________________
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);
}
}
___________________________________________________________________________________
___________________________________
import java.util.*;
int sum = 0;
for(int i = 0; i < n; i++) {
sum += arr[i];
}
System.out.println(sum);
}
}
___________________________________________________________________________________
___________________________________
import java.util.*;
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);
}
}
-----------------------------------------------------------------------------------
----------------------------------
import java.util.*;
String s = sc.nextLine();
System.out.println(s.toLowerCase());
}
}
-----------------------------------------------------------------------------------
----------------------------------
import java.util.*;
String s = sc.nextLine();
System.out.println(s.toUpperCase());
}
}
-----------------------------------------------------------------------------------
----------------------------------
import java.util.*;
String s = sc.nextLine();
String ns = "";
for(int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
System.out.println(ns);
}
}
-----------------------------------------------------------------------------------
----------------------------------
import java.util.*;
String s = sc.nextLine();
String ns = "";
for(int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
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.*;
int n = sc.nextInt();
}
}
___________________________________________________________________________________
___________________________________
/*
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.*;
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");
}
}
___________________________________________________________________________________
___________________________________
import java.util.*;
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.
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);
}
}
___________________________________________________________________________________
___________________________________
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();
___________________________________________________________________________________
___________________________________
// Write a program in Java to input 2 numbers. Display it's HCF and LCM.