Showing posts with label if else in java. Show all posts
Showing posts with label if else in java. Show all posts

Saturday, September 12, 2015

Calculator using java

/*
* Write a C program for performing as calculator which allows all Arithmetic Operators with operands from
* terminal
 */
package cb_pr176;
import java.util.Scanner;
/**
 **
 * @author Arif
 **/
public class Cb_pr176 {
    /*
     * @param args the command line arguments
     */
    public static void main(String[] args) {
    // TODO code application logic here
    Scanner inp=new Scanner(System.in);
    int a,b;
    float c = 0;
    char ch;
    System.out.println("Enter Your 1st number:");
    a=inp.nextInt();
    System.out.println("Enter your 2nd number:");
    b=inp.nextInt();
    System.out.println("Enter your choice, example: +, -, *, /");
    ch=inp.next().charAt(0);
    if (ch=='+')
        c=a+b;
    else if(ch=='-')
        c=a-b;
    else if(ch=='/')
        c=(float)a/(float)b;
    else if(ch=='*')
        c=a*b;
    System.out.println("The Result is:" + c);
         
    }
  }

Tuesday, April 16, 2013

Greatest Common Divisor or GCD : JAVA OOP

Write Java Source Code on Printing the Greatest Common Divisor or GCD in Recursion

Sample Output:
Enter the base number: 100
Enter the power: 45
The GCD of 2 numbers is: 5

Source Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//java class
public class GCD
{
    public static int gcd(int num1, int num2)
    {
        if(num2 == 0)
        {
            return num1;
        }
        else
        {
            return gcd(num2, num1%num2);
        }
    }
}
//main class
import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
       Scanner input = new Scanner(System.in);
      System.out.print("Enter the base number: ");
      int num1 = input.nextInt();
      System.out.print("Enter the power: ");
      int num2 = input.nextInt();
      GCD access = new GCD();
      System.out.print("The GCD of 2 numbers is: " + access.gcd(num1, num2));
    }
}