0% found this document useful (0 votes)
90 views43 pages

Java 2

The document contains examples of Java programs using basic input/output, operators, decision making constructs. It includes programs to print hello world, take user input, perform calculations, conditional checks for vowels, leap years, and more.

Uploaded by

dhyaviii
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)
90 views43 pages

Java 2

The document contains examples of Java programs using basic input/output, operators, decision making constructs. It includes programs to print hello world, take user input, perform calculations, conditional checks for vowels, leap years, and more.

Uploaded by

dhyaviii
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

I/O

1. Hello World
class Main
{
public static void main(String args[])
{
[Link]("\"Hello, World!\"");
}
}

2. Hello World with tab


class Main
{
public static void main(String args[])
{
//Try out your code here
[Link]("Hello World\tHello World");
}
}

3. Hello world with new line


class Main
{
public static void main(String args[])
{
//Try out your code here
[Link]("Hello World\nHello World");
}
}

4. Student Details
import [Link].*;
class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
String name = [Link]();
int age = [Link]();
Float cgpa = [Link]();
char grade = [Link]().charAt(0);
[Link]("Name: "+name);
[Link]("Age: "+age);
[Link]("CGPA: "+[Link](cgpa*100)/100);
[Link]("Grade: "+grade);
}
}
5. ASCII 1
import [Link].*;
class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
char ch = [Link]().charAt(0);
int ascii = (char)ch;
[Link](ascii);
}
}

6. ASCII 2
import [Link].*;
class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int ascii = [Link]();
char ch = (char)ascii;
[Link](ch);
}
}

7. Round off
import [Link].*;
class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
float n = [Link]();
int a = (int)n;
int b = (int)[Link](n);
int c = (int)[Link](n);
[Link](a);
[Link](b);
[Link](c);
}
}
OPERATORS
1. Fencing the ground
import [Link].*;
class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int l = [Link]();
int b = [Link]();
int length = 2*(l+b);
int area = l*b;
[Link]("The required length is "+length+" m");
[Link]("The required area of carpet is "+area+" sqm");
}
}

2. Newspaper agency
import [Link].*;
class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int a = [Link]();
int b = [Link]();
int c = [Link]();
int profit = a*(b-c)-100;
[Link](profit);
}
}

3. Harry potter
import [Link].*;
class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int n = [Link]();
n = [Link](n);
int d1 = n / 1000;
int d4 = n % 10;
[Link](d1+d4);
}
}
4. Splitting into teams
import [Link].*;
class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int frnds = [Link]();
int teams = [Link]();
int frndsPerTeam = frnds / teams;
int frndsLeft = frnds % teams;
[Link]("The number of friends in each team is "+ frndsPerTeam +" and left
out is "+frndsLeft);
}
}

5. Debt repay
import [Link].*;
class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
Float principal = [Link]();
Float rate = [Link]();
Float year = [Link]();
Float interest = (principal*rate*year)/100;
Float totalamt = principal + interest;
Float discount = 0.02f * interest;
Float finalval = totalamt - discount;
[Link]("%.2f%n",interest);
[Link]("%.2f%n",totalamt);
[Link]("%.2f%n",discount);
[Link]("%.2f%n",finalval);
}
}

6. 3 psychos
import [Link].*;
class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int x1 = [Link]();
int y1 = [Link]();
int x2 = [Link]();
int y2 = [Link]();
double mp1 = (x1+x2)/2.0;
double mp2 = (y1+y2)/2.0;
[Link]("Arun's house is located at("+mp1+","+mp2+")");
}
}

7. Hop n hop
import [Link].*;
class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int x = [Link]();
int y = [Link]();
double distance = [Link]([Link](x - 3, 2) + [Link](y - 4, 2));
[Link]([Link](distance));
}
}

8. Dollars and cents


import [Link].*;
class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int d1 = [Link]();
int c1 = [Link]();
int d2 = [Link]();
int c2 = [Link]();
int csum = (c1+c2)/100;
int rem = (c1+c2) % 100;
int dsum = (d1+d2)+((c1+c2)/100);
[Link](dsum);
[Link](rem);

}
}

9. Treasure Hunter
import [Link].*;
class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int coins = [Link]();
int ben = [Link]();
int black = [Link]();
int benShare = (ben * coins)/100; //473
int rem = coins - benShare; //256
int blackShare = (black * rem)/100; //222
int rems = rem - blackShare; //34
int otherShare = (rems/3);
[Link](benShare);
[Link](blackShare);
[Link](otherShare);
}
}

