Showing posts with label Java Program. Show all posts
Showing posts with label Java Program. Show all posts

In this program you`ll learn to display how to print series pattern program like 1.5 + 2.5 + 3.5 + ...+n


Source Code:
  1. public class SeriesProgramPart3 {
  2. public static void main(String[] args) {

  3. try (Scanner var = new Scanner(System.in)) {
  4. double i,n,sum = 0;
  5. System.out.println("Enter the number:");
  6. n = var.nextDouble();
  7. for(i=1.5;i<=n;i++) {
  8. sum = sum +i;
  9. System.out.print(i+" + ");
  10. }
  11. System.out.println("\nThe Summation if series is-"+sum);
  12. }
  13. }

  14. }
Output:

Enter the number:
5
1.5 + 2.5 + 3.5 + 4.5 + 
The Summation if series is-12.0

Enter the number:
10
1.5 + 2.5 + 3.5 + 4.5 + 5.5 + 6.5 + 7.5 + 8.5 + 9.5 + 
The Summation if series is-49.5



How to find sum of a series upto n numbers ?
This is java program to find sum of series like 1+2+3+4+..+n up to n numbers ...

Source Code:   

  1. public class SeriesProgramPart1 {

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

  3. try (Scanner var = new Scanner(System.in)) {
  4. int n,i,sum = 0;
  5. System.out.println("Enter the number:");
  6. n = var.nextInt();
  7. for(i=1;i<=n;i++) {
  8. if(i==n) {
  9. System.out.print(i);
  10. }else {
  11. System.out.print(i+ "+");
  12. }
  13. sum = sum +i;
  14. }
  15. System.out.println();
  16. System.out.println("The Summation if series is: "+sum);
  17. }
  18. }

  19. }

Outputs:

        Enter the number:
         5
        1+2+3+4+5
       The Summation if series is: 15
Reverse Number in java language ..just simple reverse program in java.
Source Code:
  1. public class ReverseNumber {
  2. public static void main(String[] args) {
  3. try (// TODO Auto-generated method stub
  4. Scanner input = new Scanner(System.in)) {
  5. int num,temp,sum = 0,r;
  6. System.out.println("Enter the number:");
  7. num = input.nextInt();
  8. temp=num;
  9. while(temp!=0) {
  10. r = temp%10;
  11. sum = sum*10 + r;
  12. temp = temp/10;
  13. }
  14. System.out.println("The Reverse number is ="+sum);
  15. }
  16. }
  17. }

Output:

Enter the number:
125
The Reverse number is =521

Enter the number:
12345
The Reverse number is =54321

This is a java program to find prime number. it find a number is prime or not .
prime  number is a number which is divisible by one or that number not others.

Source Code:

  1. public class PrimeNumber {
  2. public static void main(String[] args) {
  3. try (Scanner input = new Scanner(System.in)) {
  4. int i,num,count = 0;
  5. System.out.println("Enter the positive number:");
  6. num = input.nextInt();
  7. for(i=2;i<num;i++) {
  8. if(num%i==0) {
  9. count++;
  10. break;
  11. }
  12. }
  13. if(count==0)
  14. System.out.println("This is prime number.");
  15. else
  16. System.out.println("This is not prime number.");
  17. }
  18. }
  19. }

Output:

Enter the positive number:
5
This is prime number.

Enter the positive number:
153
This is not prime number.
This is prime number program in java language. 
rime number is a number which is not divided by other number without 1and that number.
Here , we decided that we consider the first number and last number then in this number range prnt the prime number.  
Source Code:
  1. public class PrimeNo1 {
  2. public static void main(String[] args) {
  3. try (Scanner input = new Scanner(System.in)) {
  4. int i,j,count = 0,totalprime = 0;
  5. System.out.println("Enter the first number:");
  6. int num1 =input.nextInt();
  7. System.out.println("Enter the end number:");
  8. int num2 = input.nextInt();
  9. System.out.println("prime numbers are :");
  10. for(i=num1;i<=num2;i++) {
  11.   
  12. for(j=2;j<i;j++) {
  13.     if(i%j==0) {
  14.     count++;
  15.      break;
  16. }
  17. }
  18. if(count==0) {
  19. System.out.print(i+" ");
  20. totalprime++;
  21. }
  22.     
  23. count = 0;
  24.            }
  25. System.out.println();
  26. System.out.println("Total Number of Prime Numbers are = "+totalprime);
  27. }
  28.   }
  29. }

Output:

Enter the first number:
5
Enter the end number:
50
prime numbers are :
5 7 11 13 17 19 23 29 31 37 41 43 47 
Total Number of Prime Numbers are = 13


This is a palindrome number in Java program.
1. Do you know  what is palindrome number?
ans.   Palindrome number is a number which is same after reverse.
as a example : 121 reverse it the result will be 121.
Palindrome number

Go to the series : By clicking java
Palindrome in C : Click here

Source Code:
  1. public class PalindromeNumber {
  2. public static void main(String[] args) {
  3. try (
  4. Scanner input = new Scanner(System.in)) {
  5. int num,temp,sum = 0,r;
  6. System.out.println("Enter the number:");
  7. num = input.nextInt();
  8. temp=num;
  9. while(temp!=0) {
  10. r = temp%10;
  11. sum = sum*10+ r;
  12. temp = temp/10;
  13. }
  14. if(num ==sum) {
  15. System.out.println("This is Palindrome number ");
  16. }
  17. else {
  18. System.out.println("This is not palindrome number.");
  19. }
  20. }
  21. }
  22. }

Output:


Enter the number:
153
This is not palindrome number.

Enter the number:
121
This is Palindrome number 

This is basic calculation of maximum and minimum number.  there are two number which we calculate which is max or which is min, as  a example we consider 24 and 25 then the max will be 25 and min will be 24.

Go to the series : https://browncodeit.blogspot.com/p/java-programs.html  

Source Code:  
  1. public class MaxMInimaDemo {
  2. public static void main(String[] args) {
  3. try (Scanner input = new Scanner(System.in)) {
  4. int num1,num2;
  5. System.out.print("Enter the number: ");
  6. num1 = input.nextInt();
  7. System.out.print("Enter the number: ");
  8. num2 = input.nextInt();
  9. if(num1 > num2) {
  10. System.out.print("maximum number is "+num1);
  11. }
  12. if(num1<num2){
  13. System.out.print("maximum number is "+num1);
  14. }
  15. }
  16. }
  17. }



Output:  

Enter the number: 15
Enter the number: 19
minimum number is 15

Enter the number: 59
Enter the number: 45
maximum number is 59

This is basic syntax of math class. Various type of example using Math class. 

Source Code:
  1. public class MathDemo {
  2. public static void main(String[] args) {
  3. // TODO Auto-generated method stub
  4. int a = 10,max;
  5. int b = 20;
  6. int c = -15;
  7. double x = 2,y = 3;
  8. float z = 8.49f;
  9. max = Math.max(a, b);
  10. System.out.printf("max is-%d",max); //Showing maximum value.
  11. System.out.println("\nmin is -"+Math.min(a, b)); //Showing minimum value.
  12. System.out.printf("Absolute value is-%d ",Math.abs(c));    //Showing absolute value.
  13. System.out.println("\n X to the power of Y-: "+Math.pow(x, y));
  14. System.out.println("round no. id-"+Math.round(z));
  15. System.out.println("pi = "+Math.PI);
  16. }
  17. }

Output:

max is-20
min is -10
Absolute value is-15 
 X to the power of Y-: 8.0
round no. id-8
pi = 3.141592653589793
This is a java program to check a year is leap year or not.
lea year come after 4 years next. like 2020 is a leap year after 2024 will be leap year.

Go to the java series.: https://browncodeit.blogspot.com/p/java-programs.html

Source Code:

  1. public class LeapYearDemo {
  2. public static void main(String[] args) {
  3. try (
  4. Scanner input = new Scanner(System.in)) {
  5. int yr;
  6.  
  7. System.out.print("Enter the year: ");
  8. yr = input.nextInt();
  9.  
  10. if(yr%400==0 ||(yr%100 != 0 && yr%4==0)) {
  11.  
  12. System.out.println("This is Leapyear.");
  13. }
  14. else {
  15. System.out.println("this is not leapyear.");
  16. }
  17. }
  18. }
  19. }


Output:

Enter the year: 2020
This is Leapyear.

Enter the year: 1589
this is not leapyear.

This is a example of For Loop.  
do practice..

go to the series link.: click here.

Source Code:


  1. public class ForLoopDemo {
  2. public static void main(String[] args) {
  3.  
  4. int i;
  5. for(i=1;i<=10;i++) {
  6. System.out.println(i+" India.");
  7. }
  8. }
  9. }

Output:

1 India.
2 India.
3 India.
4 India.
5 India.
6 India.
7 India.
8 India.
9 India.
10 India.

In this program ,you will learn how to print Fibonacci series in java language. The Fibonacci series is a series where next digit is sum of previous digits.


Source Code:

  1. public class FibonocciNumber {
  2. public static void main(String[] args) {
  3. try (Scanner input = new Scanner (System.in)) {
  4. int n,i,first = 0,second = 1,fibo;
  5. System.out.println("How many number: ");
  6. n = input.nextInt();
  7. System.out.println(first+"\n"+second);
  8. for(i=3;i<=n;i++) {
  9. fibo = first + second;
  10. first = second;
  11. second = fibo;
  12. System.out.println(fibo);
  13. }
  14. }
  15.   }
  16. }

output:

How many number: 3
0
1
1

How many number: 12
0
1
1
2
3
5
8
13
21
34
55
89



To find a number factorial in java , Follow the process.

Go the series : https://browncodeit.blogspot.com/p/java-programs.html

Source Code:

  1. public class FactorialDemo {
  2. public static void main(String[] args) {
  3. try (Scanner input = new Scanner(System.in)) {
  4. int n,i,fact = 1; //variable declaration.
  5. System.out.println("Enter the number: ");
  6. n = input.nextInt(); //take input in n variable.
  7. for(i=n;i>=1;i--) {
  8. fact = fact*i;
  9. }
  10. System.out.println("factorial is :"+fact);
  11. }
  12. }
  13. }

Output:

Enter the number: 5
factorial is :120

Enter the number: 8
factorial is :40320
Java program to check a number is odd or even. This is a normal program to calculate the even odd number.
Do practice.
go to the java link: click here

Source Code:

  1. public class EvenOddDemo {
  2. public static void main(String[] args) {
  3. try (// TODO Auto-generated method stub
  4. Scanner input = new Scanner(System.in)) {
  5. int num;
  6.  
  7. System.out.print("Enter the number: ");
  8. num = input.nextInt();
  9. if(num %2==0) {
  10. System.out.println("The number is Even.");
  11. }
  12. else {
  13. System.out.println("The number is odd.");
  14. }
  15. }
  16. }
  17. }

Output:


Enter the number: 84553
The number is odd.

Enter the number: 1548856
The number is Even.
This a practice to Do While loop in java Language.

Practice more gain more knowledge.
Watch serial wise to practice -Go to the java page : Click here

Source code:

  1. public class DoWhileloopDemo {
  2. public static void main(String[] args) {
  3. int i=1;
  4. do {
  5. System.out.println(i+" India");
  6. i++;
  7. }while(i<=10);
  8. }
  9. }

Output:

1 India
2 India
3 India
4 India
5 India
6 India
7 India
8 India
9 India
10 India

This is a java program to print the correct spelling of 1-10 digits.
like 1=one ,2=two etc.

Source Code:

  1. public class DigitSpelingDemo {
  2. public static void main(String[] args) {
  3. try (Scanner input = new Scanner(System.in)) {
  4. int digit;
  5. System.out.print("Enter any digit: ");
  6. digit = input.nextInt();
  7. switch(digit) {
  8. case 0:
  9. System.out.println("This is zero");
  10. break;
  11. case 1:
  12. System.out.println("This is One.");
  13. break;
  14. case 2:
  15. System.out.println("This is Two.");
  16. break;
  17. case 3:
  18. System.out.println("This is Three.");
  19. break;
  20. case 4:
  21. System.out.println("This is Four.");
  22. break;
  23. case 5:
  24. System.out.println("This is Five.");
  25. break;
  26. case 6:
  27. System.out.println("This is six.");
  28. break;
  29. case 7:
  30. System.out.println("This is Seven.");
  31. break;
  32. case 8:
  33. System.out.println("This is Eight.");
  34. break;
  35. case 9:
  36. System.out.println("This is Nine.");
  37. break;
  38. default:
  39.     System.out.println("it`s not a digit.");
  40. }
  41. }
  42. }
  43. }

