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

SIMPLE PROGRAMS

The document contains a series of Java programming examples demonstrating fundamental concepts such as variables, operators, control statements (if, switch), loops (while, for, do-while), and functions. Each example illustrates a specific programming task, such as checking for leap years, swapping values, and finding prime numbers. The code snippets are designed to help beginners understand basic Java syntax and logic.

Uploaded by

nathiya v c
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)
3 views

SIMPLE PROGRAMS

The document contains a series of Java programming examples demonstrating fundamental concepts such as variables, operators, control statements (if, switch), loops (while, for, do-while), and functions. Each example illustrates a specific programming task, such as checking for leap years, swapping values, and finding prime numbers. The code snippets are designed to help beginners understand basic Java syntax and logic.

Uploaded by

nathiya v c
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/ 8

//1.

SIMPLE Java Program


class Simple{
public static void main(String args[]){
System.out.println("Hello Java");
}
}

// 2. variables
public class Simple{
public static void main(String[] args){
int a=10;
float f=23.7f;
System.out.println(a);
System.out.println(f);
}}

// 3. unary operators
public class OperatorExample{
public static void main(String args[]){
int x=10;
System.out.println(x++);//10 (11)
System.out.println(++x);//12
System.out.println(x--);//12 (11)
System.out.println(--x);//10
}}

// 4. arithmatic operators

public class OperatorExample{


public static void main(String args[]){
int a=10;
int b=5;
System.out.println(a+b);//15
System.out.println(a-b);//5
System.out.println(a*b);//50
System.out.println(a/b);//2
System.out.println(a%b);//0
}}

// 5. if statement
public class Student {
public static void main(String[] args) {
int x = 10;
int y = 12;
if(x+y > 20) {
System.out.println("x + y is greater than 20");
}
}
}

// 6. if else
public class Student {
public static void main(String[] args) {
int x = 10;
int y = 12;
if(x+y < 10) {
System.out.println("x + y is less than 10");
} else {
System.out.println("x + y is greater than 20");
}
}
}

// 7. if else ladder
public class Student {
public static void main(String[] args) {
String city = "Delhi";
if(city == "Meerut") {
System.out.println("city is meerut");
}else if (city == "Noida") {
System.out.println("city is noida");
}else if(city == "Agra") {
System.out.println("city is agra");
}else {
System.out.println(city);
}
}
}

//8. switch
public class Student {
public static void main(String[] args) {
int num = 2;
switch (num){
case 0:
System.out.println("number is 0");
break;
case 1:
System.out.println("number is 1");
break;
default:
System.out.println(num);
}
}
}

// 9. while loop
public class Calculation {
public static void main(String[] args) {
int i = 0;
System.out.println("Printing the list of first 10 even numbers \n");
while(i<=10) {
System.out.println(i);
i = i + 2;
}
}
}

// 10. for loop


public class Calculattion {
public static void main(String[] args) {
// TODO Auto-generated method stub
int sum = 0;
for(int j = 1; j<=10; j++) {
sum = sum + j;
}
System.out.println("The sum of first 10 natural numbers is " + sum);
}
}

// 11. do while loop


public class Calculation {
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 0;
System.out.println("Printing the list of first 10 even numbers \n");
do {
System.out.println(i);
i = i + 2;
}while(i<=10);
}
}

// 12. to check for leap year


public class LeapYearExample {
public static void main(String[] args) {
int year=2020;
if(((year % 4 ==0) && (year % 100 !=0)) || (year % 400==0)){
System.out.println("LEAP YEAR");
}
else{
System.out.println("COMMON YEAR");
}
}
}

// 13. Program to check POSITIVE, NEGATIVE or ZERO:

public class PositiveNegativeExample {


public static void main(String[] args) {
int number=-13;
if(number>0){
System.out.println("POSITIVE");
}else if(number<0){
System.out.println("NEGATIVE");
}else{
System.out.println("ZERO");
}
}
}

// 14. Java Program to print Multiplication of two floating point Number.


import java.io.*;

class Multiply {
public static void main(String[] args)
{
// f is to ensures that numbers are float DATA TYPE
float f1 = 1.5f;
float f2 = 2.0f;

// to store the multiplied value


float p = f1 * f2;

// to print the product


System.out.println("The product is: " + p);
}
}

//15. Java Program to Swap Two values using third variable

// Importing generic libraries


import java.util.*;

class Swap {
public static void main(String[] args)
{
// Random integer values
int m = 9, n = 5;

// Swapping the values


int temp = m;
m = n;
n = temp;
System.out.println("Value of m is " + m
+ " and Value of n is " + n);
}
}

//16. Java Program to Check if Given Integer is Odd or Even

// Importing required classes


import java.io.*;
import java.util.Scanner;

// Main class
class OddEven {

// Main Driver Method


public static void main(String[] args)
{
// Declaring and initializing integer variable
int num = 10;
// Checking if number is even or odd number
// via remainder
if (num % 2 == 0) {

// If remainder is zero then this number is even


System.out.println("Entered Number is Even");
}
else {

// If remainder is not zero then this number is


// odd
System.out.println("Entered Number is Odd");
}
}
}

// 17. Java Program to Find the Biggest of 3 Numbers


// Importing generic Classes/Files
import java.io.*;
class Big {
// Function to find the biggest of three numbers
static int biggestOfThree(int x, int y, int z)
{
// Comparing all 3 numbers
if (x >= y && x >= z)

// Returning 1st number if largest


return x;

// Comparing 2nd no with 1st and 3rd no


else if (y >= x && y >= z)

// Return z if the above conditions are false


return y;

else

// Returning 3rd no, Its sure it is greatest


return z;
}

// Main driver function


public static void main(String[] args)
{
int a, b, c, largest;
a = 5;
b = 10;
c = 3;
// Calling the function in main() body
largest = biggestOfThree(a, b, c);
// Printing the largest number
System.out.println(largest + " is the largest number.");
}
}

// 18. Java program to find all the prime numbers from 1 to N


class gfg {

// Function to print all the


// prime numbers till N
static void prime_N(int N)
{
// Declaring the variables
int x, y, flg;
// Printing display message
System.out.println("All the Prime numbers within 1 and " + N + "
are:");
// Using for loop for traversing all the numbers from 1 to N
for (x = 2; x <= N; x++) {

// Using flag variable to check if x is prime or not


flg = 1;
for (y = 2; y * y <= x; y++) {
if (x % y == 0) {
flg = 0;
break;
}
}

// If flag is 1 then x is prime but if flag is 0 then x is not


prime
if (flg == 1)
System.out.print(x + " ");
}
}
// The Driver code
public static void main(String[] args)
{
int N = 45;
prime_N(N);
}
}

You might also like