10. Reverse a 3 digit


import [Link].*;
class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int n = [Link]();
int d1 = n / 100;
int d2 = n / 10 % 10;
int d3 = n % 10;
[Link]((d3*100)+(d2*10)+d1);
}
}

11. Tic tac toe


import [Link].*;
class Main {
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
int n = [Link]();
if (n < 1 || n > 9){
[Link]("Error");
}
int index = n - 1;
int row = index / 3;
int col = index % 3;
[Link](row + " " + col);
}
}
DECISION MAKING
1. Checking alphabets
import [Link].*;
class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
char ch = [Link]().charAt(0);
if((ch>='a' && ch<='z')|| (ch>='A' && ch<='Z'))
{
if( ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch=='A' || ch=='E' ||
ch=='I' || ch=='O' || ch=='U' )
{
[Link]("Vowel");
}
else
{
[Link]("Consonant");
}
}
else
{
[Link]("Not an alphabet");
}
}
}

2. Electricity bill
import [Link].*;
class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
int units=[Link]();
if(units<=200){
[Link]( "Rs."+units*5/10);
}
else{
[Link]("Rs."+((units*65/100)+100));
}
}}

3. Online shopping
4. Hotel traffic
import [Link];
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int month = [Link]();
int roomRentPerDay = [Link]();
int daysStayed = [Link]();
if (month < 1 || month > 12) {
[Link]("Invalid Input");
return;
}
boolean isPeakSeason = (month >= 4 && month <= 6) || (month >= 11 &&
month <= 12);
int totalTariff = roomRentPerDay * daysStayed;
if (isPeakSeason) {
totalTariff *= 1.20;
}
[Link](totalTariff);
}
}

5. Gift for bday


import [Link].*;
class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int n = [Link]();
if((n%4 == 0) && (n%100!=0))
[Link](n+" is a leap year");
else
[Link](n+" is not a leap year");
}
}

6. Trendy number
import [Link].*;
class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int n = [Link]();
if(n>=100 && n<=999)
{
int mid = n / 10 % 10;
if(mid % 3 == 0)
{
[Link]("Trendy Number");
}
else
{
[Link]("Not a Trendy Number");
}
}
else
{
[Link]("Invalid Number");
}
}
}

7. Time sheet
import [Link].*;
class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int sun = [Link]();
int mon = [Link]();
int tues = [Link]();
int wed = [Link]();
int thur = [Link]();
int fri = [Link]();
int sat = [Link]();
int ans=0;
ans=(mon+tues+wed+thur+fri)*100;
int satBonus = 125;
int sunBonus = 150;
if(mon>8)
ans=ans+(mon-8)*15;
if(tues>8)
ans=ans+(tues-8)*15;
if(wed>8)
ans=ans+(wed-8)*15;
if(thur>8)
ans=ans+(thur-8)*15;
if(fri>8)
ans=ans+(fri-8)*15;
ans=ans+(sat*satBonus)+(sun*sunBonus);
[Link](ans);
}
}

8. No of days
import [Link].*;
class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
int year = [Link]();
int month = [Link]();
if(month==2)
{
if ((year%4==0 && year%100!=0) || year%400==0)
[Link]("29 Days");
else
[Link]("28 Days");
}
else if(month==4 || month==6 || month==9 || month==11)
[Link]("30 Days");
else
[Link]("31 Days");
}
}

9. Scholarship
import [Link].*;
class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int age = [Link]();
int year = [Link]();
int famIncome = [Link]();
int arrear = [Link]();
Float marks = [Link]();
Float attendance = [Link]();
if((age <=21) && (age >=18) && (year >=2021) && (famIncome <= 200000) &&
(arrear <2) && (marks >= 60) && (attendance >= 80))
{
[Link]("Eligible");
}
else if((arrear >=2) && (marks >=80) && (attendance >=90) && (famIncome >=
200000) && (famIncome <= 250000))
{
[Link]("Partially Eligible");
}
else
{
[Link]("Not Eligible");
}
}
}

10. Mango tree


import [Link].*;
class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int r = [Link]();
int c = [Link]();
int t = [Link]();
if(t<=c || t%c==1 || t%c==0)
{
[Link]("Yes");
}
else
{
[Link]("No");
}
}
}

