LOGICAL
PROGRAMS
BY
PUNNAM RADHIKA REDDY
Class—1----------------20-11-2023---------------MONDAY
Concept: How to use temp variable
1)Write a program for swapping of two numbers
package Conditions_OR_BasicInstructions;
public class Swapping_of_two_variables {
public static void main(String[] args) {
METHOD-1: Swapping_of_two_variables by temp variable
int a = 10;
int b=20;
//Logic starts here
int temp =a;
a=b;
b=temp;
[Link](a);
[Link](b);
//METHOD-2: Swapping_of_two_variables without temp
variable by using arithmetic operators
int a = 10;
int b=20;
//Logic starts here
a=a+b;
b=a-b;
a=a-b;
[Link](a);
[Link](b);
}
Output:
20
10
2)Write a program for swapping of three numbers
package Conditions_OR_BasicInstructions;
public class Swapping_of_three_variables {
public static void main(String[] args) {
//METHOD-1: Swapping_of_three_variables by temp
variable
//A to B, B to c, C to A o/p =30,10,20
int a=10;
int b=20;
int c=30;
//Logic starts here
/*int temp=a;
a=c;
c=b;
b=temp;*/
/*int temp =b;
b=a;
a=c;
c=temp;*/
/*int temp=c;
c=b;
b=a;
a=temp;
[Link](a);
[Link](b);
[Link](c);*/
//METHOD-2: Swapping_of_three_variables without temp
variable by using arithmetic operators
b=b+c;//20+30=50
c=b-c;//50-30=20
b=b-c;//50-20=30
b=b+a;//30+10=40
a=b-a;//40-10=30
b=b-a;//40-30=10
[Link](a);
[Link](b);
[Link](c);
}
Output:
30
10
20
3)Write a program for swapping of four numbers
package Conditions_OR_BasicInstructions;
public class Swapping_of_four_variables {
public static void main(String[] args) {
//METHOD-1: Swapping_of_four_variables by temp
variable
//A to B, B to C, C to D and D to A[O/P: 40, 10, 20, 30]
int a=10;
int b=20;
int c=30;
int d=40;
/*int temp=a;
a=d;
d=c;
c=b;
b=temp;*/
/*int temp=b;
b=a;
a=d;
d=c;
c=temp;
*/
/*int temp=c;
c=b;
b=a;
a=d;
d=temp;
[Link](a+"\n"+b+"\n"+c+"\n"+d);*/
//METHOD-2: Swapping_of_four_variables without temp
variable by using arithmetic operators
/* b=b+c;//20+30=50
c=b-c;//50-30=20
b=b-c;//50-20=30
b=b+a;//30+10=40
a=b-a;//40-10=30
b=b-a;//40-30=10
d=d+a;//40+30=70
a=d-a;//70-30=40
d=d-a;//70-40=30*/
// Swapping without a temporary variable using XOR
b = b ^ c ^ (c = b);
a = a ^ b ^ (b = a);
d = d ^ a ^ (a = d);
[Link](a);
[Link](b);
[Link](c);
[Link](d);
}
Output:
40
10
20
30
Class—2----------------------21-11-2023---------------------TUESDAY
Concept: Using temp in Array related programs
=>When Array has some values, we will apply rotation.
1)Write a program to shift numbers to left side direction(Clock Wise
Rotation Of Array).
package Conditions_OR_BasicInstructions;
public class ClockRotationOfArray {
public static void main(String[] args) {
// 0 1 2 3 4 5 6
int arr[] = {4,2,6,8,9,5,7};
//O/P:{2,6,8,9,5,7,4}
[Link]("Before Clock or Left Rotation: ");
for(int i=0;i<[Link];i++) {
[Link]("Index is "+i + " value
is:"+arr[i]);
}
[Link]("==========================");
/*Basic Understanding:
arr[0]=arr[1];
arr[1]=arr[2];
arr[2]=arr[3];
arr[3]=arr[4];
arr[4]=arr[5];
arr[5]=arr[6];
arr[6]=arr[0];
*/
//Level 0:
/*int temp=arr[0];
arr[0]=arr[1];
arr[1]=arr[2];
arr[2]=arr[3];
arr[3]=arr[4];
arr[4]=arr[5];
arr[5]=arr[6];
arr[6]=temp;*/
// level 1:
/*
int temp = arr[0]; // {4,2,6,8,9}
arr[0] = arr[1]; // {2,2,6,8,9}
arr[1] = arr[2]; // {2,6,6,8,9}
arr[2] = arr[3]; // {2,6,8,8,9}
arr[3] = arr[4]; // {2,6,8,9,9}
arr[4] = temp; // {2,6,8,9,4}
*/
/*
* Level 2:
int temp = arr[0]; // {4,2,6,8,9}
for(int i=0 ;i <=3 ; i++) {
arr[i] = arr[i+1];
}
arr[4] = temp; // {2,6,8,9,4}
*/
//Level-3:
int temp = arr[0]; // {4,2,6,8,9}
for(int i=0 ;i <=[Link]-2; i++) {
arr[i] = arr[i+1];
}
arr[[Link]-1] = temp; // {2,6,8,9,4}
[Link]("After Clock or left Rotation: ");
for(int i=0;i<[Link];i++) {
[Link]("Index is "+i + " value
is:"+arr[i]);
}
}
}
Output:
Before Clock or Left Rotation:
Index is 0 value is:4
Index is 1 value is:2
Index is 2 value is:6
Index is 3 value is:8
Index is 4 value is:9
Index is 5 value is:5
Index is 6 value is:7
==========================
After Clock or left Rotation:
Index is 0 value is:2
Index is 1 value is:6
Index is 2 value is:8
Index is 3 value is:9
Index is 4 value is:5
Index is 5 value is:7
Index is 6 value is:4
2)Write a program to shift numbers to right side direction(AntiClock
Wise Rotation Of Array).
package Conditions_OR_BasicInstructions;
public class AntiClockRotationOfArray {
public static void main(String[] args) {
// 0 1 2 3 4 5
int arr[] = {4,2,6,8,9,10,55,33,54};
// 0 1 2 3 4 5
// {4,4,4,4,4,4};
// {2,6,8,9,10,1,3,4};
[Link]("Before AntiClock or right
Rotation: ");
for(int i=0;i<[Link];i++) {
[Link]("Index is "+i + " value
is:"+arr[i]);
}
[Link]("==========================");
/*Basic Understanding:
arr[1]=arr[0];
arr[2]=arr[1];
arr[3]=arr[2];
arr[4]=arr[3];
arr[5]=arr[4];
arr[6]=arr[5];
arr[7]=arr[6];
*/
/*
Level 0:
int temp = arr[1];
arr[1] = arr[0];
int temp2 = arr[2];
arr[2] = temp;
int temp3 = arr[3];
arr[3] = temp2;
int temp4 = arr[4];
arr[4] = temp3;
int temp5 = arr[5];
arr[5] = temp4;
arr[0] = temp5;
*/
/*
* Level 1
int temp = arr[5]; // {4,2,6,8,9,10}
arr[5] = arr[4]; // {4,2,6,8,9,9}
arr[4] = arr[3]; // {4,2,6,8,8,9}
arr[3] = arr[2]; // {4,2,6,6,8,9}
arr[2] = arr[1]; // {4,2,2,6,8,9}
arr[1] = arr[0]; // {4,4,2,6,8,9}
arr[0] = temp;
*/
// Level 2
/*
int temp = arr[5];
for(int i=5 ; i>=1;i--) {
arr[i] = arr[i-1];
}
arr[0] = temp;
*/
// Level 3
int temp = arr[[Link]-1];
for(int i=[Link]-1 ; i>=1;i--) {
arr[i] = arr[i-1];
}
arr[0] = temp;
[Link]("After AntiClock or right
Rotation: ");
for(int i=0;i<[Link];i++) {
[Link]("Index is "+i + " value
is:"+arr[i]);
}
Output:
Before AntiClock or right Rotation:
Index is 0 value is:4
Index is 1 value is:2
Index is 2 value is:6
Index is 3 value is:8
Index is 4 value is:9
Index is 5 value is:10
Index is 6 value is:55
Index is 7 value is:33
Index is 8 value is:54
==========================
After AntiClock or right Rotation:
Index is 0 value is:54
Index is 1 value is:4
Index is 2 value is:2
Index is 3 value is:6
Index is 4 value is:8
Index is 5 value is:9
Index is 6 value is:10
Index is 7 value is:55
Index is 8 value is:33
----22-11-2023-----WEDNESDAY---------NO CLASS---------
-------------23-11-2023--------THURSDAY-------DAY-3----------------
Concept: Modulus Operator
Modulus - % [Example: 10%5 = 0(REMAINDER)]
Division - / [Example: 10/5 = 2(QUOTIENT)]
1)Write a program to check a number even or odd
package Modulus;
public class EvenOROdd {
public static void main1() {
int a = 15;
[Link](a%5);//modulus operator it will give
you remainder(0)
[Link](a/5);//division operator it will give you
quotient(3)
}
public static void main(String[] args) {
int num = 37;//35//36
[Link](num%2);
[Link](num/2);
if(num%2 == 0 ) {
[Link]("It is a even number: "+num);
}
else {
[Link]("It is a odd number: "+num);
}
}
}
Output:
1
18
It is a odd number: 37
2)Write a program to print even no’s from array of elements
package Modulus;
public class Even_nos_from_array_of_elements {
public static void main(String[] args) {
int arr[] = {5,2,4,1,7,2,9,8};
for(int i=0;i<[Link];i++) {
if(arr[i]%2==0) {
[Link]("Even no in array:
"+arr[i]);
}
}
}
Output:
Even no in array: 2
Even no in array: 4
Even no in array: 2
Even no in array: 8
3)Write a program to print even index values in array
package Modulus;
//From Given Index of values. Take the Even index and go inside it
and print the value inside it.
public class Even_Index_Values_In_Array {
public static void main(String[] args) {
int arr[] = {5,2,4,1,7,2,9,8};
for(int i=0;i<[Link];i++) {
if(i%2==0) {
//Zero is even number,2,4,6 = (5,4,7,9)
[Link]("Values in Even Indexes:
"+arr[i]);
}
}
}
}
Output:
Values in Even Indexes: 5
Values in Even Indexes: 4
Values in Even Indexes: 7
Values in Even Indexes: 9
/*Sir Said Program In Class
package [Link];
public class Module {
public static void main1(String[] args) {
int a = 15;
[Link](a/15);
[Link](a%15);
}
public static void main2(String[] args) {
int num = 36;
[Link](num/2);
[Link](num % 2); // 0 or 1
if( num % 2 == 0 ) {
[Link]("It is even");
}else {
[Link]("It is odd");
}
}
public static void main3(String[] args) {
// even values.
int arr[] = {5,2,4,1,7,2,9,8};
for(int i=0; i<=[Link]-1; i++) {
if( arr[i] % 2 == 0 ) {
[Link](arr[i]);
}
}
}
public static void main4(String[] args) {
// 0 1 2 3 4 5 6 7
int arr[] = {5,2,4,1,7,2,9,8};
for(int i=0;i<[Link];i++) {
if( i % 2 == 0) {
[Link](arr[i]);
}
}
}
public static void main5(String[] args) {
// 0 1 2 3 4 5 6 7 8
int arr[] = {5,2,4,1,7,2,9,8,10};
for(int i=0;i<[Link];i++) {
if(i%2==0 & arr[i]%2==0 ) {
[Link](arr[i]);
}
}
}
public static void main6(String[] args) {
int arr[] = {5,2,4,1,7,2,9,8,10};
int acc=0;
for(int i=0;i<[Link];i++) {
if(arr[i] %2 ==0) {
acc++;
}
}
[Link](acc);
}
public static void main7(String[] args) {
int arr[] = {5,2,4,1,7,2,9,8,10};
int acc=0;
for(int i=0;i<[Link];i++) {
if(arr[i]%2==0) {
acc =acc + arr[i];
}
}
[Link](acc);
}
public static void main(String[] args) {
int arr[] = {5,2,4,1,7,2,9,8,10};
int acc=1;
for(int i=0;i<[Link];i++) {
if(arr[i]%2 ==0) {
acc = acc * arr[i];
}
}
[Link](acc);
}
}
*/
4)Write a program to print even index with even number in array
package Modulus;
public class EvenIndex_with_EvenNo {
public static void main(String[] args) {
int arr[] = {5,2,4,1,7,2,9,8};
for(int i=0;i<[Link];i++)
if(i%2==0 & arr[i]%2==0) {
[Link]("EvenIndex & No Values in
index : "+arr[i]);
}
}
Output:
EvenIndex & No Values in index : 4
5)Write a program to print count of even numbers in array
package Modulus;
public class EvenNo_Count_In_a_Array {
public static void main(String[] args) {
int arr[] = {5,2,4,1,7,2,9,8};
int accumulator =0;
for(int i=0;i<[Link];i++) {
if(arr[i]%2==0) {
accumulator++;
}
}
[Link]("Count of even no's in array:
"+accumulator);
}
Output:
Count of even no's in array: 4
6)Write a program to print sum of even numbers in array
package Modulus;
public class Sum_Of_Even_Nos_In_Array {
public static void main(String[] args) {
int arr[] = {5,2,4,1,7,2,9,8};
int accumulator =0;
for(int i=0;i<[Link];i++) {
if(arr[i]%2==0) {
accumulator=accumulator+arr[i];
}
}
[Link]("Sum of even no's in array:
"+accumulator);
}
Output: Sum of even no's in array: 16
7)Write a program to print multiplication of even numbers in array
package Modulus;
public class Mul_Of_Even_Nos_In_Array
public static void main(String[] args) {
int arr[] = {5,2,4,1,7,2,9,8};
int accumulator =1;
for(int i=0;i<[Link];i++) {
if(arr[i]%2==0) {
accumulator=accumulator*arr[i];
}
}
[Link]("Mul of Even no's in array:
"+accumulator);
}
Output:
Mul of Even no's in array: 128
8)Write a program to print multiplication of odd numbers in array
package Modulus;
public class Mul_Of_Odd_Nos_In_Array {
public static void main(String[] args) {
int arr[] = {5,2,4,1,7,2,9,8};
int accumulator =1;
for(int i=0;i<[Link];i++) {
if(arr[i]%2==1) {
accumulator=accumulator*arr[i];
}
}
[Link]("Mul of Odd no's in array:
"+accumulator);
}
}
Output:
Mul of Odd no's in array: 315
9)Write a program to print sum of odd numbers in array
package Modulus;
public class Sum_Of_Odd_Nos_In_Array {
public static void main(String[] args) {
int arr[] = {5,2,4,1,7,2,9,8};
int accumulator =0;
for(int i=0;i<[Link];i++) {
if(arr[i]%2==1) {
accumulator=accumulator+arr[i];
}
}
[Link]("Sum of Odd no's in array:
"+accumulator);
}
Output:
Sum of Odd no's in array: 22
10)Write a program to print multiplication of all array values whose
index values is even in array.
package Modulus;
public class
Multiplication_Of_All_Array_Values_Whose_Index_Is_Even {
public static void main(String[] args) {
int arr[] = {5,2,4,1,7,2,9,8};
int acc=1;
for(int i=0;i<[Link];i++) {
if(i%2==0) {
acc=acc*arr[i];
}
}
[Link]("Multiplication_Of_All_Array_Values_Whose_In
dex_Is_Even: "+acc);
}
}
Output:
Multiplication_Of_All_Array_Values_Whose_Index_Is_Even: 1260
11)Write a program to print multiplication of all array values whose
index values is odd in array.
package Modulus;
public class
Multiplication_Of_All_Array_Values_Whose_Index_Is_Odd {
public static void main(String[] args) {
int arr[] = {5,2,4,1,7,2,9,8};
int acc=1;
for(int i=0;i<[Link];i++) {
if(i%2==1) {
acc=acc*arr[i];
}
}
[Link]("Multiplication_Of_All_Array_Values_Whose_In
dex_Is_Odd: "+acc);
}
Output:
Multiplication_Of_All_Array_Values_Whose_Index_Is_Odd: 32
12)Write a program to print multiplication of all array values in
array.
package Modulus;
public class Multiplication_of_All_Array_Values {
public static void main(String[] args) {
int arr[] = {5,2,4,1,7,2,9,8};
int acc =1;
for(int i=0;i<[Link];i++) {
acc=acc*arr[i];
}
[Link]("Multiplication Of Array Values:
"+acc);
}
}
Output:
Multiplication Of Array Values: 40320
13)Write a program to print Odd index values in array.
package Modulus;
public class Odd_Index_Values_In_Array {
public static void main(String[] args) {
int arr[] = {5,2,4,1,7,2,9,8};
//1,3,5,7=(2,1,2,8)
for(int i=0;i<[Link];i++) {
if(i%2==1) {
[Link]("Values in Odd Indexes:
"+arr[i]);
}
}
}
}
Output:
Values in Odd Indexes: 2
Values in Odd Indexes: 1
Values in Odd Indexes: 2
Values in Odd Indexes: 8
14)Write a program to print Odd numbers from array of elements in
array.
package Modulus;
public class Odd_nos_from_array_of_elements {
public static void main(String[] args) {
int arr[] = {5,2,4,1,7,2,9,8};
for(int i=0;i<[Link];i++) {
if(arr[i]%2==1) {
[Link]("Odd no is array is: "+arr[i]);
}}
}
}
Output:
Odd no is array is: 5
Odd no is array is: 1
Odd no is array is: 7
Odd no is array is: 9
15)Write a program to print odd index with odd number in array
package Modulus;
public class OddIndex_with_OddNo {
public static void main(String[] args) {
int arr[] = {5,2,4,1,7,2,9,8};
for(int i=0;i<[Link];i++) {
if(i%2==1 & arr[i]%2==1) {
[Link]("OddIndex & No Values in
index : "+arr[i]);
}
}
}
}
Output:
OddIndex & No Values in index : 1
16)Write a program to print odd number count in a array.
package Modulus;
public class OddNo_Count_In_a_Array {
public static void main(String[] args) {
int arr[] = {5,2,4,1,7,2,9,8};
int accumulator =0;
for(int i=0;i<[Link];i++) {
if(arr[i]%2==1) {
accumulator++;
}
}
[Link]("Count of Odd no's in array: "+accumulator);
}
}
Output:
Count of Odd no's in array: 4
17)Write a program to sum of Array Values in a array.
package Modulus;
public class Sum_Of_Array_Values {
public static void main(String[] args) {
int arr[] = {5,2,4,1,7,2,9,8};
int acc = 0;
for(int i=0;i<[Link];i++) {
acc=acc+arr[i];
}
[Link]("Sum of Array Values: "+acc);
Output:
Sum of Array Values: 38
-------------24-11-2023--------FRIDAY-------DAY-4----------------
1)Write a program to Find_Nos_Divisible_By_Five
package Divisible_By_Nos;
public class Find_Nos_Divisible_By_Five {
public static void main(String[] args) {
int arr[]= {5,2,12,10,89,90};
for(int i=0;i<[Link];i++) {
if(arr[i]%5==0) {
[Link](arr[i]);
}
}
Output:
5
10
90