Tasks - Methods - Practice Problems #4
Replies: 53 comments
-
|
**Code for square of a number ** public class Square { }
} } public class EvenOdd { } |
Beta Was this translation helpful? Give feedback.
-
public class Square { }
public class Details { }
public class Swap { }
public class EvenOdd { } |
Beta Was this translation helpful? Give feedback.
-
Program to compute the square of a numberProgram for printing your name, city, and a hobby in formatted manner.Program to swap two numbers.Program to print whether a number is even or odd. |
Beta Was this translation helpful? Give feedback.
-
|
Program to compute the square of a number. } } } } |
Beta Was this translation helpful? Give feedback.
-
|
SQUARE OF A NUMBER**** } WAF to print your name, city, and a hobby in formatted manner** } SWAP TWO NUMBERS } NUMBER IS EVEN OR ODD************ } |
Beta Was this translation helpful? Give feedback.
-
|
WAF to compute the square of a number. import java.util.*; class Square1 { } WAF to print your name, city, and hobby in a formatted manner. import java.util.*; class Printing { } WAF to swap two numbers. import java.util.*; class swaping { } import java.util.*; class Evenodd { } |
Beta Was this translation helpful? Give feedback.
-
|
SQUARE OF A NUMBER WAF to print your name, city, and a hoobby in formatted manner. Swap Two Numbers Even or Odd |
Beta Was this translation helpful? Give feedback.
-
|
squares } } } } |
Beta Was this translation helpful? Give feedback.
-
|
public class Functions { } |
Beta Was this translation helpful? Give feedback.
-
|
Even or Odd Program } Square of a Number Program } Swapping of Two Numbers } Printing name, city, and hobby in a formatted manner } class PersonalDetails { } |
Beta Was this translation helpful? Give feedback.
-
PROGRAM 1 public class prog1{ PROGRAM 2 PROGRAM 3 } PROGRAM 4 } |
Beta Was this translation helpful? Give feedback.
-
|
code for square public class Square { } code for printing details } code for swap public class Swap { code for even Odd public class Numbers { |
Beta Was this translation helpful? Give feedback.
-
import java.util.*; } 2.PROGRAM TO PRINT DETAILS---> import java.util.*; }
import java.util.*; }
import java.util.*; } |
Beta Was this translation helpful? Give feedback.
-
|
SQUARE OF A NUMBER } DETAILS }` SWAPPING NUMBERS } EVEN ODD } |
Beta Was this translation helpful? Give feedback.
-
`import java.util.*; public class SquareOfNumber { }`
import java.util.; }
} `
` } ` |
Beta Was this translation helpful? Give feedback.
-
1import java.util.*;
public class Main
{
static int square(int squareOfNumber)
{
return (squareOfNumber*squareOfNumber);
}
public static void main(String[] args) {
Scanner obj=new Scanner(System.in);
System.out.println("Enter the square of the number:");
int squareOfNumber=obj.nextInt();
System.out.println("square of the number is :"+square(squareOfNumber));
}
}2import java.util.*;
public class Main
{
static void userDetail(String Name, String City, String Hobby)
{
System.out.println("name is :"+Name+"city is: "+City+"hobby is: "+Hobby);
}
public static void main(String[] args) {
Scanner obj=new Scanner(System.in);
System.out.println("Enter the name:");
String name=obj.next();
System.out.println("Enter the city:");
String city=obj.next();
System.out.println("Enter the hobby:");
String hobby=obj.next();
userDetail(name,city,hobby);
}
}3import java.util.*;
class Swap
{
public void swapping(int numberOne,int numberTwo)
{
int temp=numberOne;
numberOne=numberTwo;
numberTwo=temp;
System.out.println("swapping input numberOne is: "+numberOne+" numberTwo is: "+numberTwo);
}
}
public class Main
{
public static void main(String[] args) {
Scanner obj=new Scanner(System.in);
System.out.println("Enter the input numberOne:");
int numberOne=obj.nextInt();
System.out.println("Enter the input numberTwo:");
int numberTwo=obj.nextInt();
Swap swap=new Swap();
swap.swapping(numberOne,numberTwo);
}
}4import java.util.*;
class AddOrEven{
public void checkAddOrEven(int number)
{
if(number%2==0)
{
even();
}
else{
add();
}
}
public void add()
{
System.out.println("the number is add number");
}
public void even()
{
System.out.println("the number is even number");
}
}
public class Main
{
public static void main(String[] args) {
Scanner obj=new Scanner(System.in);
System.out.println("Enter the number:");
int number=obj.nextInt();
AddOrEven addOrEven=new AddOrEven();
addOrEven.checkAddOrEven(number);
}
} |
Beta Was this translation helpful? Give feedback.
-
1.Squarepackage Operators.Methods;
import java.util.Scanner;
public class Square {
static void findSquare(int n){
System.out.println(n*n);
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number");
int a=Integer.parseInt(sc.nextLine());
findSquare(a);
sc.close();
}
}2.Detailspackage Operators.Methods;
import java.util.Scanner;
public class Details {
static void myDetails(String name,String city,String hobby){
System.out.print("\nName: " + name);
System.out.print("\nCity: " + city);
System.out.print("\nHobby: " +hobby);
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the name");
String name=sc.nextLine();
System.out.println("Enter the City");
String city=sc.nextLine();
System.out.println("Enter the Hobby");
String hobby=sc.nextLine();
myDetails(name,city,hobby);
sc.close();
}
}3.Swappingpackage Operators.Methods;
import java.util.Scanner;
public class Swap {
void swap(int a,int b){
int temp;
temp=a;
a=b;
b=temp;
System.out.printf("Swapping of a,b is %d %d:",a,b);
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter number 1:");
int a=Integer.parseInt(sc.nextLine());
System.out.println("Enter number 2:");
int b=Integer.parseInt(sc.nextLine());
Swap obj=new Swap();
obj.swap(a,b);
sc.close();
}
}4.OddEvenpackage Operators.Methods;
import java.util.Scanner;
public class OddEven {
String oddEven(int a){
if(a%2==0)
return "Even";
else
return "Odd";
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number:");
int a=Integer.parseInt(sc.nextLine());
OddEven obj=new OddEven();
System.out.println("The number is a "+ obj.oddEven(a)+" number");
sc.close();
}
} |
Beta Was this translation helpful? Give feedback.
-
1public class Square {
static int sq(int n){
return n*n;
}
public static void main(String[] args) {
int n=10;
System.out.println("Square of "+n+" = "+sq(n));
}
}2class Print{
void display(String name, String city, String hobby){
System.out.println("Name\t: "+name);
System.out.println("City\t: "+city);
System.out.println("Hobby\t: "+hobby);
}
}
public class Details {
public static void main(String[] args) {
Print p=new Print();
p.display("karthick","Chennai","Listening to Music");
}
}3public class Swap2nos {
static void swap(int a,int b){
int temp=a;
a=b;
b=temp;
System.out.println("After Swapping");
System.out.println("a = "+a+" b= "+b);
}
public static void main(String[] args) {
int a=10,b=20;
System.out.println("Before Swapping");
System.out.println("a = "+a+" b= "+b);
swap(a,b);
}
}4public class OddOrEven {
static void check(int x){
if(x%2==0){
System.out.println(x+" is Even");
}
else{
System.out.println(x+" is Odd");
}
}
public static void main(String[] args) {
int n=11;
check(n);
}
} |
Beta Was this translation helpful? Give feedback.
-
1. WAF to compute the square of a number.import java.util.Scanner;
class NumSquare {
int calcSquare(int x) {
return (x * x);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
NumSquare ob = new NumSquare();
System.out.println("enter a number");
int n = Integer.parseInt(sc.nextLine());
System.out.println(String.format("The square of %d is %d", n, ob.calcSquare(n)));
sc.close();
}
}2. WAF to print your name, city, and hobby in a formatted manner.Sample Output: import java.util.Scanner;
public class DispDetail {
void display(String n, String c, String h) {
System.out.println();
System.out.println("Name: " + n);
System.out.println("City: " + c);
System.out.println("Hobby: " + h);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
DispDetail ob = new DispDetail();
System.out.println("Enter your name");
String name = sc.nextLine();
System.out.println("Enter city");
String city = sc.nextLine();
System.out.println("Enter hobby");
String hobby = sc.nextLine();
ob.display(name, city, hobby);
sc.close();
}
}3. WAF to swap two numbers.INPUT: a = 10, b = 13 import java.util.Scanner;
public class Swapping {
static void swap(int a, int b) {
int t;
t = a;
a = b;
b = t;
System.out.println("After swapping");
System.out.println("a = " + a + " b = " + b);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter two numbers");
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println("before swapping");
System.out.println("a = " + a + " b = " + b);
swap(a, b);
sc.close();
}
}4. WAF to print whether a number is even or odd.import java.util.Scanner;
public class OddOrEven {
static void check(int n) {
if (n % 2 == 0) {
System.out.println(String.format("%d is even number", n));
}
if (n % 2 != 0) {
System.out.println(String.format("%d is odd number", n));
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
int num = Integer.parseInt(sc.nextLine());
check(num);
sc.close();
}
} |
Beta Was this translation helpful? Give feedback.
-
1.Square of numberclass SquareCalculation
{
public int result(int a)
{
return a*a;
}
}
public class Square
{
public static void main(String[] args) {
SquareCalculation sq=new SquareCalculation();
Scanner sc=new Scanner(System.in);
System.out.print("Enter num :");
int num=Integer.parseInt(sc.nextLine());
int total=sq.result(num);
System.out.println("Square is "+total);
sc.close();
}
}2. Detailspublic class Details {
static void myDetails(String name, String city, String hobby)
{
System.out.println("Name :"+name+"\n"+"city :"+city+"\n"+"hobby :"+hobby);
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter name: ");
String name=sc.nextLine();
System.out.println("Enter city: ");
String city=sc.nextLine();
System.out.println("Enter hobby: ");
String hobby=sc.nextLine();
myDetails(name,city,hobby);
sc.close();
}
}3. Swappingpublic class SwapFunction {
static void fun(int a,int b)
{
int temp=a;
a=b;
b=temp;
System.out.printf("Swapping with temp variable a=%d and b=%d",a,b);
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Enter num1 :");
int num1=Integer.parseInt(sc.nextLine());
System.out.print("Enter num2 :");
int num2=Integer.parseInt(sc.nextLine());
fun(num1,num2);
sc.close();
}
}4. odd or evenpublic class OddorEven {
static void oddeven(int n)
{
if(n%2==0)
{
System.out.println("Given number is EVEN");
}
else{
System.out.println("Given number is ODD");
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num=sc.nextInt();
oddeven(num);
sc.close();
}
} |
Beta Was this translation helpful? Give feedback.
-
1//WAF to compute the square of a number
class Method1{
public static void square(int a){
System.out.println("The square of a\t"+a*a);
}
public static void main(String[] args) {
square(5);
}
}2class Sample{
void print(String name,String city,String hobby) {
System.out.println("Name: " + name + " \ncity: " + city+" \nhobby:"+hobby);
}
}
public class Method2 {
public static void main(String[] args) {
Sample sample = new Sample();
sample.print("Raj Kapoor","Chennai", "Dance");
}
}3public class Method3 {
public static void swapping(int a, int b) {
int temp = a;
a=b;
b=temp;
System.out.println("After swapping");
System.out.println("a: " + a+" b: " + b);
}
public static void main(String[] args) {
int a = 10,b=13;
System.out.println("Before swapping");
System.out.println("a: " + a+" b: " + b);
swapping(a,b);
}
}4public class Method4 {
public static void EvenOdd(){
int i=13;
if(i%2==0){
System.out.println("Even Number");}
else{ System.out.println("Odd Number");}
}
public static void main(String[] args) {
EvenOdd();
}
} |
Beta Was this translation helpful? Give feedback.
-
Methods1 Square programpackage methods;
public class SquareProgm {
static void method(int x){
int square=x*x;
System.out.println("Square:" +square);
}
public static void main(String[] args){
method(4);
}
}2package methods;
public class FormatDemo {
static void format(String name,String city,String Hobby){
System.out.println(String.format("Name :%s\ncity:%s\nHobby:%s\n",name,city,Hobby));
}
public static void main(String[] args){
format("Abitha Kannan","Tenkasi","Singing");
}
}3package methods;
public class SwapMethod{
static void swapdisplay(int a,int b){
int temp;
temp=a;
a=b;
b=temp;
System.out.printf("After swapping using third variable : a=%d, b=%d",a,b);
}
public static void main(String[] args){
swapdisplay(5,10);
}
}4package methods;
import java.util.Scanner;
public class OddEven {
static void method(int i){
if(i%2==0)
System.out.println("Number is even");
else
System.out.println("Number is odd");
}
public static void main(String[] args){
Scanner sc=new Scanner (System.in);
System.out.println("Enter the number");
int n=sc.nextInt();
method(n);
}
} |
Beta Was this translation helpful? Give feedback.
-
1waf1.javaimport java.util.Scanner;
public class waf1 {
public static int square(int x)
{
return x*x;
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Entert the number");
int n=Integer.parseInt(sc.nextLine());
System.out.println(square(n));
sc.close();
}
}Output2waf2.javaimport java.util.Scanner;
public class waf2 {
public static void show(String name,String city,String hobby)
{
System.out.println("Name: "+name);
System.out.println("City: "+city);
System.out.println("Hobby: "+hobby);
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the name: ");
String name=sc.nextLine();
System.out.print("Enter the city: ");
String city=sc.nextLine();
System.out.print("Your hobby: ");
String hobby=sc.nextLine();
show(name,city,hobby);
sc.close();
}
}Output3.waf3.javaimport java.util.Scanner;
public class waf3
{
public static void swap(int x,int y)
{
System.out.println(String.format("a=%d b=%d",x,y));
x=x*y;
y=x/y;
x=x/y;
System.out.println(String.format("a=%d b=%d",x,y));
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.print("Entert the values of a & b: ");
int a=Integer.parseInt(sc.nextLine());
int b=Integer.parseInt(sc.nextLine());
swap(a,b);
}
}Output4.import java.util.Scanner;
public class waf4 {
public static void oddev(int n)
{
if(n%2==0)
System.out.println((String.format("%d is even number",n)));
else
System.out.println((String.format("%d is odd number",n)));
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number: ");
int n=Integer.parseInt(sc.nextLine());
oddev(n);
sc.close();
}
}Output |
Beta Was this translation helpful? Give feedback.
-
TASK-METHODS1.WAF to compute the square of a number.SquareMethodDemo.javaimport java.util.Scanner;
public class SquareMethodDemo {
static void displaySquare(int a)
{
int square=a*a;
System.out.println(String.format("square of %d = %d",a,square));
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("ENTER A NUMBER :");
int a=Integer.parseInt(sc.nextLine());
displaySquare(a);
sc.close();
}
}Output2.WAF to print your name, city, and hobby in a formatted manner.Sample Output: DisplayDetail.javaimport java.util.Scanner;
public class DisplayDetail {
public static void dispaly(String name, String city,String hobby){
System.out.println("NAME : " +name);
System.out.println("CITY : " +city);
System.out.println("HOBBY : "+hobby);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("ENTER YOUR NAME :");
String name = sc.nextLine();
System.out.println("ENTER YOUR CITY :");
String city = sc.nextLine();
System.out.println("ENTER YOUR HOBBY :");
String hobby = sc.nextLine();
dispaly(name, city, hobby);
sc.close();
}
}Output3.WAF to swap two numbers.INPUT: a = 10, b = 13 SwapMethod.javaimport java.util.Scanner;
public class SwapMethod {
public static void swap(int a,int b)
{
System.out.println("VALUES BEFORE SWAPPING : a = " + a + " b = " + b);
int temp = a;
a=b;
b=temp;
System.out.println("VALUES AFTER SWAPPING : a = " + a + " b = " + b);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a :");
int a=sc.nextInt();
System.out.println("Enter b :");
int b=sc.nextInt();
swap(a,b);
}
}Output4.WAF to print whether a number is even or oddEvenOddMethod.javaimport java.util.Scanner;
public class EvenOddMethod {
public void check(int a)
{
if(a%2==0)
System.out.println(a+" IS EVEN NUMBER");
else
System.out.println(a+" IS ODD NUMBER");
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
EvenOddMethod ob=new EvenOddMethod();
System.out.println("ENTER A NUMBER :");
int a=Integer.parseInt(sc.nextLine());
ob.check(a);
sc.close();
}
}Output |
Beta Was this translation helpful? Give feedback.
-
1 Square of a numberimport java.util.*;
class SqNum{
static void display(int num){
int square = num*num;
System.out.println("Square of a number : "+square);
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number : ");
int num = sc.nextInt();
display(num);
}
}Output:2 Print Name, City and Hobbyimport java.util.*;
public class FormatDetails{
static void display(String name, String city, String hobby){
System.out.println("Name : " + name+"\nCity : " + city+"\nHobby : " +hobby);
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter name : " );
String name = sc.nextLine();
System.out.println("Enter city : ");
String city = sc.nextLine();
System.out.println("Enter hobby");
String hobby = sc.nextLine();
display(name, city, hobby);
}
}Output:3 Swappingimport java.util.*;
class Swap{
void display(int a, int b){
System.out.println("Before Swapping : "+" a : "+a+" b : "+b);
a = a+b;
b = a-b;
a = a-b;
System.out.println("After swapping : "+" a : "+a+" b : "+b);
}
}
public class SwapTwo{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
Swap s1 = new Swap();
s1.display(10, 5);
}
}Output:4 Even or Oddimport java.util.*;
public class EvenOdd{
static void display(int num){
if (num%2==0){
System.out.println("Number is even...");
}
else{
System.out.println("Number is odd...");
}
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number : ");
int num = sc.nextInt();
display(num);
}
}Output; |
Beta Was this translation helpful? Give feedback.
-
Problem 1 - Square Of a Numberimport java.util.Scanner;
public class MethodsSample1 {
static void Square(float side){
System.out.println(side*side);
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the side of square: ");
float side=sc.nextFloat();
Square(side);
sc.close();
}
}outputProblem 2 - PrintDetailspublic class MethodsSample2 {
static void printDetails(String name,String city,String hobby){
System.out.println("Name: "+name+"\nCity: "+city+"\nHobby: "+hobby);
}
public static void main(String[] args) {
String name="Vijey";
String city="Coimbatore";
String hobby="Reading Novels";
printDetails(name, city, hobby);
}
}outputProblem 3 - Swap Two Numbersimport java.util.Scanner;
class Swapping{
public void swap(int a,int b){
a+=b;
b=a-b;
a-=b;
System.out.println("After Swapping 12:\na = " + a + " b = " + b);
}
}
public class MethodsSample3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("a= ");
int a=sc.nextInt();
System.out.print("b=");
int b=sc.nextInt();
Swapping swapping = new Swapping();
swapping.swap(a,b);
}
}outputProblem 4 - Check a number is odd or evenimport java.util.Scanner;
public class MethodsSample4 {
static void checkEvenOrOdd(int num) {
if(num%2==0) {
System.out.println(num+" is Even Number");
}
else{
System.out.println(num+" is Odd Number");
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Enter a number: ");
int num=sc.nextInt();
checkEvenOrOdd(num);
sc.close();
}
}output |
Beta Was this translation helpful? Give feedback.
-
Square Numberimport java.util.Scanner;
public class SquareNumber {
public static void square(int a) {
System.out.println("Square of " + a + " is: " + a * a);
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int a=sc.nextInt();
sc.close();
square(a);
}
}Printing Detailspublic class PrintingDetails {
static void printDetails(String name, String city, String hobby) {
System.out.println("Name : " + name + "\n" + "City : " + city + "\n" + "Hobby : " + hobby);
}
public static void main(String[] args) {
String name = "Lakshmi";
String city = "Parkavi";
String hobby = "Drawing";
printDetails(name, city, hobby);
}
}Swap Numberspublic class SwapNumbers {
static void swap(int a, int b) {
int temp = a;
a = b;
b = temp;
System.out.println("After Swapping a = " + a + " b = " + b);
}
public static void main(String[] args) {
int a = 20;
int b = 27;
System.out.println("Before Swapping a = " + a + " b = " + b);
swap(a, b);
}
}Odd or Evenimport java.util.Scanner;
public class OddEven
{
static void oddEven(int a) {
if (a % 2 == 0) {
System.out.println("Even");
}
else {
System.out.println("Odd");
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number: ");
int n = scanner.nextInt();
oddEven(n);
scanner.close();
}
} |
Beta Was this translation helpful? Give feedback.
-
1 Square of a Given Numberimport java.util.Scanner;
public class SquareofNumber {
static void square(int num){
int squareOfNumber=num*num;
System.out.println("The square of the number is :"+squareOfNumber);
}
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number");
int num=sc.nextInt();
if (num!=0 && num!=1){
square(num);
}
}
}2 Printing the detailspublic class MyDetail {
static void myDetail(String name, String city, String hobby){
System.out.println(String.format("Name: %s \n City: %s \n hobby: %s", name, city,hobby));
}
public static void main(String[] args){
String name,city,hobby;
name="tamilarasi";
city="karur";
hobby="Singing";
myDetail(name,city,hobby);
}
}3 Swapping two numberspublic class Swap {
static void swap(int a, int b){
a=a+b;
b=a-b;
a=a-b;
System.out.println(" After Swapping a: " + a + " b: " + b);
}
public static void main(String[] args) {
int a=5,b=10;
System.out.println(" Before Swappping a: " + a + " b: " + b);
swap(a,b);
}
}4 Odd or Even Numberimport java.util.Scanner;
public class OddEven {
static void OddEven(int num){
if(num%2 == 0){
System.out.println(String.format("%d is even Number",num));
}
else {
System.out.println(String.format("%d is Odd Number",num));
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number");
int num=scanner.nextInt();
OddEven(num);
}
} |
Beta Was this translation helpful? Give feedback.
-
Find Square of a Numberpublic class Square {
public static void squareNum(int n) {
System.out.println("The square number is " + n*n);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number");
int n = sc.nextInt();
squareNum(n);
sc.close();
}
}Printing the Infopublic class MyInfo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the name: ");
String n = sc.nextLine();
System.out.print("Enter the city: ");
String c = sc.nextLine();
System.out.print("Enter your hobby: ");
String h = sc.nextLine();
printInfo(n,c,h);
sc.close();
}
private static void printInfo(String n, String c, String h) {
System.out.println("Name: " +n + "\n" + "City:" +c +"\n" + "Hobby:" + h);
}
}Swapping of Two Numberspublic class Swap {
public static void swapNum(int x, int y) {
int temp = x;
x = y;
y = temp;
System.out.println("After Swapping x = " + x + " y = " + y);
}
public static void main(String[] args) {
int x = 5;
int y = 10;
System.out.println("Before Swapping x = " + x + " y = " + y);
swapNum(x, y);
}
}Even or Oddpublic class EvenOdd {
public static void findEvenOrOdd(int n) {
if (n % 2 == 0) {
System.out.println("Given Number is EVEN");
}
else {
System.out.println("Given Number is ODD");
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number: ");
int n = scanner.nextInt();
findEvenOrOdd(n);
scanner.close();
}
} |
Beta Was this translation helpful? Give feedback.
-
1public class MainClass {
public int findSquare(int x){
return x*x;
}
public static void main(String[] args){
int number = 10;
MainClass obj = new MainClass();
int square = obj.findSquare(number);
System.out.println("square of number "+square);
}
}2public class MainClass {
public void details(String name,String city,String hobby){
System.out.println("Name : "+name);
System.out.println("City : "+city);
System.out.println("Hobby : "+hobby);
}
public static void main(String[] args){
String name = "Saratha";
String city = "Trichy";
String hobby = "Music";
MainClass obj = new MainClass();
obj.details(name,city,hobby);
}
}3public class MainClass {
public void findEvenOrOdd(){
int x, y, t;
x = 10;
y = 13;
System.out.println("before swapping numbers: "+x +" "+ y);
t = x;
x = y;
y = t;
System.out.println("After swapping: "+x +" " + y);
System.out.println( );
}
public static void main(String[] args){
MainClass obj = new MainClass();
obj.findEvenOrOdd();
}
}4public class MainClass {
public void findEvenOrOdd(int num){
if(num % 2 == 0){
System.out.println(num+" number is Even ");
}
else{
System.out.println(num+" number is Odd ");
}
}
public static void main(String[] args){
int number = 2;
MainClass obj = new MainClass();
obj.findEvenOrOdd(number);
}
} |
Beta Was this translation helpful? Give feedback.



















Uh oh!
There was an error while loading. Please reload this page.
-
WAF to compute the square of a number.
WAF to print your name, city, and hobby in a formatted manner.
Sample Output:
Beta Was this translation helpful? Give feedback.
All reactions