11. Cricket
import [Link];
class Main
{
public static void main(String[] args)
{
Scanner scanner = new Scanner([Link]);
int totalBalls = [Link]();
int totalRuns = [Link]();
int runsScored = [Link]();
int ballsBowled = [Link]();
float overs,oversFinished,currentRR,totalRR;

overs = totalBalls/6;
int z1= ballsBowled / 6;
int z2= ballsBowled % 6;
oversFinished = z1 + z2 * 0.1f;
currentRR = runsScored / oversFinished;
totalRR = totalRuns/overs;

[Link]((int)overs);
[Link]("%.1f\n", oversFinished);
[Link]("%.1f\n", currentRR);
[Link]("%.1f\n", totalRR);

if (currentRR>totalRR)
{
[Link]("Eligible to Win");
}
else
{
[Link]("Not Eligible to Win");
}
}
}
CONTROL STATEMENTS
1. Multiplication table
import [Link].*;
class Main
{
public static void main(String args[])
{
Scanner sc =new Scanner([Link]);
int n = [Link]();
int m = [Link]();
int val=n;
[Link]("Enter n");
[Link]("Enter m");
[Link]("The multiplication table of "+n+" is");
for(int i=1;i<=m;i++)
{
[Link](i+"*"+n+"="+val);
val=val+n;
}
}
}

2. Prime num
import [Link].*;
class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int n = [Link]();
for(int i=2;i<=n;i++)
{
boolean isPrime = true;
for(int j=2;j<=i/2;j++)
{
if(i%j==0)
{
isPrime = false;
break;
}
}
if(isPrime)
{
[Link](i+" ");
}
}
}
}
3. Special number
import [Link].*;
class Main
{

public static void fun(int n)


{
int a = n%10;
int b = n/10;
int ans = (a*b)+(a+b);
if(ans==n)
[Link](n);
}
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int s = [Link]();
int e = [Link]();
for(int i=s;i<=e;i++)
fun(i);
}
}

4. Amoeba multiplication
import [Link].*;
class Main
{
public static int fib(int n)
{
if(n==1)
return 0;
else if(n==2)
return 1;
else
return (fib(n-1)+fib(n-2));
}
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int n = [Link]();
[Link](fib(n));
}
}

5. Number series
import [Link].*;
class Main
{
public static int fun(int n)
{
if(n%2==0)
return (n*n)-2;
else
return (n*n)-1;
}

public static void main(String args[])


{
Scanner sc = new Scanner([Link]);
int n = [Link]();
for(int i=1;i<=n;i++)
[Link](fun(i)+" ");
}
}

6. Hollow square
import [Link].*;
class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int n = [Link]();
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(i==1 || i==n || j==1 || j==n)
[Link]("*");
else
[Link](" ");
}
[Link]();
}
}
}

7. Treasure finder
import [Link].*;
class Main
{
public static int min(int a,int b,int c)
{
if(a>b && b>c)
return a;
else if(b>a && b>c)
return b;
else
return c;
}
public static int find(int a,int b,int c)
{
for(int i=min(a,b,c);i>=1;i--)
{
if(a%i==0 && b%i==0 && c%i==0)
return i;
}
return 1;
}
public static int max2(int a,int b,int c)
{
if((a>b && a<c) || (a<b && a>c))
return a;
else if((b > a && b < c) || (b < a && b > c))
return b;
else
return c;
}
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int a = [Link]();
int b = [Link]();
int c = [Link]();
int ans = max2(a,b,c);
int ans1 = find(a,b,c);
[Link]("The treasure is in the box which has the number "+ans);
[Link]("The code to open the box is "+ans1);
}
}

8. Collatz problem
import [Link].*;
class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int n = [Link]();
int steps = generateCollatzSequence(n);
[Link](steps);
}
public static int generateCollatzSequence(int n)
{
int steps = 0;
while(n!=1)
{
[Link](n);
if(n%2 == 0)
{
n=n/2;
}
else
{
n=3*n+1;
}
steps++;
}
[Link](n);
return steps;
}
}

9. Strong number
import [Link].*;
class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int n = [Link]();
int temp = n;
int sum=0;
while(n!=0)
{
int i=1;
int fact=1;

int rem=n%10;
while(i<=rem)
{
fact=fact*i;
i++;
}
sum=sum+fact;
n=n/10;
}
if(temp == sum)
[Link]("Yes");
else
[Link]("No");
}
}

10. Inverted right angle triangle


import [Link].*;
class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int n = [Link]();
int i,j;
for(i=n;i>0;i--)
{
for(j=1;j<=i;j++)
{
[Link]("*");
}
[Link]();
}
}
}