Output:

Enter any digit: 5 This is Five.

Enter any digit: 8 This is Eight.
There are two types of data type in java .
  1. Primitive data type .
  2. Non-Primitive data type.
Primitive data types are- 
  1. int data type
  2. boolean data type
  3. float data type
  4. char data type
  5. double data type
  6. long data type
  7. short data type
Non-primitive data types are -
  1.  String 
  2. Array 
  3. etc
Source Code:
  1. public class DataType {
  2. public static void main(String[] args) {
  3. boolean b;
  4. int i = 25;
  5. double d = 203.5;
  6. byte bt = 127;
  7. float f= 12.2f;
  8. b = true;
  9. System.out.println("Boolean ="+b);
  10. System.out.println("integer="+i);
  11. System.out.println("double="+d);
  12. System.out.println("byte="+bt);
  13. System.out.println("float="+f);
  14. }
  15. }
Output:

Boolean =true
integer=25
double=203.5
byte=127
float=12.2

It is depends on the boolean expression is true or false. the main motive of this expression is which value should be assign .

 expression :    variable  x=(expression)  ?  true value  :  false value

Source Code:
  1. public class ConditionalDemo {
  2. private static Scanner input;
  3. public static void main(String[] args) {
  4. input = new Scanner(System.in);
  5. int num1,num2,large;
  6. System.out.print("Enter two number: ");
  7. num1 = input.nextInt();
  8. num2 = input.nextInt();
  9. large = (num1>num2)?num1:num2;
  10. System.out.println("large no is ="+large);
  11. }
  12. }
