0% found this document useful (0 votes)
68 views

Java Program

The document contains 10 Java programs that demonstrate various programming concepts like input/output, conditional statements, loops, methods, classes, etc. The programs range from basic examples like printing numbers in a range to more complex examples like calculating factorials, Fibonacci series, checking if a number is prime, etc.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views

Java Program

The document contains 10 Java programs that demonstrate various programming concepts like input/output, conditional statements, loops, methods, classes, etc. The programs range from basic examples like printing numbers in a range to more complex examples like calculating factorials, Fibonacci series, checking if a number is prime, etc.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

1) //Example program to utilize readLine() method import java.io.

DataInputStream; public class iotesting { public void datainput() { DataInputStream in=new DataInputStream(System.in); int a=0; int b=0; try { System.out.println("Enter the value of a"); a=Integer.parseInt(in.readLine()); System.out.println("Enter the value of b"); b=Integer.parseInt(in.readLine()); System.out.println("The sum of numbers is "+(a+b)); System.out.println("The product of numbers is "+(a*b)); } catch(Exception e) {} } } 2) //Program to swap two numbers public class Swap { public void swap(int a,int b) { a=10; b=12; int sum=a+b; System.out.println("Sum is "+sum); } } 3) Program to input two numbers and find the greater between the two. import java.io.DataInputStream; public class ifelse { public void ifelse()

{ DataInputStream in=new DataInputStream(System.in); int x; int y; System.out.flush(); try { System.out.println("Enter the 1st Number"); x=Integer.parseInt(in.readLine()); System.out.println("Enter the 2nd Number"); y=Integer.parseInt(in.readLine()); if(x>y) System.out.println("x is greater"); else if(x==y) System.out.println("x and y has the same value"); else System.out.println("y is greater"); } catch(Exception e) {} } }

4) Program to print from 1 to 50 with the help of While Loop. public class WhileDemo { public void WhileDemo() { int count=1; while(count<=50) { System.out.print(" "+count); count++; } } }

5)

Write a program to enter a number and check whether it is positive or not. If positive, then whether it is even or odd. If it is even, then multiply 2 with that number and print it. If it is odd, then add 2.

import java.io.DataInputStream; public class NestedIf { public void NestedIf() { DataInputStream in=new DataInputStream(System.in); int num; try { System.out.println("Enter a Number"); num=Integer.parseInt(in.readLine()); if(num>=0) { System.out.println("Number is Positive"); if((num%2)==0) { System.out.println(+(num*2)); } else { System.out.println(+(num+2)); } }

else { System.out.println("Number is negative"); } } catch(Exception e) {} } }

6) Program to input the month number and display the month public class monthday { public void monthday(int mnt) { switch(mnt) { case 1: System.out.println("January"); break; case 2: System.out.println("February"); break; case 3: System.out.println("March"); break; case 4: System.out.println("April"); break; case 5: System.out.println("May"); break; case 6: System.out.println("June"); break; case 7: System.out.println("July"); break; case 8: System.out.println("August"); break; case 9: System.out.println("September"); break; case 10: System.out.println("October"); break; case 11: System.out.println("November"); break; case 12:

System.out.println("December"); break; default: System.out.println("Sorry Wrong Month Code"); } } } 7) Program to check a number whether prime or not*/ public class Prime { public void Prime(int a) { int i,c=0; for(i=1;i<=a;i++) { if(a%i==0) c++; } if(c==2) System.out.println(a+" is a prime number"); else System.out.println(a+" is not a prime number"); } 8) Program to enter a number and find its factorial import java.io.DataInputStream; public class Factorial { public void Factorial() { DataInputStream in=new DataInputStream(System.in); int num,x,fact=1; try { System.out.println("Enter a number to get its Factorial"); num=Integer.parseInt(in.readLine()); for(x=1;x<=num;x++) { fact=fact*x;

} System.out.println("The Factorial of "+num+" is "+fact); } catch(Exception e) {} } } 9) Program to generate Fibonacci Series upto 10 terms public class Fibonacci { public void Fibonacci() { int count=1,a=1,b=0,c=0; while(count<=10) { System.out.println(a); c=a+b; b=a; a=c; count++; } } } 10) Program to change if-else to switch-case as per Books Exercise No-15 Page-315 with little modification. public class switchcase { public void switchcase(int inputnum) { int calcval; switch(inputnum) { case 6:calcval=inputnum*25-20; System.out.println(inputnum+calcval); break; case 7:calcval=inputnum*25-20; System.out.println(inputnum+calcval);

break; case 8:calcval=inputnum*25-20; System.out.println(inputnum+calcval); break; case 9:calcval=inputnum*25-20; System.out.println(inputnum+calcval); break; case 10:calcval=inputnum*20-15; System.out.println(calcval-inputnum); break; default: System.out.println("invalid input"); break; } }

} Java Series 1. Program to print 1! 2! 3!.....N terms public class FactorialSeries { public void FactorialSeries(int n) { int fact=1; for(int i=1;i<=n;i++) { for(int j=1;j<=i;j++) { fact=fact*j; } System.out.print(" "+fact);

fact=1; } } 2. Program to get the sum of the following series S=1-2+3-4.N terms public class PlusMinusSeries { public void PlusMinusSeries(int N) { int S=0; for(int i=1;i<=N;i++) { if(i%2==0) S=S-i; else S=S+i; } System.out.print(S); } 3. /**Program to get sum of the following series s=x/1!+x/2!+x/3!+.....n terms*/ public class Series24d { public void Series24d(int n,int x) { double s=0,fact=1; for(int i=1;i<=n;i++) { fact=fact*i; s=s+x/fact; } System.out.println(s);

} } 4. /**Program to get sum of the following series s=1/1!+2/2!+3/3!+.....n terms*/ public class Series24b { public void Series24b(int n) { double s=0,fact=1; for(int i=1;i<=n;i++) { fact=fact*i;

You might also like