11. Sum of digits


import [Link].*;
class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
String input = [Link]();
int number = [Link](input);
int result = sumOfDigits(number);
[Link](result);
}
public static int sumOfDigits(int number)
{
while(number>=10)
{
number = digitalSum(number);
}
return number;
}

public static int digitalSum(int number)


{
int sum=0;
while(number > 0){
sum +=number%10;
number /=10;
}
return sum;
}
}

12. Kaprekar number


import [Link].*;
class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int n = [Link]();
int count = 0;
int temp = n;
int sq = n*n;
while(n!=0)
{
n = n/10%10;
count++;
}
int p = (int)[Link](10,count);
int r = sq % p;
int q = sq/p;
int sum= r+q;
if(temp==sum)
{
[Link]("Kaprekar Number");
}
else
{
[Link]("Not a Kaprekar Number");
}
}
}

13. Trapezium pattern


import [Link].*;
class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int n = [Link]();
int l = 1;
int r = n*n+1;
for(int i=n;i>0;i--)
{
for(int j=n;j>i;j--)
{
[Link]("--");
}
for(int k=i;k>0;k--)
{
[Link](l++ +"*");
}
for(int k=i;k>1;k--)
{
[Link](r++ +"*");
}
[Link](r);
r = r-2*(i-1);
}
}
}
ARRAYS 1D
1. Same or not
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int size1 = [Link]();
int size2 = [Link]();
if (size1 != size2) {
[Link]("Not Same");
return;
}
int[] array1 = new int[size1];
int[] array2 = new int[size2];
for (int i = 0; i < size1; i++) {
array1[i] = [Link]();
}
for (int i = 0; i < size2; i++) {
array2[i] = [Link]();
}
int sum1 = 0;
int sum2 = 0;
for (int num : array1) {
sum1 += num;
}
for (int num : array2) {
sum2 += num;
}
if (sum1 == sum2) {
[Link]("Same");
} else {
[Link]("Not Same");
}
}
}

2. Count distinct elements


import [Link].*;
class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int size = [Link]();
int[] array = new int[size];
for(int i=0;i<size;i++){
array[i] = [Link]();
}
Set<Integer>distinctElements = new HashSet<>();
for(int num : array){
[Link](num);
}
[Link]("There are "+[Link]()+" distinct element in the array.");
}
}

3. Compatible arrays
import [Link].*;
class Main
{
public static void main(String args[])
{
Scanner sc= new Scanner([Link]);
int n1=[Link]();
int[] array1 = new int[n1];
for(int i=0;i<n1;i++){
array1[i]=[Link]();
}
int n2=[Link]();
int[] array2 = new int[n2];
for(int i=0;i<n2;i++)
{
array2[i] = [Link]();
}
boolean compatible = true;
if(n1 != n2){
compatible = false;
}
else
{
for(int i=0;i<n1;i++){
if(array1[i] < array2[i]){
compatible = false;
break;
}
}
}
if(compatible)
{
[Link]("Compatible");
}
else{
[Link]("Incompatible");
}
}
}

4. Sum of even and odd


import [Link].*;
class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int n = [Link]();
int[] array = new int[n];
for(int i=0;i<n;i++){
array[i] = [Link]();
}
int sumEven =0;
int sumOdd = 0;
for(int num:array){
if(num%2==0){
sumEven +=num;
} else{
sumOdd +=num;
}
}
[Link]("The sum of the even numbers in the array is " + sumEven);
[Link]("The sum of the odd numbers in the array is " + sumOdd);
}
}

5. Ascending order
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int size = [Link]();
int[] arr = new int[size];
for (int i = 0; i < size; i++) {
arr[i] = [Link]();
}
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}

[Link]("The Sorted array is:");


for (int i = 0; i < size; i++) {
[Link](arr[i]);
}
}
}
6. Queue
import [Link].*;
class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int n = [Link]();
int m = [Link]();
int[] groups = new int[n];
for(int i=0;i<n;i++)
{
groups[i] = [Link]();
}
int buses = 0;
int passengers = 0;
for(int i=0;i<n;i++)
{
if(passengers + groups[i] <= m)
{
passengers += groups[i];
}
else
{
buses++;
passengers = groups[i];
}
}
if(passengers > 0)
{
buses++;
}
[Link](buses);
}
}

