0% found this document useful (0 votes)
15 views

9_10 INTRO BASIC PROGRAMS

The document outlines four Java programs that perform various operations on two-digit numbers and other integers. Program 1 calculates the sum of the digits of a two-digit number, Program 2 computes the difference between the digits, Program 3 finds the next multiple of 100 for a given number, and Program 4 sums three rounded integers based on specific rounding rules. Each program includes a main class for user input and calls the respective methods from their defined classes.

Uploaded by

duneshvasa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

9_10 INTRO BASIC PROGRAMS

The document outlines four Java programs that perform various operations on two-digit numbers and other integers. Program 1 calculates the sum of the digits of a two-digit number, Program 2 computes the difference between the digits, Program 3 finds the next multiple of 100 for a given number, and Program 4 sums three rounded integers based on specific rounding rules. Each program includes a main class for user input and calls the respective methods from their defined classes.

Uploaded by

duneshvasa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

PROGRAM 1

Define a method which returns the sum of digits of the given two digit number.
Write the method with the following specifications:
Name of the BLC class:TwoDigitsSum
Name of method: getSumOfDigits()
Access Modifier : public, static
Arguments: one argument of type integer
Return type: an integer value
For example,
If the given value is 34, return 7

Create an ELC class Main to test the application

package sep10;

import java.util.*;
public class p1 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter integer: ");


int n = sc.nextInt();

System.out.println("Sum: "+ TwoDigitsSum.getSumOfDigits(n));


sc.close();

package sep10;

public class TwoDigitsSum {

public static int getSumOfDigits(int n){


int s;
for(s=0;n!=0;n=n/10) {
s+=n%10;
}
return s;

}
Program-2
Define a method which returns the difference of digits of the given two digit number.
Write the method with the following specifications:
Name of the BLC class:TwoDigitsDifference
Name of method: getDiffOfDigits()
Access Modifier : public, static
Arguments: one argument of type integer
Return type: an integer value
For example,
If the given value is 83, 8 - 3 return 5. If x = 38, 3 - 8 return -5.

Create an ELC class Main to test the application

package sep10;

import java.util.*;

public class p2 {

public static void main(String[] args) {

System.out.print("Enter the digit: ");


Scanner sc = new Scanner(System.in);
int n = sc.nextInt();

TwoDigitsDifference.getDiffOfDigits(n);
sc.close();

package sep10;

public class TwoDigitsDifference {

public static void getDiffOfDigits(int n) {

System.out.println("Difference: "+(n/10-n%10));

}
Program-3
Define a method which returns the next multiple of 100 for the given number.
Write the method with the following specifications:
Name of the BLC class:NextMultipleOfHundred
Name of method: getNextMultipleOfHundred()
Access Modifier : public, static
Arguments: one argument of type integer
Return type: an integer value
For example,
If the given value is 123, return 200.

Create an ELC class Main to test the application

package sep10;

import java.util.*;

public class p3 {

public static void main(String[] args) {

System.out.println("Enter a number:");
Scanner sc = new Scanner(System.in);
int n =sc.nextInt();

NextMultipleOfHundred.getNextMultipleOfHundred(n);

sc.close();
}

package sep10;

public class NextMultipleOfHundred {


public static void getNextMultipleOfHundred(int n) {

System.out.println("Result: "+(n/100+1)*100);

}
}
Program-4
Define a method which returns the sum of three rounded numbers. If the right most digit of the
number is less than 5, then round off its value to the previous multiple of 10 otherwise if the right
most digit of the number is greater or equal to 5, and then round off to the next multiple of 10.
Write the method with the following specifications:
Name of the BLC class:RoundedSum
Name of method: sumOfRoundedValues()
Access Modifier : public, static
Arguments: three argument of type integer
Return type: an integer value
For example
If a = 23, b = 34, c = 66
20 + 30 + 70 = 120
Note:Don't use any control statements and looping statements

Create an ELC class Main to test the application

package sep10;

import java.util.Scanner;

public class p4 {

public static void main(String[] args) {

System.out.println("Enter a number:");
Scanner sc = new Scanner(System.in);
int a =sc.nextInt();
int b =sc.nextInt();
int c =sc.nextInt();

RoundedSum.sumOfRoundedValues(a, b, c);
sc.close();
}

package sep10;

public class RoundedSum {

public static void sumOfRoundedValues(int a, int b, int c) {


System.out.println("Add: "+(round(a)+round(b)+round(c)));
}
public static int round(int num) {
return ((num + 5) / 10) * 10;
}
}

You might also like