JYOTI Prog

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

2

import java.io.*;

class EvenOddSum {

public static void main(String args[])

int arr[] = { 1, 2, 3, 4, 5, 6 };

int even = 0, odd = 0;

// Loop to find even, odd sum

for (int i = 0; i < arr.length; i++) {

if (i % 2 == 0)

even += arr[i];

else

odd += arr[i];

System.out.println("Even index positions sum: " + even);

System.out.println("Odd index positions sum: " + odd);

}
4
// Java program to convert a decimal

// number to binary number

import java.io.*;

class GFG {

// function to convert decimal to binary

static void decToBinary(int n)

// array to store binary number

int[] binaryNum = new int[32];

// counter for binary array

int i = 0;

while (n > 0) {

// storing remainder in binary array

binaryNum[i] = n % 2;

n = n / 2;

i++;

// printing binary array in reverse order

for (int j = i - 1; j >= 0; j--)

System.out.print(binaryNum[j]);

}
// driver program

public static void main(String[] args)

int n = 17;

decToBinary(n);

}
6
import java.util.*;

import java.lang.*;

class GFG{

static int computeSalary(int basic,char grade)

double allowance;

double hra, da, pf;

hra = 0.2 * basic;

da = 0.5 * basic;

pf = 0.11 * basic;

if (grade == 'A')

allowance = 1700.0;

else if (grade == 'B')

allowance = 1500.0;

else

allowance = 1300.0;

}
double gross;

gross = Math.round(basic + hra + da +

allowance - pf);

return (int)gross;

public static void main (String[] args)

int basic = 10000;

char grade = 'A';

// Function call

System.out.println(computeSalary(basic, grade));

}
8

public class Main {

public static int removeduplicates(int a[], int n)

if (n == 0 || n == 1) {

return n;

int[] temp = new int[n];

int j = 0;

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

if (a[i] != a[i + 1]) {

temp[j++] = a[i];

temp[j++] = a[n - 1];

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

a[i] = temp[i];

return j;

public static void main(String[] args)

{
int a[] = { 1, 1, 2, 2, 2 };

int n = a.length;

n = removeduplicates(a, n);

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

System.out.print(a[i] + " ");

}
9
import java.util.*;

public class Main

public static void main(String[] args)

Scanner sc = new Scanner(System.in);

int[] arr = new int[1000];

int[] freq = new int[1000];

int size, i, j, count;

System.out.print(“nEnter size of array: “);

size = sc.nextInt();

System.out.print(“nEnter elements in array: “);

for(i=0; i<size; i++)

arr[i] = sc.nextInt();

freq[i] = -1;

for(i=0; i<size; i++)

count = 1;

for(j=i+1; j<size; j++)

if(arr[i]==arr[j])

count++;

freq[j] = 0;
}

if(freq[i] != 0)

freq[i] = count;

System.out.print(“nFrequency of all elements of array : n”);

for(i=0; i<size; i++)

if(freq[i] != 0)

System.out.print(arr[i] + ” occurs ” + freq[i] + ” times” + “n”);

}
13

3 ARRAY accept vala

import java.util.Scanner;

public class Kboat3Arrays


{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);

int P[] = new int[6];


int Q[] = new int[4];
int R[] = new int[10];
int i = 0;

System.out.println("Enter 6 elements of array P:");


for (i = 0; i < P.length; i++) {
P[i] = in.nextInt();
}

System.out.println("Enter 4 elements of array Q:");


for (i = 0; i < Q.length; i++) {
Q[i] = in.nextInt();
}

i = 0;
while(i < P.length) {
R[i] = P[i];
i++;
}

int j = 0;
while(j < Q.length) {
R[i++] = Q[j++];
}

System.out.println("Elements of Array R:");


for (i = 0; i < R.length; i++) {
System.out.print(R[i] + " ");
}
}
}
18
import java.util.Scanner;

public class KboatStdCodes


{
public static void main(String args[]) {
final int SIZE = 10;
Scanner in = new Scanner(System.in);
String cities[] = new String[SIZE];
String stdCodes[] = new String[SIZE];
System.out.println("Enter " + SIZE +
" cities and their STD codes:");

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


System.out.print("Enter City Name: ");
cities[i] = in.nextLine();
System.out.print("Enter its STD Code: ");
stdCodes[i] = in.nextLine();
}

System.out.print("Enter name of city to search: ");


String city = in.nextLine();

int idx;
for (idx = 0; idx < SIZE; idx++) {
if (city.compareToIgnoreCase(cities[idx]) == 0) {
break;
}
}

if (idx < SIZE) {


System.out.println("Search Successful");
System.out.println("City: " + cities[idx]);
System.out.println("STD Code: " + stdCodes[idx]);
}
else {
System.out.println("Search Unsuccessful");
}
}
}
17
import java.util.Scanner;

public class KboatSelectionSort


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
double weightArr[] = new double[10];
System.out.println("Enter weights of 10 people: ");
for (int i = 0; i < 10; i++) {
weightArr[i] = in.nextDouble();
}

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


int idx = i;
for (int j = i + 1; j < 10; j++) {
if (weightArr[j] > weightArr[idx])
idx = j;
}

double t = weightArr[i];
weightArr[i] = weightArr[idx];
weightArr[idx] = t;
}

System.out.println("Sorted Weights Array:");


for (int i = 0; i < 10; i++) {
System.out.print(weightArr[i] + " ");
}
}
}
19
class BinarySearch {

// Returns index of x if it is present in arr[l..

// r], else return -1

int binarySearch(int arr[], int l, int r, int x)

if (r >= l) {

int mid = l + (r - l) / 2

if (arr[mid] == x)

return mid;

if (arr[mid] > x)

return binarySearch(arr, l, mid - 1, x);

return binarySearch(arr, mid + 1, r, x);

// We reach here when element is not present

// in array

return -1;

public static void main(String args[])

BinarySearch ob = new BinarySearch();

int arr[] = { 2, 3, 4, 10, 40 };

int n = arr.length;
int x = 10;

int result = ob.binarySearch(arr, 0, n - 1, x);

if (result == -1)

System.out.println("Element not present");

else

System.out.println("Element found at index " + result);

You might also like