Java Programs for Beginners

Java Programs for Beginners.

 

Hello friends, in this article I am discussing the some java code which are useful for you or any person and programmer.

The code will be very simple and run in very less time interval, here all codes is running on the all platform like – One Compiler, IDE for JAVA and some other platforms are available on the internet.

Java Programs for Beginners

 


x

  Write a Program to find user eligible for voting.

class A

{

public static void main (String args[])

{

int a=27;

if(a>=18)

{

System.out.println(“You are Eligible for Voting”);

}

else

{

System.out.println(“You Are  not Eligible for voating because your age is ”+a);

}

}

}

Here Output is :-

 

 You are Eligible for Voting.

 

Here staring the class A which is main class which contains the main function, then open the curly brackets then closed at last so again we are use main function and open bracket and close the curly brackets and then we are use System.out.println() for printing ,if you are eligible for voting or not.

 

Write a program to find the Greater Number.

 

 class A

{

public static void main (String args[])

{

int a=25;

int b=30;

if(a<b)

{

System.out.println(“a is less than b”);

}

else

{

System.out.println(“a is greater than b”);

}

}

}

 

Here Output is :-

 

 a is less than  b

 

Here staring the class A which is main class which contains the main function, then open the curly brackets then closed at last.

So again we are use main function and open bracket and close the curly brackets.

And here I have used the if else condition for check the condtion true or false.

And then we are use System.out.println() for, if a is less than b or not.

 

 

Write a Program to find leap year.

 

class A

{

public static void main (String args[])

{

int  a= 2022;

if(a%4)

{

System.out.println(“This Year is Leap Year”);

}

else

{

System.out.println(“This Year is not Leap Year”);

}

}

}

 

Here Output is :-

 

 This Year is not  Leap Year

 

Here staring the class A which is main class which contains the main function, then open the curly brackets then closed at last so again we are use main function and open bracket and close the curly brackets and here I have used the if else condition for check the condition true or false and then we are use System.out.println() for printing, if this year is leap year or not.

 

Write a Program to Swap Two Number’s.

 

class Swap

{

public static void main (String args[])

{

int  a= 20;

int  b= 30;

int  temp;

temp = a;

a = b;

b = temp;

System.println(“a = ” +a);

System.println(“b = ” +b);

}

}

}

 

Here Output is :-

 

  a  =  30

  b  = 20

 

Here staring the class Swap which is main class which contains the main function, then open the curly brackets then closed at last so again we are use main function and open bracket and close the curly brackets.

And here I have used the three variables, the first variable ‘a’ is contains 20 & ‘b’ contains 30 and we are take the temporary variable ‘temp’ this helping the swap both variable values ‘a’ and ‘b’.

Previous Post Next Post

Contact Form