0% found this document useful (0 votes)
3 views4 pages

FUNCTIONS

The document provides Java code examples for three different programs: one for calculating the sum and difference of two integers, another for displaying a series based on user choice or checking if a number is a neon number, and a third for performing basic arithmetic operations based on user input. Each program utilizes functions to encapsulate logic and employs user input for interaction. The code demonstrates the use of conditionals, loops, and basic arithmetic operations in Java.

Uploaded by

hkschool999
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

FUNCTIONS

The document provides Java code examples for three different programs: one for calculating the sum and difference of two integers, another for displaying a series based on user choice or checking if a number is a neon number, and a third for performing basic arithmetic operations based on user input. Each program utilizes functions to encapsulate logic and employs user input for interaction. The code demonstrates the use of conditionals, loops, and basic arithmetic operations in Java.

Uploaded by

hkschool999
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

FUNCTIO

NS
1. Wap to accept two integers from the user and print their sum and difference using
functions/methods in java.

Sol.
import java.util.*;
public class Main {
public static void Sum(int a, int b)
{
System.out.println(a + b);
return;
}
public static void diff(int a, int b)
{

System.out.println(a - b);
return;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
System.out.print("sum= ");
Sum(a, b);
System.out.print("difference= ");
diff(a,b);

}
}

2. Wap in java to accept user’s choice in main and display the following as per the
user’s choice:

Choice A or a => 10/1 + 20/2 + 30/6 + 40/4 … nth term


Choice B or b => Input a number and check if it’s a neon number or not {neon number is a
number where the sum of the digits of the square = the number itself, for e.g., 9*9 = 81, 8+1
= 9, so 9 is a neon number

sol.
import java.util.*;
public class Main {
static void series(int a)
{
int f = 1, num = 0;
double sum = 0.0;
for(int i = 1, j = 10; i <= a; i++, j+= 10)
{
num = j;
f *= i;
System.out.print(num + "/" + f + " ");
sum += (double)(num/f);
}
System.out.print(" = " + sum);
}
static void neon(int n)
{
int s = 0, d, sq = n*n;
for(int i = sq; i>0; i/=10)
{
d = i % 10;
s+=d;
}
if(s == n)
{
System.out.println(n + " is a neon number");
}
else {
System.out.println(n + " is not a neon number");
}
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
System.out.println("Enter A/a for series");
System.out.println("Enter B/b to check whether the given number is
a neon number or not");
System.out.println("Enter any other choice to quit the program");

char ch = sc.next().charAt(0);
switch(ch)
{
case 'A':
case 'a':
int a = sc.nextInt();
series(a);
break;

case 'B':
case 'b':
int n = sc.nextInt();
neon(n);
break;

default:
System.out.println("invalid choice");
System.exit(0);
}
}
}
3. Guided comp appl. Prog 32 pg – 201, conditional statements.

Sol.

import java.util.*;
public class Main {
static void addition(int n1, int n2)
{
System.out.println("The sum of " + n1 + " and " + n2 + " is " + (n1
+ n2));
return;
}
static void subtraction(int n1, int n2)
{
System.out.println("The difference of " + n1 + " and " + n2 + " is
" + (n1 - n2));
return;
}
static void product(int n1, int n2)
{
System.out.println("The product of " + n1 + " and " + n2 + " is " +
(n1 * n2));
return;
}
static void division(int n1, int n2)
{
System.out.println("The quotient of " + n1 + " and " + n2 + " is "
+ (n1 / n2));
return;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter two integers: ");
int n1 = in.nextInt();
int n2 = in.nextInt();

System.out.println("1. Enter '+' for addition");


System.out.println("2. Enter '-' for subtraction");
System.out.println("3. Enter '*' for multiplication");
System.out.println("4. Enter '/' for division");
char ch = in.next().charAt(0);
switch(ch)
{
case '+':
addition(n1, n2);
break;

case '-':
subtraction(n1, n2);
break;

case '*':
product(n1, n2);
break;

case '/':
division(n1, n2);
break;

default:
System.out.println("Wrong input, try again.");
}
}
}

You might also like