জাভা প্রোগ্রামিং পর্ব-০৮



2.6** (Summing the digits in an integer) Write a program that reads an integer between 0 and 1000 and adds all the digits in the integer. For example, if an integer is 932, the sum of all its digits is 14.
Hint: Use the % operator to extract digits, and use the / operator to remove the extracted digit. For instance, 932 % 10 = 2 and 932 / 10 = 93.

Code : File Name- SummingDigit.java
import java.util.*;
public class SummingDigit {
    public static void main (String arg[]){
         Scanner input =new Scanner(System.in);
        System.out.print("Enter Digit: = ") ;
        double digit= input.nextDouble();
            int sum=0, d, r;
            while(digit!=0){
            d=(int)(digit/10);
            r=(int)(digit%10);
            sum=(int)(sum+r);
            digit=d;
            }
            System.out.print("Sum: = " +sum);
        }
    }

2.7* (Finding the number of years) Write a program that prompts the user to enter the minutes (e.g., 1 billion) and displays the number of years and days for the minutes. For simplicity, assume a year has 365 days.

Code : File Name- FindingNumberofYear.java
import java.util.*;
public class FindingNumberofYear {
    public static void main (String arg[]){
        Scanner input= new Scanner (System.in);
        System.out.print("Enter the minutes: = ");
        int minutes =input.nextInt();

        int hours = minutes/60;
        int totaldays = hours/24;
        int years =totaldays/365;
        int days =totaldays%365;
        System.out.print(+years +"years and "+days +"days");
        }
    }

2.8* (Finding the character of an ASCII code) Write a program that receives an ASCII code (an integer between 0 and 128) and displays its character. For example, if the user enters 97, the program displays character a.

Code : File Name- FindingCharacter.java
import java.util.*;
public class FindingCharacter {
    public static void main (String arg[]){
        Scanner input = new Scanner (System.in);
        int code;
        System.out.print ("Enter an ASCII code : ");
        code = input.nextInt();
        int c = code%65;
        char ch = (char)((65+c));
        System.out.print(ch);
        }
    }

2.9* (Financial application: monetary units) Rewrite Listing 2.10, Compute- Change.java, to fix the possible loss of accuracy when converting a double value to an int value. Enter the input as an integer whose last two digits represent the cents. For example, the input 1156 represents 11 dollars and 56 cents.

Code : File Name- MonetaryUnits.java
import java.util.*;
public class MonetaryUnits {
    public static void main (String arg[]){
        Scanner input= new Scanner (System.in);
        System.out.print("Enter the amount: = ");
        int amount =input.nextInt();
        int dollar = amount/100;
        int cent = amount%100;

        System.out.print(+dollar +"Dollars and " + cent +"Cents");
        }
    }

2.10* (Using the GUI input) Rewrite Listing 2.10, ComputeChange.java, using the GUI input and output.

Code : File Name- GuiInput.java
import java.util.*;
import javax.swing.*;
public class GuiInput {
    public static void main (String arg[]){
        String amounts =JOptionPane.showInputDialog("Enter amount: ");
        double amount=Double.parseDouble(amounts);
        int dollar = (int)(amount/100);
        int cent = (int)(amount%100);

        String output = + dollar+"Dollars\n" +cent+"Cenys";
        JOptionPane.showMessageDialog(null, output);
        }
    }

Loading
Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Flying Twitter Bird Widget By ICT Sparkle