Showing posts with label Calculator. Show all posts
Showing posts with label Calculator. Show all posts

Java Program to Make Simple Calculator

/* Java Program Example - Make Calculator */

import java.util.Scanner;

public class JavaProgram
{
    public static void main(String args[])
    {
        float a, b, res;
        char choice, ch;
        Scanner scan = new Scanner(System.in);

        do
        {
            System.out.print("1. Addition\n");
            System.out.print("2. Subtraction\n");
            System.out.print("3. Multiplication\n");
            System.out.print("4. Division\n");
            System.out.print("5. Exit\n\n");
            System.out.print("Enter Your Choice : ");
            choice = scan.next().charAt(0);
            switch(choice)
            {
                case '1' : System.out.print("Enter Two Number : ");
                    a = scan.nextFloat();
                    b = scan.nextFloat();
                    res = a + b;
                    System.out.print("Result = " + res);
                    break;
                case '2' : System.out.print("Enter Two Number : ");
                    a = scan.nextFloat();
                    b = scan.nextFloat();
                    res = a - b;
                    System.out.print("Result = " + res);
                    break;
                case '3' : System.out.print("Enter Two Number : ");
                    a = scan.nextFloat();
                    b = scan.nextFloat();
                    res = a * b;
                    System.out.print("Result = " + res);
                    break;
                case '4' : System.out.print("Enter Two Number : ");
                    a = scan.nextFloat();
                    b = scan.nextFloat();
                    res = a / b;
                    System.out.print("Result = " + res);
                    break;
                case '5' : System.exit(0);
                    break;
                default : System.out.print("Wrong Choice!!!");
                    break;
            }
            System.out.print("\n---------------------------------------\n");
        }while(choice != 5);       
    }
}

Tip Calculator in Swift Programming

How to write a program to create Tip Calculator in Swift Programming


//
//  ViewController.swift
//  Tip Calculator
//

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

    //Outlets
    @IBOutlet weak var tenPercent: UIButton!
    @IBOutlet weak var fifteenPercent: UIButton!
    @IBOutlet weak var twentyPercent: UIButton!
    @IBOutlet weak var twentyFivePercent: UIButton!
    @IBOutlet weak var billInput: UITextField!
    @IBOutlet weak var finalTipAmount: UILabel!
    @IBOutlet weak var splitDisplay: UILabel!
    @IBOutlet weak var personDisplay: UILabel!
    @IBOutlet weak var downButtonOutlet: UIButton!
    
    @IBAction func billInputAction(sender: UITextField) {
        billInput.resignFirstResponder()
        billAmount = (Double)(billInput.text!)!
    }
    @IBAction func viewTapped(sender : AnyObject) {
        billInput.resignFirstResponder()
        billAmount = (Double)(billInput.text!)!
        
    }
    
    
    @IBAction func tenPercent(sender: UIButton) {
        resetSelectedPercentage(10)
    }
    @IBAction func fifteenPercent(sender: AnyObject) {
        resetSelectedPercentage(15)
    }
    @IBAction func twentyPercent(sender: UIButton) {
        resetSelectedPercentage(20)
    }
    @IBAction func twentyFivePercent(sender: AnyObject) {
        resetSelectedPercentage(25)
    }
    
    @IBAction func calculateButton(sender: UIButton) {
        let tempResult = (billAmount * gratuityPercent) / (Double)(peopleToSplit)
        finalTipAmount.text = "$" + (NSString(format: "%.2f", tempResult) as String)
    }
    @IBAction func upButton(sender: AnyObject) {
        peopleToSplit++
        splitDisplay.text = (String)(peopleToSplit)
        if(peopleToSplit > 1){
            personDisplay.text = "People"
            downButtonOutlet.enabled = true
        }
    }
    @IBAction func downButton(sender: AnyObject) {
        peopleToSplit--
        splitDisplay.text = (String)(peopleToSplit)
        if(peopleToSplit == 1){
            personDisplay.text = "Person"
            downButtonOutlet.enabled = false
        }
    }
    
    
    
    //Variables
    var gratuityPercent = 0.0
    var billAmount = 0.0
    var peopleToSplit = 1
    
    
 
    //Functions
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        billAmount = (Double)(billInput.text!)!
        billInput.resignFirstResponder()
        return true
    }
    
    func textFieldDidEndEditing(textField: UITextField) {
        billAmount = (Double)(billInput.text!)!
    }
    
    
   
    func resetSelectedPercentage(percentage:Int){
        tenPercent.setImage(UIImage(named: "10_unselected_image"), forState: UIControlState.Normal)
        fifteenPercent.setImage(UIImage(named: "15_unselected_image"), forState: UIControlState.Normal)
        twentyPercent.setImage(UIImage(named: "20_unselected_image"), forState: UIControlState.Normal)
        twentyFivePercent.setImage(UIImage(named: "25_unselected_image"), forState: UIControlState.Normal)
        switch percentage {
        case 10:
            gratuityPercent = 0.1
            tenPercent.setImage(UIImage(named: "10_selected_image"), forState: UIControlState.Normal)
        case 15:
            gratuityPercent = 0.15
            fifteenPercent.setImage(UIImage(named: "15_selected_image"), forState: UIControlState.Normal)
        case 20:
            gratuityPercent = 0.2
            twentyPercent.setImage(UIImage(named: "20_selected_image"), forState: UIControlState.Normal)
        case 25:
            gratuityPercent = 0.25
            twentyFivePercent.setImage(UIImage(named: "25_selected_image"), forState: UIControlState.Normal)
        default:
            gratuityPercent = 0.0
        }
        
    }


}