Thursday, 29 October 2020

Strings Anagram or not

Write a Java Program to Check Strings Anagram or not to each other 
import java.util.Scanner;
public class StringAnagram 
{
	public static void main(String[] args) 
	{
		String s1,s2; //for input strings
		int length, length1, length2;//for string lengths
		int i, j; //for 2 iteration
		int found=0, not_found=0; //to check string anagram of not
		Scanner scan = new Scanner(System.in);
		//taking string input from user
		System.out.println("Enter first String :- ");
		s1=scan.nextLine();//scanning first string
		System.out.println("Enter Second String :- ");
		s2=scan.nextLine();//scanning second string
	
		length1=s1.length();
		length2=s2.length();
		//checking condition both strings have same length or not
		if(length1==length2)
		{
			length=length1;
                        for(i=0;i<length;i++)
			{
			   found=0;
			for(j=0;j<length;j++)
			{
			//condition to check strings are anagram or not
			if(s1.charAt(i)==s2.charAt(j))
			{
				found=1;//if same then found=1
				break;
			}
			}
			//if not same then not_found=1
			if(found==0)
			{
			not_found=1;//means both strings are not anagram 
			break;
			}
		}
		if(not_found==1)
		{
		System.out.println("strings are not anagram to each other");
		}
		else
		{
		System.out.println("strings are anagram to each other");
		}
	}
else
{
System.out.println("please enter same length strings to be an anagram");
}
}
}
Output:

Enter a Number: 
159874632

 Given Number:-159874632

 After Reverse :-236478951

Tuesday, 27 October 2020

Reverse Number Using for loop

Write a Java Program to Reverse Number Using for loop  
 
import java.util.Scanner;
public class ReverseUsingFor 
{
	public static void main(String[] args) 
	{
		int reminder,reverse=0,temp;
		Scanner scan = new Scanner(System.in);
		System.out.println("\n\nEnter a Number: ");
		int n = scan.nextInt();
		temp=n;
			for( ; n!=0 ; )
			{
				reminder=n%10;
				reverse=reverse*10+reminder;
				n=n/10;
			}
		System.out.println("\n Given Number:-"+temp);
		System.out.println("\n After Reverse :-" + reverse);
	}
}


Output:
Enter a Number: 
159874632

 Given Number:-159874632

 After Reverse :-236478951

Reverse Number Using do-while loop

Write a Java Program to Reverse Number Using do-while loop 
import java.util.Scanner;
public class ReverseDoWhile 
{
	public static void main(String[] args) 
	{
		int reminder,reverse=0,temp;
		Scanner scan = new Scanner(System.in);
		System.out.println("\n\nEnter a Number: ");
		int n = scan.nextInt();
		temp=n;
			do
			{
				reminder=n%10;
				reverse=reverse*10+reminder;
				n=n/10;
			}while(n!=0);
		System.out.println("\n Given Number:-"+temp);
		System.out.println("\n After Reverse :-" + reverse);
	}
}

Output:

Enter a Number: 
159874632

 Given Number:-159874632

 After Reverse :-236478951


Reverse Number without user interaction

Write a Java Program to Reverse Number without user interaction 

public class ReverseNumberWithoutUserInteraction 
{
	public static void main(String[] args) 
	{
		int number=159623487;
		int reminder;
		int reverse=0;
		int temp=number;
		while(number!=0)
		{
			reminder=number%10;
			reverse=reverse*10+reminder;
			number=number/10;
		}
		System.out.println("\n\n Given Number:-"+temp);
		System.out.println("\n After Reverse :-" + reverse);	
	}
}

Output:

Enter a Number: 
159874632

 Given Number:-159874632

 After Reverse :-236478951





Reverse Number Using method

Write a Java Program to Reverse Number Using method 
import java.util.Scanner;
public class ReverseUsingMethod 
{
	void reverse()
	{
		int reminder,reverse=0,temp;
		Scanner scan = new Scanner(System.in);
		System.out.println("\n\nEnter a Number: ");
		int n = scan.nextInt();
		temp=n;
			while(n>0)
			{
				reminder=n%10;
				reverse=reverse*10+reminder;
				n=n/10;
			}
		System.out.println("\n Given Number:-"+temp);
		System.out.println("\n After Reverse :-" + reverse);
	}
	public static void main(String[] args) 
	{
		ReverseUsingMethod obj = new ReverseUsingMethod();
		obj.reverse();
	}
}