7. Array insertion
8. Remove duplicate elements
import [Link];
import [Link];
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int size = [Link]();
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = [Link]();
}
Set<Integer> set = new LinkedHashSet<>();
for (int num : array) {
[Link](num);
}
for (int num : set) {
[Link](num);
}
[Link]();
}
}
9. Online game
import [Link].*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int size = [Link]();
int[] arr = new int[size];
for (int i = 0; i < size; i++) {
arr[i] = [Link]();
}
segregateArray(arr);
[Link]("Array after Segregation");
for (int num : arr) {
[Link](num + " ");
}
[Link]();
}
public static void segregateArray(int[] arr) {
int left = 0, right = [Link] - 1;

while (left < right) {


while (arr[left] % 2 == 0 && left < right) {
left++;
}
while (arr[right] % 2 != 0 && left < right) {
right--;
}
if (left < right) {
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++;
right--;
}
}
}
}
10. Largest house
import [Link].*;
public class Main
{
static class House implements Comparable<House>
{
int num;
int pos;
public House(int num, int pos)
{
[Link] = num;
[Link] = pos;
}
@Override
public int compareTo(House other)
{
return [Link] - [Link];
}
}
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
int num = [Link]();
House[] houses = new House[num];
for (int i = 0; i < num; i++)
{
int houseNum = [Link]();
int pos = [Link]();
houses[i] = new House(houseNum, pos);
}
[Link](houses);
int maxDistance = Integer.MIN_VALUE;
int house1 = -1, house2 = -1;
for (int i = 0; i < num - 1; i++)
{
int distance = houses[i+1].pos - houses[i].pos;
if (distance > maxDistance)
{
maxDistance = distance;
house1 = houses[i].num;
house2 = houses[i+1].num;
}
}
[Link]([Link](house1, house2) + " " + [Link](house1, house2));
}
}

11. Pair the container


import [Link].*;
class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int numContainers = [Link]();
int[] capacities = new int[numContainers];
for(int i=0;i<numContainers;i++){
capacities[i] = [Link]();
}
[Link](capacities);
int left=0,right = numContainers-1;
while(left<right){
[Link](capacities[right] + " " + capacities[left]);
left++;
right--;
}
if(left == right){
[Link](capacities[left] + " 0");
}
}
}

12. Smallest positive missing


import [Link].*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int n = [Link]();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = [Link]();
}
int missingNumber = findMissingNumber(a);
[Link](missingNumber);
}
public static int findMissingNumber(int[] a) {
int n = [Link];
for (int i = 0; i < n; i++) {
while (a[i] > 0 && a[i] <= n && a[a[i]-1] != a[i]) {
int temp = a[a[i]-1];
a[a[i]-1] = a[i];
a[i] = temp;
}
}
for (int i = 0; i < n; i++) {
if (a[i] != i+1) {
return i + 1;
}
}
return n + 1;
}
}
ARRAYS 2D
1. Transpose Matrix
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int size = [Link]();
int[][] matrix = new int[size][size];
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
matrix[i][j] = [Link]();
}
}
[Link]("Original matrix is:");
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
[Link](matrix[i][j] + " ");
}
[Link]();
}
int[][] transpose = new int[size][size];
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
transpose[j][i] = matrix[i][j];
}
}
[Link]("Transpose matrix is:");
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
[Link](transpose[i][j] + " ");
}
[Link]();
}
[Link]();
}
}

2. Upper triangular matrix


import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int n = [Link]();
int[][] matrix = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
matrix[i][j] = [Link]();
}
}
boolean isUpperTriangular = true;
for (int i = 1; i < n; i++) {
for (int j = 0; j < i; j++) {
if (matrix[i][j] != 0) {
isUpperTriangular = false;
break;
}
}
if (!isUpperTriangular) {
break;
}
}
if (isUpperTriangular) {
[Link]("Upper triangular matrix");
} else {
[Link]("Not an Upper triangular matrix");
}
[Link]();
}
}

3. Max element in each col


import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int m = [Link]();
int n = [Link]();
int[][] matrix = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
matrix[i][j] = [Link]();
}
}
for (int j = 0; j < n; j++) {
int max = matrix[0][j];
for (int i = 1; i < m; i++) {
if (matrix[i][j] > max) {
max = matrix[i][j];
}
}
[Link](max);
}
[Link]();
}
}

4. Matrix multiplication
5. Sum of zigzag
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int r = [Link]();
int c = [Link]();
int[][] a = new int[r][c];
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
a[i][j] = [Link]();
}
}
int sum = 0;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (i == 0 || i == r - 1 || i == j) {
sum += a[i][j];
}
}
}
[Link]("Sum of Zig-Zag pattern is " + sum);
}
}