Output:
        
          Enter two number: 25 24
          large no is =25

Enter two number: 50 55
large no is =55

This is an example of assignment operator. 



** If you copy it ,ignore package.** Remind this is a practice program .

Source Code:

  1. package basicjava;
  2. //import java.util.Scanner;
  3. public class AssinmentDemo {
  4. public static void main(String[] args) {
  5. // Scanner input = new Scanner(System.in);
  6. int x = 3,y = 2;
  7. /* System.out.printf("Enter the number of x:");
  8. x = input.nextInt();
  9. System.out.printf("Enter the number of y:");
  10. y = input.nextInt();*/
  11. x += y;   //x = x + y = 5
  12. System.out.println("1.ans is ="+x);
  13. x -= y;    //x = x - y = 3
  14. System.out.println("2.ans is ="+x);
  15. x *= y;    //x = x * y = 6
  16. System.out.println("3.ans is ="+x);
  17. x /= y;   //x = x / y = 3
  18. System.out.println("4.ans is ="+x);
  19. x %= y;   //x = x % y = 1
  20. System.out.println("4.ans is ="+x);
  21. }
  22. }

Output:

1.ans is =5
2.ans is =3
3.ans is =6
4.ans is =3
4.ans is =1

This is an Armstrong number in java program.  Armstrong number is an number = sum of  cube of each digits . 
As a example  we consider -- 153= (1)3 +(5)3 + (3)3
Q: Is 153 an Armstrong number?
A: yes, 153 is an Armstrong number.

Q: Is 1 an Armstrong number?
A: Yes. 

Q: Give example  Armstrong number?
A: 1,153,370,371

Source Code:

  1. package basicjava;
  2. import java.util.Scanner;
  3. public class ArmstrongNumber {
  4. public static void main(String[] args) {
  5. try (
  6. Scanner input = new Scanner(System.in)) {
  7. int num,temp,sum = 0,r;
  8. System.out.println("Enter the number:");
  9. num = input.nextInt();
  10. temp=num;
  11. while(temp!=0) {
  12. r = temp%10;
  13. sum = sum + r*r*r;
  14. temp = temp/10;
  15. }
  16. if(num==sum) {
  17. System.out.print(true);
  18. }
  19. else {
  20. System.out.println("This is not Armstrong number.");
  21. }
  22. }
  23. }
  24. }

Output:

1. Enter the number: 153
     true

2.  Enter the number: 123
     This is not Armstrong number.
Let`s create "Hello World"  program in java --


Source Code:

  1. public class HelloWorld {
  2. public static void main(String[] args) {
  3.      System.out.print("hello world");
  4.      System.out.print(12345);
  5. }
  6. }

Output:


hello world
12345