Java Calculator
import java.util.Scanner;
public class main{
public static void main(String[]args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the first number:");
double num1 = sc.nextDouble();
System.out.println("Enter the operator to perform:");
char op = sc.next().charAt(0);
System.out.println("Enter the Second number:");
double num2 = sc.nextDouble();
double result;
switch(op){
case'+':
result = num1+num2;
System.out.println(num1+"+"+num2+"="+result);
break;
case'-':
result = num2-num1;
System.out.println(num2+"-"+num1+"="+result);
break;
case'*':
result = num1*num2;
System.out.println(num1+"*"+num2+"="+result);
break;
case'/':
result = num2/num1;
System.out.println(num1+"/"+num2+"="+result);
break;
case'%':
result = num1%num2;
System.out.println(num1+"%"+num2+"="+result);
break;
default:
System.out.println("Invalid operator please choose correct one");
}
}
}
Output:
Enter the first number:
5
Enter the operator to perform:
*
Enter the Second number:
5
5.0*5.0=25.0
PRIME NUMBER CHECK
import java.util.Scanner;
public class Prime {
static isPrime(int n) {
if (n <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Prime p = new Prime();
System.out.print("Enter the number to check prime: ");
int num = sc.nextInt();
if (p.isPrime(num)) {
System.out.println(num + " is a prime number");
} else {
System.out.println(num + " is not a prime number");
}
}
}
OUTPUT
Enter the number to check prime: 5
5 is a prime number
ARITHMETIC MEAN
// User function Template for Java
class Solution {
public long sum_of_ap(long n, long a, long d) {
long sum = 0;
for (int i = 0; i < n; i++)
{
sum = sum + a;
a = a + d;
}
return sum;
}
}
All divisors of a natural number
class Solution {
static int countFactors(int n) {
int count = 0;
int i;
for (i = 1; i * i <= n; i++) {
if (n % i == 0) {
count++;
if (i != n / i) {
count++;
}
}
}
return count;
}
}
LCM AND GCD
class Solution {
public static int[] lcmAndGcd(int a, int b) {
int gcd = findGCD(a, b);
int lcm = (a * b) / gcd;
return new int[]{lcm, gcd};
}
private static int findGCD(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}}
Strong Numbers are the numbers whose sum of factorial of digits is equal to the original number. Given a
number N, the task is to check if it is a Strong Number or not. Print 1 if the Number is Strong, else Print 0.
class Solution {
static int isStrong(int N) {
int original = N;
int sum = 0;
while (N > 0) {
int digit = N % 10;
sum += factorial(digit);
N /= 10;
}
return (sum == original) ? 1 : 0;
}
private static int factorial(int num) {
int fact = 1;
for (int i = 1; i <= num; i++) {
fact *= i;
}
return fact;
}
}
You are given four numbers num1, den1, num2, and den2. You need to find (num1/den1)+(num2/den2) and output
the result in the form of (numx/denx).
Input Format:
The first line of input contains an integer T denoting the number of test cases . Then T test cases follow . Each test
case contains four integers num1, den1, num2, den2 respectively .
Output Format:
For each test case, in a new line, output will be the fraction in the form a/b where the fraction denotes the sum of
the two given fractions in reduced form.
Your Task:
Since this is a function problem, you don't need to worry about the testcases. Your task is to complete the
function addFraction which adds the two fractions and prints the resulting fraction. The function takes four
arguments num1, den1, num2, den2 where num1, num2 denotes the numerators of two fractions and den1,
den2 denotes their denominators.
class GfG {
void addFraction(int num1, int den1, int num2, int den2) {
int num = (num1 * den2) + (num2 * den1);
int den = den1 * den2;
int gcd = findGCD(num, den);
num /= gcd;
den /= gcd;
System.out.println(num + "/" + den);
}
private int findGCD(int a, int b) {
if (b == 0) return a;
return findGCD(b, a % b);
}
}
Voting
import java.util.*;
class main{
public static void main(String[]args){
Scanner sc = new Scanner(System.in);
System.out.print("Enter the age of the person:");
int age = sc.nextInt();
if(age>=18){
System.out.println("The Person is eligible to vote");
}
else {
System.out.println("The person is not eligible to vote");
}
}
}
Leap year
import java.util.*;
class Main {
static boolean check(int n) {
if (n % 4 == 0) {
if (n % 100 == 0) {
return n % 400 == 0;
}
return true;
}
return false;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the year to check leap year: ");
int year = sc.nextInt();
if (check(year)) {
System.out.println("true");
} else {
System.out.println("false");
}
}
}
Truth table
Input 1
Input 2
AND &&
OR ||
Not!
0
0
0
0
1
0
1
0
1
1
1
0
0
1
0
1
1
1
1
0
Largest number using relational operator
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the first number: ");
int first = sc.nextInt();
System.out.println("Enter the second Number;");
int second = sc.nextInt();
System.out.println("Enter the third number:");
int third = sc.nextInt();
int largest;
if(first>second){
if(first>third){
largest = first;
}
else{
largest = third;
}
}
else{
if(second>third){
largest = second;
}
else{
largest = third;
}
}
System.out.println("The lagrest number is "+largest);
}
}
Bitwise operator
It performs the operations on bits of integer values
They are mostly use in low level programming
Operator
&
|
^
name
AND
or
Xor
Finding max and Min in array
public class MinMax{
public static void main(String[]args){
int[] num = {10,20,30,40,50};
for(int i=0;i<num.length;i++){
System.out.println("Array elements are:"+num[i]);
}
int max = num[0];
int min = num[0];
for(int i=0;i<num.length;i++){
if(num[i]>max){
max = num[i];
}
if(num[i]<min){
min = num[i];
}
}
System.out.println("The max element:"+max);
Description
1 if both 1 are true
1 if sum of 1 & 2 is true
System.out.println("the min elemment:"+min);
}
}
Array elements are:10
Array elements are:20
Array elements are:30
Array elements are:40
Array elements are:50
The max element:50
the min elemment:10
2. **Reverse an Array** Task: Given an array, reverse it without using built-in functions.
public class MaxMinFinder {
public static void main(String[] args) {
int num[]={10,20,30,40,50,60};
for(int i=0;i<num.length;i++){
System.out.println("Array elements:"+num[i]);
}
for(int i=0;i<num.length/2;i++){
int rev = num[i];
num[i]= num[num.length-1-i];
num[num.length-1-i]=rev;
}
for(int i=0;i<num.length;i++){
System.out.println("Reverese Elements are:"+num[i]+" ");
}
}
}
Array elements:10
Array elements:20
Array elements:30
Array elements:40
Array elements:50
Array elements:60
Reverese Elements are:60
Reverese Elements are:50
Reverese Elements are:40
Reverese Elements are:30
Reverese Elements are:20
Reverese Elements are:10
*Check if an Array is a Palindrome** Task: Check if an array reads the same forward and backward
public class Palindrome {
public static void main(String[] args) {
int num[]={1,2,2,1};
int n = num.length;
boolean pal = true;
for(int i=0;i<n;i++){
System.out.println("Array elements:"+num[i]);
}
for(int i=0;i<n/2;i++){
if(num[i]!=num[n-1-i]){
pal = false;
break;
}
}
if(pal){
System.out.println("array is palindrome");
}
else{
System.out.println("array is not palindrome");
}
}
}
4. **Find the Second Largest Element** Task: Find the second largest element in an array.
public class SecondLargestElement {
public static void main(String[] args) {
int[] arr = {10, 5, 20, 8, 25, 25, 15};
int lar = Integer.MIN_VALUE;
int sec = Integer.MIN_VALUE;
for (int num : arr) {
if (num > lar) {
sec = lar;
lar = num;
} else if (num > sec && num < lar) {
sec = num;
}
}
if (sec == Integer.MIN_VALUE) {
System.out.println("No second largest element found");
} else {
System.out.println("Second largest element: " + sec);
}
}
}
Task: Move all zeros in an array to the end while maintaining the order of other elements.
public class main {
public static void main(String[] args) {
int[] arr = {0, 1, 0, 3, 12, 0, 5};
int n = arr.length;
int count = 0;
for (int i = 0; i < n; i++) {
if (arr[i] != 0) {
arr[count++] = arr[i];
}
}
while (count < n) {
arr[count++] = 0;
}
for (int num : arr) {
System.out.print(num + " ");
}
}
}
Task: Calculate the sum of all elements in the array.
public class main{
public static void main(String[]args){
int num[] = {10,20,30,40,50,60,70,80,90};
int sum =0;
for(int arr:num){
sum+=arr;
}
System.out.println("The sum of array elements are:"+sum);
}
}
The sum of array elements are:450
Task: Given an array containing n-1 numbers from the range [1, n], find the missing number.
public class MissingNumber {
public static void main(String[] args) {
int[] arr = {1, 2, 4, 5};
int n = arr.length + 1;
int totalSum = n * (n + 1) / 2;
int arraySum = 0;
for (int num : arr) {
arraySum += num;
}
int missingNumber = totalSum - arraySum;
System.out.println("Missing number: " + missingNumber);
}
}
Count of numbers using hashmap
import java.util.*;
class OccurrenceOfNumbers {
static void numberCount(int[] numbers) {
HashMap<Integer, Integer> numCountMap = new HashMap<>();
for (int num : numbers) {
if (numCountMap.containsKey(num)) {
numCountMap.put(num, numCountMap.get(num) + 1);
} else {
numCountMap.put(num, 1);
}
}
for (Map.Entry<Integer, Integer> entry : numCountMap.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
}
public static void main(String[] args) {
int[] nums = {2, 4, 2, 5, 4, 4, 2};
numberCount(nums);
}
}
Finding the common elements are
public class commonNumber{
public static void main(String[]args){
int arr1[]={1,2,3,4};
int arr2[]={4,3,5,6};
System.out.println("The commone elemests are:");
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr.length;j++){
if(arr[i]==arr[j]){
System.out.println("The common elements are:"+arr[i]+" ")
break;
}
}
}
}
}
Checking the duplicates
import java.util.LinkedHashSet;
public class Main {
public static void main(String[] args) {
int arr[] = {1, 2, 1, 1, 2, 4, 7, 8};
LinkedHashSet<Integer> set = new LinkedHashSet<>();
for (int num : arr) {
set.add(num);
}
System.out.print("Array without duplicates: ");
for (int num : set) {
System.out.print(num + " ");
}
}
}
public class Main {
public static void main(String[] args) {
int[] arr = {10, 5, 20, 8, 25};
int largest = Integer.MIN_VALUE;
int secondLargest = Integer.MIN_VALUE;
for (int num : arr) {
if (num > largest) {
secondLargest = largest;
largest = num;
} else if (num > secondLargest) {
secondLargest = num;
}
}
int maxSum = largest + secondLargest;
System.out.println("Largest sum of two elements: " + maxSum);
}
}
Task: Calculate the sum of elements located at odd indices.
public class Main {
public static void main(String[] args) {
int[] arr = {10, 20, 30, 40, 50, 60};
int sum = 0;
for (int i = 1; i < arr.length; i += 2) {
sum += arr[i];
}
System.out.println("Sum of elements at odd indices: " + sum);
}
}
Task: Find the index of the first occurrence of a target element. Return -1 if not found.
public class Main {
public static void main(String[] args) {
int[] arr = {4, 7, 1, 9, 7, 3};
int target = 7;
int index = -1;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == target) {
index = i;
break;
}
}
System.out.println("First occurrence index: " + index);
}
}
String is palindrome
public class PalindromeString {
public static void main(String[] args) {
String str = "madam";
int n = str.length();
boolean pal = true;
for(int i = 0; i < n; i++) {
System.out.println("Character: " + str.charAt(i));
}
for(int i = 0; i < n / 2; i++) {
if(str.charAt(i) != str.charAt(n - 1 - i)) {
pal = false;
break;
}
}
if(pal) {
System.out.println("String is palindrome");
} else {
System.out.println("String is not palindrome");
}
}
}
Reverse a string
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string to reverse it:");
String str = sc.nextLine();
char[] ch = str.toCharArray();
for(int i = 0; i < ch.length / 2; i++) {
char temp = ch[i];
ch[i] = ch[ch.length - 1 - i];
ch[ch.length - 1 - i] = temp;
}
String reversed = new String(ch);
System.out.println("Reversed String: " + reversed);
}
}
0
You can add this document to your study collection(s)
Sign in Available only to authorized usersYou can add this document to your saved list
Sign in Available only to authorized users(For complaints, use another form )