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

Javacoding

The document contains examples of Java code demonstrating various programming concepts including: 1) Printing patterns using loops, performing arithmetic operations, accepting user input, and conditional statements like if-else. 2) Examples of switch statements, creating a basic calculator program, and different types of loops like for, while, and do-while. 3) A program to print even numbers between a range of numbers entered by the user. The document provides code snippets covering fundamental Java concepts such as variables, data types, operators, control flow, user input, and loops through multiple short examples.

Uploaded by

ayushyadav73095
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Javacoding

The document contains examples of Java code demonstrating various programming concepts including: 1) Printing patterns using loops, performing arithmetic operations, accepting user input, and conditional statements like if-else. 2) Examples of switch statements, creating a basic calculator program, and different types of loops like for, while, and do-while. 3) A program to print even numbers between a range of numbers entered by the user. The document provides code snippets covering fundamental Java concepts such as variables, data types, operators, control flow, user input, and loops through multiple short examples.

Uploaded by

ayushyadav73095
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

System.out.

print("*\n**\n***\n****\n*****\n");
---
*
**
***
****
*****

int a=22;
int b=10;
int diff=a+b;
System.out.println(diff);
---
32

priority of operators is from left to write


* / % these are more priority than < > + and evry thing
import java.util.*;
Scanner bc = new Scanner(System.in);
String name= bc.nextLine();
System.out.println(name);

// input from user


Scanner sc = new Scanner(System.in);
int a= sc.nextInt();
int b= sc.nextInt();

int sum= a*b;


System.out.println(sum);

//conditional statement

import java.util.*;
class firstclass {
public static void main(String args[]) {

Scanner sc =new Scanner(System.in);


int a = sc.nextInt();
int b = sc.nextInt();

if (a==b) {
System.out.println("both are equal");
}
else {
if (a>b) {
System.out.println("a is greater");
}
else {
System.out.println("b is greter");
}
}
}
}

//using else if

if (a==b) {
System.out.println("both are equal");
}
else if (a>b) {
System.out.println("a is greter");
}
else {
System.out.println("b is greater");
}

// example of conditional statement

import java.util.Scanner;

class firstclass {
public static void main(String args[]) {

Scanner sc=new Scanner(System.in);


int button=sc.nextInt();

if (button==1) {
System.out.println("HELLO");
}
else if (button==2) {
System.out.println("NAMASTE");
}
else if (button==3) {
System.out.println("BOUNJOUR");
}
else {
System.out.println("INVALID BUTTON");
}

}
}

// switch use

import java.util.Scanner;

class firstclass {
public static void main(String args[]) {

Scanner sc=new Scanner(System.in);


int button=sc.nextInt();

switch (button) {
case 1 : {
System.out.println("HELLO");
}break;
case 2 : {
System.out.println("NAMASTE");
} break;
case 3 : {
System.out.println("BONJOUR");
} break;
default : {
System.out.println("invalid button");
}
}
}
}

// calculator

import java.util.*;
class calculatorclass {
public static void main(String args[]) {

Scanner sc= new Scanner(System.in);


System.out.println("enter the first number ");
int a =sc.nextInt();
System.out.println("enter the second number ");
int b= sc.nextInt();
System.out.println("choose an operation");
System.out.print("1: Sum\n2: Difference\n3: Multiplication\n4: Division\n5:
Remender");
int operation=sc.nextInt();
int sum= a+b;
int diff=a-b;
int mul=a*b;
int div=a/b;
int rem=a%b;

switch (operation) {
case 1 : {
System.out.println(sum);
} break;
case 2 : {
System.out.println(diff);
} break;
case 3 : {
System.out.println(mul);
} break;
case 4 : {
System.out.println(div);
} break;
case 5 : {
System.out.println(rem);
}
default : {
System.out.println("invalid input operation : READ THE COMMANDS
PROPERLY");
}

}
}

//loop

import java.util.*;
class calculatorclass {
public static void main(String args[]) {

for(int j=1; j<=11; j++) {


System.out.println(j);
}

int i=1;
while(i<=11) {
System.out.println(i);
i++;
}
}

// do while

int i=1;
do {
System.out.println(i);
i++;
} while (i<=10);

// even number between n numbers

import java.util.*;
class calacutta {
public static void main (String args[]) {

Scanner sc = new Scanner(System.in);


int n = sc.nextInt();

for (int i=1; i<=n; i++) {


if (i%2==0){
System.out.println("even numbers are" + i);

}
}

You might also like