6. Move all zeros


import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int T = [Link]();

while (T-- > 0) {


String input = [Link]();
StringBuilder result = new StringBuilder();
int countZeroes = 0;
for (int i = 0; i < [Link](); i++) {
if ([Link](i) == '0') {
countZeroes++;
} else {
[Link]([Link](i));
}
}
for (int i = 0; i < countZeroes; i++) {
[Link]('0');
}
[Link](result);
}
[Link]();
}
}

7. Uniformity matrix
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int n = [Link]();
int[][] matrix = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
matrix[i][j] = [Link]();
}
}
boolean isUniform = true;
int firstElement = matrix[0][0] % 2;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i][j] % 2 != firstElement) {
isUniform = false;
break;
}
}
if (!isUniform) {
break;
}
}
if (isUniform) {
[Link]("Yes");
} else {
[Link]("No");
}
[Link]();
}
}

8. Magic square
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int n = [Link]();
int[][] a = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
a[i][j] = [Link]();
}
}
int s1 = 0, s2 = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j) {
s1 += a[i][j];
}
if (i + j == n - 1) {
s2 += a[i][j];
}
}
}
if (s1 == s2) {
[Link]("Yes");
} else {
[Link]("No");
}
}
}
9. Sum of rows and columns
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int rows = [Link]();
int cols = [Link]();
int[][] fruits = new int[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
fruits[i][j] = [Link]();
}
}
int maxRowSum = Integer.MIN_VALUE;
int maxRowIndex = -1;
[Link]("The Sum of rows is ");
for (int i = 0; i < rows; i++) {
int rowSum = 0;
for (int j = 0; j < cols; j++) {
rowSum += fruits[i][j];
}
[Link](rowSum + " ");
if (rowSum > maxRowSum) {
maxRowSum = rowSum;
maxRowIndex = i + 1;
}
}
[Link]("\nRow " + maxRowIndex + " has a maximum sum");
int maxColSum = Integer.MIN_VALUE;
int maxColIndex = -1;
[Link]("The Sum of columns is ");
for (int j = 0; j < cols; j++) {
int colSum = 0;
for (int i = 0; i < rows; i++) {
colSum += fruits[i][j];
}
[Link](colSum + " ");
if (colSum > maxColSum) {
maxColSum = colSum;
maxColIndex = j + 1;
}
}
[Link]("\nColumn " + maxColIndex + " has the maximum sum");
[Link]();
}
}

10. Spiral pattern


import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int N = [Link]();
int[][] matrix = new int[N][N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
matrix[i][j] = [Link]();
}
}
spiralTraversal(matrix);
[Link]();
}
public static void spiralTraversal(int[][] matrix) {
int topRow = 0, bottomRow = [Link] - 1;
int leftCol = 0, rightCol = matrix[0].length - 1;
while (topRow <= bottomRow && leftCol <= rightCol) {
for (int i = leftCol; i <= rightCol; i++) {
[Link](matrix[topRow][i] + " ");
}
topRow++;
for (int i = topRow; i <= bottomRow; i++) {
[Link](matrix[i][rightCol] + " ");
}
rightCol--;

if (topRow <= bottomRow) {


for (int i = rightCol; i >= leftCol; i--) {
[Link](matrix[bottomRow][i] + " ");
}
bottomRow--;
}
if (leftCol <= rightCol) {
for (int i = bottomRow; i >= topRow; i--) {
[Link](matrix[i][leftCol] + " ");
}
leftCol++;
}
}
}
}

11. Matrix rotation


import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int N = [Link]();
int[][] matrix = new int[N][N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
matrix[i][j] = [Link]();
}
}
rotateMatrix(matrix);
printMatrix(matrix);
[Link]();
}
public static void transpose(int[][] matrix) {
int N = [Link];
for (int i = 0; i < N; i++) {
for (int j = i; j < N; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
}
public static void reverseRows(int[][] matrix) {
int N = [Link];
for (int i = 0; i < N; i++) {
int start = 0;
int end = N - 1;
while (start < end) {
int temp = matrix[i][start];
matrix[i][start] = matrix[i][end];
matrix[i][end] = temp;
start++;
end--;
}
}
}
public static void rotateMatrix(int[][] matrix) {
transpose(matrix);
reverseRows(matrix);
}
public static void printMatrix(int[][] matrix) {
int N = [Link];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
[Link](matrix[i][j] + " ");
}
[Link]();
}
}
}
RECURSION
1. Factorial of a number
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int number = [Link]();
[Link]();
int factorial = computeFactorial(number);
[Link]("The factorial of " + number + " is " + factorial);
}
public static int computeFactorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * computeFactorial(n - 1);
}
}
}

