Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

javaexercise

The document contains multiple Java programs demonstrating various functionalities, including finding the minimum and maximum of user-input integers, sorting an array, printing patterns with asterisks, and converting decimal numbers to binary, octal, and hexadecimal formats. Each section of the document showcases a different programming task, illustrating basic control structures and array manipulations in Java. The code snippets are organized sequentially, with some incomplete or commented-out sections.

Uploaded by

eyosib54
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

javaexercise

The document contains multiple Java programs demonstrating various functionalities, including finding the minimum and maximum of user-input integers, sorting an array, printing patterns with asterisks, and converting decimal numbers to binary, octal, and hexadecimal formats. Each section of the document showcases a different programming task, illustrating basic control structures and array manipulations in Java. The code snippets are organized sequentially, with some incomplete or commented-out sections.

Uploaded by

eyosib54
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

//1;

import java.util.Scanner;

public class MinMaxFinder {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Ask the user how many numbers they will enter


System.out.print("Enter the number of integers: ");
int n = scanner.nextInt();

// Check if the number is valid


if (n <= 0) {
System.out.println("Please enter a positive number.");
return;
}

int[] numbers = new int[n];

// Get the numbers from the user


System.out.println("Enter " + n + " numbers:");
for (int i = 0; i < n; i++) {
numbers[i] = scanner.nextInt();
}

// Assume the first number is the smallest and largest


int min = numbers[0];
int max = numbers[0];

// Compare each number to find the smallest and largest


for (int i = 1; i < n; i++) {
if (numbers[i] < min) {
min = numbers[i];
}
if (numbers[i] > max) {
max = numbers[i];
}
}

// Print the results


System.out.println("Smallest number: " + min);
System.out.println("Largest number: " + max);

scanner.close();
}
}
//2
public class SimpleSort {
public static void main(String[] args) {
// Given unsorted array
int[] numbers = {60, 40, 20, 30, 50, 10};

// Display original array


System.out.print("Original array: ");
for (int num : numbers) {
System.out.print(num + " ");
}
System.out.println();
// Sorting the array in a simple way
for (int i = 0; i < numbers.length; i++) {
for (int j = i + 1; j < numbers.length; j++) {
// Swap if the current number is greater than the next one
if (numbers[i] > numbers[j]) {
int temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
}
}

// Display sorted array


System.out.print("Sorted array: ");
for (int num : numbers) {
System.out.print(num + " ");
}
}
}
//3

//4
int i,j;
for( i =10; i>0; i--){
for(j=0; j<10; j++){
if(j<=i){
System.out.print(" ");}
else{

System.out.print(" * "); }}
System.out.print("\n"); }
for( i =0; i <10; i++){
for(j=0; j<10; j++){
if(j>=i){
System.out.print(" * ");}
else{
System.out.print(" "); }}
System.out.print("\n"); }
for( i =0; i <10; i++){
for(j=0; j<=i; j++){
System.out.print(" * "); }
System.out.print("\n"); }
for( i =0; i <=10; i++){
for(j=10; j>=i; j--){
System.out.print(" * "); }
System.out.print("\n"); } } }
//5
int[]ar;
// int arr[]= new int[10];
// ar= new int[10];
// Scanner input= new Scanner( System.in);
// for(int i=0;i<=9;i++)
// {
// System.out.println("enter array ["+i+"]");
// ar[i]=input.nextInt();
// }
double[]myList={1.9,2.9,3.4,3.5};

int[]List={10,20,21,12,34,5,64,34,23};
// Print all the array elements
for(int k=0; k<=8;k++)
{ System.out.print(List[k]+","); }
for(double element:myList){
System.out.println(element); }
for(int j:List){ System.out.println(j);
}

//6
double[]myList={1.9,9,3.4,3.5};
// Print all the array elements
for(int i =0; i <myList.length; i++){
System.out.println(myList[i]+" ");}
// Summing all elements
double total =0;
for(int i =0; i <myList.length; i++){
total+=myList[i];}
System.out.println("Total is "+ total);
// Finding the largest element
double max =myList[0];
for(int i =1; i <myList.length; i++){
if(myList[i]> max) max =myList[i];}
System.out.println("Max is "+ max +" array size="+ myList.length);
//7
char grade ='A';
switch(grade){
case 'A':
System.out.println("Excellent!");
break;

case 'B':
case 'C':
System.out.println("Well done");
break;
case 'D':
System.out.println("You passed");
case'F':
System.out.println("Better try again");
break;
default: System.out.println("Invalid grade");}
System.out.println("Your grade is "+ grade);
//8

int dec,ib,ic,ih;
int[] bi=new int[15];
int[] oc=new int[10];
char[] hex=new char[10];
void calbin() {
System.out.println("decimal\t"+"bin eqvt.\t"+"octal eqvt\t"+"hexa eqvt");
for(int i=1;i<256;i++) {
dec=i; ib=0; ic=0; ih=0; int dev,com;
while(dec>0) {
bi[++ib]=dec%2;
dev=dec/2;
dec=dev; }
System.out.print(i+"\t");
for(int j=ib;j>0;j--) {
System.out.print(bi[j]); }
dec=i; while(dec>0) {
oc[++ic]=dec%8;
dev=dec/8;
dec=dev; }
System.out.print("\t"+"\t");
for(int j=ic;j>0;j--) {
System.out.print(oc[j]); }
dec=i;
while(dec>0) {

com=dec%16;
if(com==0)
hex[++ih]='0';
else if(com==1)
hex[++ih]='1';
else if(com==2)
hex[++ih]='2';
else if(com==3)
hex[++ih]='3';
else if(com==4)
hex[++ih]='4';
else if(com==5)
hex[++ih]='5';
else if(com==6)
hex[++ih]='6';
else if(com==7)
hex[++ih]='7';
else if(com==8)
hex[++ih]='8';
else if(com==9)
hex[++ih]='9';
else if(com==10)
hex[++ih]='A';

else if(com==11)
hex[++ih]='B';
else if(com==12)
hex[++ih]='C';
else if(com==13)
hex[++ih]='D';
else if(com==14)
hex[++ih]='E';
else
hex[++ih]='F';
dev=dec/16;
dec=dev; }
System.out.print("\t"+"\t");
for(int j=ih;j>0;j--) {
System.out.print(hex[j]); }
System.out.println(); } } }
public class DecimalToPrime {
public static void main(String[] args) {
// TODO code application logic here
decimal dd=new decimal();
dd.calbin(); }}

You might also like