Output:

Enter a Number: 159874632

 Given Number:-159874632
 After Reverse :-236478951



Reverse Number Using While loop

Write a Java Program to Reverse Number Using While loop 


import java.util.Scanner;
public class reverse 
{
	public static void main(String[] args) 
	{
		int reminder,reverse=0,temp;
		Scanner scan = new Scanner(System.in);
		System.out.println("\n\nEnter a Number: ");
		int n = scan.nextInt();
		temp=n;
			while(n>0)
			{
				reminder=n%10;
				reverse=reverse*10+reminder;
				n=n/10;
			}
		System.out.println("\n Given Number:-"+temp);
		System.out.println("\n After Reverse :-" + reverse);
	}
}

Output:

Enter a Number: 
159874632

 Given Number:-159874632

 After Reverse :-236478951

Thursday, 22 October 2020

Menu Driven Calculator

Write a Java Program to make a Menu Driven Calculator
import java.util.Scanner;
public class CalciMenuDriven 
{
	public static void main(String[] args) 
	{
		int n1,n2;
		int ch=0,ans = 0;
		int choice;
		Scanner scan = new Scanner(System.in);
		System.out.println("Enter First Number: ");
		n1=scan.nextInt();
		System.out.println("Enter Second Number: ");
		n2=scan.nextInt();
		do
		{
			System.out.println("Enter Your Choice  1. Addition 
            		2.Substraction 3. Multiplication 4. Division");
			choice=scan.nextInt();
			switch(choice)
			{
				case 1 :
					ans=n1+n2;
				System.out.println(n1+" "+ "+" +" "+n2+" = "+ans);
					break;
				case 2 :
					ans=n1-n2;
				System.out.println(n1+" "+ "-" +" "+n2+" = "+ans);
					break;
				case 3 :
					ans=n1*n2;
				System.out.println(n1+" "+ "*" +" "+n2+" = "+ans);
					break;
				case 4 :
					ans=n1/n2;
				System.out.println(n1+" "+ "/" +" "+n2+" = "+ans);
					break;
				default:
					System.out.println("Wrong input....");
					System.out.println("Exit...");
					return;
			}
		}while(choice!=5);
	}
}
Output:

Enter First Number: 
50
Enter Second Number: 
10
Enter Your Choice
  1. Addition
  2. Substraction
  3. Multiplication
  4. Division

1
50 + 10 = 60
Enter Your Choice
  1. Addition
  2. Substraction
  3. Multiplication
  4. Division

2
50 - 10 = 40
Enter Your Choice
  1. Addition
  2. Substraction
  3. Multiplication
  4. Division

3
50 * 10 = 500
Enter Your Choice
  1. Addition
  2. Substraction
  3. Multiplication
  4. Division

4
50 / 10 = 5
Enter Your Choice
  1. Addition
  2. Substraction
  3. Multiplication
  4. Division

5
Wrong input....
Exit... 




  1. Write a Java Program to make simple Calculator using methods
  2. Write a Java Program to make simple Calculator using if
  3. Write a Java Program to make a Menu Driven Calculator
  4. Write a Java Program to make simple Calculator using Equals
  5. Write a Java Program to make simple Calculator using switch...case

Calculator using Equals

Write a Java Program to make Calculator using Equals 
import java.util.Scanner;
public class CalciUsingEquals 
{
	public static void main(String[] args) 
	{
		int ans=0;
		Scanner s = new Scanner(System.in);
		System.out.println("Enter First Number...");
		int n1=s.nextInt();
		System.out.println("Enter Second Number...");
		int n2=s.nextInt();
		System.out.println("Enter Operator (+, -, *, /)...");
		String operation=s.next();
		System.out.println(n1+" "+ "*" +" "+n2+" = ");
			if(operation.equals("+"))
			{
				System.out.println(ans=n1+n2);
			}
			if(operation.equals("-"))
			{
				System.out.println(ans=n1-n2);
			}
			if(operation.equals("*"))
			{
				System.out.println(ans=n1*n2);
			}
			if(operation.equals("/"))
			{
				System.out.println(ans=n1/n2);
			}
	}
}


Output:
Enter First Number...
10
Enter Second Number...
5
Enter Operator (+, -, *, /)...
/
10 * 5 = 2