2. Number of digits
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a non-negative integer:");
int number = [Link]();
[Link]();

int numDigits = countDigits(number);

[Link]("The number of digits in " + number + " is " + numDigits);


}

public static int countDigits(int n) {


if (n < 10) {
return 1;
} else {
return 1 + countDigits(n / 10);
}
}
}

3. Sum of array elements


import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int n = [Link]();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = [Link]();
}
[Link]();
int sum = calculateSum(arr, n);
[Link](sum);
}
public static int calculateSum(int[] arr, int n) {
if (n <= 0) {
return 0;
} else {
return arr[n - 1] + calculateSum(arr, n - 1);
}
}
}

4. Fibonacci Series
import [Link];
public class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int n, t1 = 0, t2 = 1, nextTerm = 0, i;
n = [Link]();
if(n == 0 || n == 1)
[Link](n);
else
nextTerm = t1 + t2;
for (i = 3; i <= n; ++i)
{
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
[Link]("The term "+n+" in the Fibonacci series is "+t2);
}
}

5. Compute a^n
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int a = [Link]();
int n = [Link]();
[Link]();
long result = power(a, n);
[Link]("The value of " + a + " power " + n + " is " + result);
}
public static long power(int a, int n) {
if (n == 0) {
return 1;
} else if (n % 2 == 0) {
long temp = power(a, n / 2);
return temp * temp;
} else {
long temp = power(a, n / 2);
return a * temp * temp;
}
}
}

6. Sum of positive odd numbers


import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int n = [Link]();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = [Link]();
}
[Link]();
int sum = sumPositiveOdd(arr, n);
[Link]("Sum = " + sum);
}
public static int sumPositiveOdd(int[] arr, int n) {
if (n <= 0) {
return 0;
}
int currentSum = 0;
if (arr[n - 1] > 0 && arr[n - 1] % 2 != 0) {
currentSum = arr[n - 1];
}
return currentSum + sumPositiveOdd(arr, n - 1);
}
}

7. Max element in an array


import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int size = [Link]();
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = [Link]();
}
[Link]();
int max = findMax(array, size);
[Link]("Maximum element in the array is " + max);
}
public static int findMax(int[] array, int size) {
if (size == 1) {
return array[0];
}
int maxRest = findMax(array, size - 1);
return [Link](maxRest, array[size - 1]);
}
}

8. Prime number
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int number = [Link]();
[Link]();
if (isPrime(number, 2)) {
[Link]("Prime Number");
} else {
[Link]("Not a Prime Number");
}
}
public static boolean isPrime(int n, int divisor) {
if (n <= 2) {
return (n == 2) ? true : false;
}
if (n % divisor == 0) {
return false;
}
if (divisor * divisor > n) {
return true;
}
return isPrime(n, divisor + 1);
}
}

9. GCD of 2 numbers
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int num1 = [Link]();
int num2 = [Link]();
[Link]();
int gcdResult = gcd(num1, num2);
[Link](gcdResult);
}
public static int gcd(int num1, int num2) {
if (num2 == 0) {
return num1;
}
return gcd(num2, num1 % num2);
}
}

10. Decimal to binary


import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int decimal = [Link]();
[Link]();
String binary = convertToBinary(decimal);
[Link](binary);
}
public static String convertToBinary(int decimal) {
if (decimal == 0) {
return "0";
}
return convertToBinaryHelper(decimal, "");
}
public static String convertToBinaryHelper(int decimal, String binary) {
if (decimal == 0) {
return binary;
}
binary = (decimal % 2) + binary;
return convertToBinaryHelper(decimal / 2, binary);
}
}
STRINGS
1. Count the vowels
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
String a = [Link]();
int c = 0, vc = 0;
for (int i = 0; i < [Link](); i++) {
char z = [Link](i);
if (z == 'a' || z == 'e' || z == 'i' || z == 'o' || z == 'u' || z == 'A' || z == 'E' || z == 'I' || z == 'O'
|| z == 'U')
vc++;
}
[Link]("Number of vowels: %d", vc);
[Link]();
}
}

