Вы находитесь на странице: 1из 8

JAVA Programs

Converting Temperature in Fahrenheit into Temperature in Celsius.


import java.util.Scanner; class TempConverter { public static void main(String[] args) { int temp,far,cel; System.out.print("What is the temperature you'd like to convert? "); Scanner myScanner = new Scanner(System.in); temp = myScanner.nextInt(); far = temp * 9 / 5 + 32; //use this line if Celsius to Fahrenheit cel = (temp - 32) * 5 / 9; // use this line if Fahrenheit to Celsius System.out.println("Fahrenheit --> Celsius: " + cel); //use this line if Celsius to Fahrenheit System.out.println("Celsius --> Fahrenheit: " + far);// use this line if Fahrenheit to Celsius } }

Program for student Mark-List preparation

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; public class TestClass { private static BufferedReader stdin = new BufferedReader( new InputStreamReader( System.in ) ); static int input = 0; public static void main ( String [] args ) { int inputdata; try { inputdata = getinput(); if(inputdata<5 || inputdata>25){ System.out.println("Invalid Input");

getinput(); } //System.out.println(inputdata); System.out.println("***Data Entry Session***"); String name = getname(); ArrayList al =getlistofMarks(inputdata); System.out.println("=============== System.out.println("student result analysis"); System.out.println("prepared by: "+ name); System.out.println("=============== int sum =0; for (Iterator iter = al.iterator();iter.hasNext();){ sum +=Integer.parseInt(iter.next().toString( } //Collections.max(al); int average = sum/inputdata; int highest = Integer.parseInt(Collections.max(al).toS int lowest = Integer.parseInt(Collections.min(al).toS System.out.println("average: "+average); System.out.println("higest: "+highest); System.out.println("lowest: "+lowest); int count =0; for (Iterator iter = al.iterator();iter.hasNext();){ if(Integer.parseInt(iter.next().to count++; } } System.out.println(count+" students scored above average"); System.out.println("The list printed (5 marks per row)"); for (int i=0;i<al.size();i++){ if(i!=0 && i%5==0){ System.out.println(""); } System.out.print(al.get(i)+" "); } System.out.println(""); System.out.println("Thank you for trying this program."); System.out.println("try again (0 = to exit, Other number to continue)? : "); String opt = stdin.readLine(); int option = Integer.parseInt(opt); if(option==0){ System.out.println("Program Abort."); System.exit(0); }else{ main(args); } }catch(NumberFormatException num){ System.err.println("Invalid number. Restarting Program..."); main(args); } catch (IOException e) { System.err.println("IO Exception. Restarting Program...."); main(args); } } private static ArrayList getlistofMarks(int count) throws IOException { ArrayList al = new ArrayList(count); int j=1; for(int i=0;i<count;i++){ System.out.print("enter student mark #"+j+": "); String marks = stdin.readLine(); int mark = Integer.parseInt(marks); al.add(mark); j++; }

return al; } private static String getname() throws IOException { System.out.print("Enter your first name: "); String frstnamestr = stdin.readLine(); System.out.print("Enter your last name: "); String lastnamestr = stdin.readLine(); return frstnamestr+" "+lastnamestr; } private static int getinput() throws IOException { // Prompt the user System.out.print( "enter a number (range 5 - 25 only) :" ); // Read a line of text from the user. String inputstr = stdin.readLine(); input = Integer.parseInt(inputstr); if(input<5 || input>25){ System.out.println("Invalid Input"); getinput(); } return input; } }

Program for reverse and finding sum of individual digits of a given number
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. package in.net4java.examples; import java.util.Scanner; public class SumOfDigits { public static void main(String[] args) { int num = 0; int sum = 0,temp = 0; System.out.println("Enter a number :"); Scanner in = new Scanner(System.in); num = in.nextInt(); while( num > 0){ temp = num % 10; sumsum = sum + temp; numnum = num / 10; } System.out.println("Sum of individual digits :" + sum); } }

Program to generate Fibonacci series


class Fibonacci { public static void main(String args[]){ int num = 5; //taking no. as command line argument. System.out.println("*****Fibonacci Series*****"); int f1, f2=0, f3=1; for(int i=1;i<=num;i++){ System.out.print(" "+f3+" "); f1 = f2; f2 = f3; f3 = f1 + f2; } } }

Program for finding Factorial of a given number


/*Write a program to Find Factorial of Given no. */ class Factorial{ public static void main(String args[]){ int num = Integer.parseInt(args[0]); //take argument as command line int result = 1; while(num>0){ result = result * num; num--; } System.out.println("Factorial of Given no. is : "+result); } }

Program for find whether a given number is prime or not


/* Program to Find whether number is Prime or Not. */ class PrimeNo{ public static void main(String args[]){ int num = Integer.parseInt(args[0]); int flag=0; for(int i=2;i<num;i++){ if(num%i==0) { System.out.println(num+" is not a Prime Number"); flag = 1; break; } } if(flag==0) System.out.println(num+" is a Prime Number"); } }

Program for sorting the given numbers in Ascending and Descending order
package javaapplication5; importjava.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int[] hold = new int[10];

int temp; //getting input from user for(int a=0;a<10;a++) { System.out.print("Enter Number: "); hold[a] = input.nextInt(); } //Sorting an array for(int i=0;i<10;i++) { for(int b=i;b<10;b++) { if(hold[b+1]>hold[b]){ temp=hold[b]; hold[b]=hold[b+1]; hold[b+1]=temp; } } } System.out.print("Which Type of output you want:\n1-Decending\n2-Acending"); int choice=input.nextInt(); if(choice==1) for(int i=0;i<10;i++)

System.out.println(hold[i]); else for(int i=9;i>=0;i++) System.out.println(hold[i]); } }

Program for Matrix Multiplication


import java.util.*; class MatrixMultiplication{ public static void main(String[] args){ Scanner input = new Scanner(System.in); int[][] A = new int[3][3]; int[][] B = new int[3][3]; int[][] C = new int[3][3]; System.out.println("Enter elements for matrix A : "); for (int i=0 ; i < A.length ; i++) for (int j=0 ; j < A[i].length ; j++){ A[i][j] = input.nextInt(); } System.out.println("Enter elements for matrix B : "); for (int i=0 ; i < B.length ; i++) for (int j=0 ; j < B[i].length ; j++){ B[i][j] = input.nextInt(); } System.out.println("Matrix A: "); for (int i=0 ; i < A.length ; i++){ System.out.println(); for (int j=0 ; j < A[i].length ; j++){ System.out.print(A[i][j]+" "); } } System.out.println(); System.out.println(); System.out.println("Matrix B: "); for (int i=0 ; i < B.length ; i++){ System.out.println();

for (int j=0 ; j < B[i].length ; j++){ System.out.print(B[i][j]+" "); } } System.out.println(); System.out.println(); System.out.println("Result is: "); System.out.println(); for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ for(int k=0;k<3;k++){ C[i][j]+=A[i][k]*B[k][j]; } } } for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ System.out.print(+C[i][j]+" "); } System.out.println(); } } }