  1. Write a Java Program to make simple Calculator using methods
  2. Write a Java Program to make simple Calculator using if
  3. Write a Java Program to make a Menu Driven Calculator
  4. Write a Java Program to make simple Calculator using Equals
  5. Write a Java Program to make simple Calculator using switch...case


Calculator using if

Write a Java Program to make Calculator using if 
import java.util.Scanner;
public class CalciUsingIf 
{
	public static void main(String[] args) 
	{
		int ans=0;
		Scanner s = new Scanner(System.in);
		System.out.println("Enter First Number...");
		int n1=s.nextInt();
		System.out.println("Enter Second Number...");
		int n2=s.nextInt();
		System.out.println("Enter Operator (+, -, *, /)...");
		char op = s.next().charAt(0);
		System.out.println(n1+" "+ op +" "+n2+" = ");
			if(op=='+')
			{
				System.out.println(ans=n1+n2);
			}
			if(op=='-')
			{
				System.out.println(ans=n1-n2);
			}
			if(op=='*')
			{
				System.out.println(ans=n1*n2);
			}
			if(op=='/')
			{
				System.out.println(ans=n1/n2);
			}
	}
}
Output:

Enter First Number...

5

Enter Second Number...

6

Enter Operator (+, -, *, /)...

+

5 * 6 = 11



  1. Write a Java Program to make simple Calculator using methods
  2. Write a Java Program to make simple Calculator using if
  3. Write a Java Program to make a Menu Driven Calculator
  4. Write a Java Program to make simple Calculator using Equals
  5. Write a Java Program to make simple Calculator using switch...case

Thursday, 1 October 2020

Calculator using methods

Write a Java Program to make simple Calculator using methods 
import java.util.Scanner;
public class CalculatorUsingMethod 
{
	public int n1,n2;
	public int ans = 0;
	CalculatorUsingMethod()
	{
		Scanner scan = new Scanner(System.in);
		System.out.println("Enter First Number: ");
		n1=scan.nextInt();
		System.out.println("Enter Second Number: ");
		n2=scan.nextInt();
	}
	void add()
	{
		ans=n1+n2;
		System.out.println(n1+" "+ "+" +" "+n2+" = "+ans);
	}
	void sub()
	{
		ans=n1-n2;
		System.out.println(n1+" "+ "-" +" "+n2+" = "+ans);
	}
	void mul()
	{
		ans=n1*n2;
		System.out.println(n1+" "+ "*" +" "+n2+" = "+ans);
	}
	void div()
	{
		ans=n1/n2;
		System.out.println(n1+" "+ "/" +" "+n2+" = "+ans);
	}
	public static void main(String[] args) 
	{
		CalculatorUsingMethod obj = new CalculatorUsingMethod();
		Scanner scan = new Scanner(System.in);
		System.out.println("Enter The Operator (+, -, *, /):  ");
		char operator=scan.next().charAt(0);
		switch(operator)
		{
			case '+' :
				obj.add();
				break;
			case '-' :
				obj.sub();
				break;
			case '*' :
				obj.mul();
				break;
			case '/' :
				obj.div();
				break;
			default:
			System.out.println("You have entered wrong operator...");
		}
	}
}
Output:
Enter First Number: 
30
Enter Second Number: 
5
Enter The Operator (+, -, *, /):  
/
30 / 5 = 6

Calculator using simple switch...case

Write a Java Program to make Calculator using switch...case 
import java.util.Scanner;
public class calculator 
{
	public static void main(String[] args) 
	{
		int n1,n2;
		int ans = 0;
		Scanner scan = new Scanner(System.in);
		System.out.println("Enter First Number: ");
		n1=scan.nextInt();
		System.out.println("Enter Second Number: ");
		n2=scan.nextInt();
		System.out.println("Enter The Operator (+, -, *, /):  ");
		char operator=scan.next().charAt(0);
			switch(operator)
			{
				case '+' :
					ans=n1+n2;
					break;
				case '-' :
					ans=n1-n2;
					break;
				case '*' :
					ans=n1*n2;
					break;
				case '/' :
					ans=n1/n2;
					break;
				default:
					System.out.println("Wrong input....");
					System.out.println("Exit...");
			}
		System.out.println(n1+" "+ "*" +" "+n2+" = "+ans);	
	}
}
Output:
Enter First Number: 
10
Enter Second Number: 
20
Enter The Operator (+, -, *, /):  
*
10 * 20 = 200       



Pattern 31 & 32