2. Palindrome
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
String a = [Link]();
int c = [Link]();
int i = 0, j = c - 1, f = 0;
while (i < j) {
if ([Link](i) != [Link](j))
f = 1;
i++;
j--;
}
if (f == 0)
[Link]("Palindrome");
else
[Link]("Not a Palindrome");
[Link]();
}
}

3. First non repeating


4. Sorting
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
String a = [Link]();
char[] charArray = [Link]();
for (int i = 0; i < [Link]; i++) {
for (int j = i + 1; j < [Link]; j++) {
if (charArray[i] > charArray[j]) {
char temp = charArray[i];
charArray[i] = charArray[j];
charArray[j] = temp;
}
}
}
[Link](new String(charArray));
[Link]();
}
}

5. Wordakshari
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
String[] a = new String[10];
int n = 0;
while (true) {
a[n] = [Link]();
if (a[n].equals("####"))
break;
n++;
}
[Link](a[0]);
int i = 0, j = 1;
while (i < n - 1) {
char l = a[i].charAt(a[i].length() - 1);
if (l == a[j].charAt(0)) {
[Link](a[j]);
} else {
break;
}
i++;
j++;
}
[Link]();
}
}

6. Reverse each word


import [Link];
public class Main {
public static void reverseSentence(String A) {
int len = [Link]();
int count = 0;
for (int i = len - 1; i >= 0; i--) {
if ([Link](i) != ' ') {
count++;
} else {
for (int j = i + 1; j <= (i + count); j++) {
[Link]([Link](j));
}
count = 0;
[Link](" ");
}
}
for (int i = 0; i < count; i++) {
[Link]([Link](i));
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
String A = [Link]();
reverseSentence(A);
[Link]();
}
}

7. String subsequence
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
String a = [Link]();
String b = [Link]();
int s1 = [Link]();
int s2 = [Link]();

int i = 0, j = 0;
while (i < s1 && j < s2) {
if ([Link](i) == [Link](j)) {
j++;
}
i++;
}
if (j >= s2) {
[Link]("1");
} else {
[Link]("0");
}
[Link]();
}
}

8. Anagram
import [Link];
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
String firstString = [Link]();
String secondString = [Link]();
if (areAnagrams(firstString, secondString)) {
[Link]("Anagram");
} else {
[Link]("Not Anagram");
}
[Link]();
}
public static boolean areAnagrams(String str1, String str2) {
if ([Link]() != [Link]()) {
return false;
}
char[] charArray1 = [Link]();
char[] charArray2 = [Link]();
[Link](charArray1);
[Link](charArray2);
return [Link](charArray1, charArray2);
}
}

9. Encrypted string
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int n = [Link]();
int c = (int) (Math.log10(n) + 1);
int[] a = new int[c];
int i = c - 1;
while (n > 0) {
a[i--] = n % 10;
n = n / 10;
}
for (i = 0; i < c - 1; i++) {
int t = a[i];
a[i] = a[i + 1];
a[i + 1] = t;
i++;
}
int ans = 0;
for (i = 0; i < c; i++) {
ans = ans * 10 + a[i];
}
[Link](ans);
}
}
10. Count the sum
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
String a = [Link]();
int ans = 0, fans = 0;
for (int i = 0; i < [Link](); i++) {
if ([Link]([Link](i))) {
ans = ans * 10 + ([Link](i) - '0');
} else {
fans += ans;
ans = 0;
}
}
fans += ans;
[Link](fans);
}
}

11. Remove all char


import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
String a = [Link]();
String b = [Link]();
StringBuilder result = new StringBuilder();
for (int i = 0; i < [Link](); i++) {
boolean found = false;
for (int j = 0; j < [Link](); j++) {
if ([Link](i) == [Link](j)) {
found = true;
break;
}
}
if (!found) {
[Link]([Link](i));
}
}
[Link]([Link]());
}
}
STRUCTURE AND UNION
1. Student details
2. Add two distances
3. Time differences
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int h1, m1, s1, h2, m2, s2;
h1 = [Link]();
m1 = [Link]();
s1 = [Link]();
h2 = [Link]();
m2 = [Link]();
s2 = [Link]();
int s = s1 - s2;
int m = m1 - m2;
int h = h1 - h2;
[Link]("TIME DIFFERENCE: %d:%d:%d - %d:%d:%d = %d:%d:%d", h1, m1, s1, h2,
m2, s2, h, m, s);
}
}

You might also like