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

Computer Project (1) (3)

The document provides Java code examples for various programming tasks including finding the maximum occurring digit in a number, generating consecutive natural numbers that sum to a given number, checking for prime pairs that sum to an even number, validating ISBN numbers, and overloading functions for calculating the area and perimeter of geometric shapes. Each task is accompanied by sample code and expected outputs. The code demonstrates fundamental programming concepts such as loops, conditionals, and function overloading.

Uploaded by

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

Computer Project (1) (3)

The document provides Java code examples for various programming tasks including finding the maximum occurring digit in a number, generating consecutive natural numbers that sum to a given number, checking for prime pairs that sum to an even number, validating ISBN numbers, and overloading functions for calculating the area and perimeter of geometric shapes. Each task is accompanied by sample code and expected outputs. The code demonstrates fundamental programming concepts such as loops, conditionals, and function overloading.

Uploaded by

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

Q.3.

Using the switch case statement, write a menu driven program to do the following:
(i) To accept a number from the user and print the digit which occurs maximum number of times in
the number. Also print its frequency.
(ii) To accept a number N and print all possible consecutive natural numbers that add upto N.
e.g.: N= 5
Output = 1 2 3 4 5
456
78
(i)

Finds maximum occurring digit


// without using any array/string
import java.io.*;
class GFG
{
// Simple function to count
// occurrences of digit d in x
static int countOccurrences(int x,int d)
{ // Initialize count
// of digit d
int count = 0;
while (x > 0)
{ // Increment count if
// current digit is
// same as d
if (x % 10 == d)
count++;
x = x / 10;
}
return count;
}
// Returns the max
// occurring digit in x
static int maxOccurring( int x)
{
// Handle negative number
if (x < 0)
x = -x;
// Initialize result
// which is a digit
int result = 0;
// Initialize count
// of result
int max_count = 1;

// Traverse through
// all digits
for (int d = 0; d <= 9; d++)
{ // Count occurrences
// of current digit
int count = countOccurrences(x, d);
// Update max_count
// and result if needed
if (count >= max_count)
{ max_count = count;
result = d;
}
}
return result;
}
// Driver Code
public static void main (String[] args)
{ int x = 1223355;
System.out.println("Max occurring digit is " +
maxOccurring(x));
}
}

Output
Max occurring digit is 5

(i) Java program to find

// the frequency of a
// digit in a number
class GFG
{

// function to find frequency


// of digit in a number
static int frequencyDigits(int n,
int d)
{
// Counter variable to
// store the frequency
int c = 0;

// iterate till number


// reduces to zero
while (n > 0)
{

// check for equality


if (n % 10 == d)
c++;
// reduce the number
n = n / 10;
}
return c;
}

// Driver Code
public static void main(String args[])
{
// input number N
int N = 1122322;

// input digit D
int D = 2;

System.out.println(frequencyDigits(N, D));
}
}

Examples :
Input: N = 1122322 , D = 2
Output: 4
(ii)

import java.util.*;

public class Consecutive

public static void main(String arr[])

Scanner sc=new Scanner(System.in);

int i,j,k,n,sum;

System.out.println("Enter a number");

n=sc.nextInt();

for(i=1;i<=n/2+1;i++)

sum=0;

for(j=i;j<=n/2+1;j++)

sum=sum+j;

if(sum==n)

break;
}

if(j<=n/2+1)

for(k=i;k<=j;k++)

System.out.print(k+" ");

System.out.println();

Copy
Solution 2: (Using Function)
import java.util.*;

class number

int sum(int i,int num)

int s1=0;

for(int x=i;s1<num;x++)

s1=s1+x;

return (s1);

public static void main(String args[])


{

Scanner sc=new Scanner (System.in);

System.out.println("Enter a number");

int n=sc.nextInt();

int s;

number obj=new number();

for(int j=1;j<=n;j++)

int ans=obj.sum(j,n);

s=0;

if(ans==n)

for(int y=j;s<n;y++)

s=s+y;

System.out.print(y+" ");

System.out.println();

Output
Enter a number

21
1 2 3 4 5 6
6 7 8
10 11
21
Test 2:

Enter a number
15
12345
456
78
Q.5. Every even integer greater than 2 can be expressed as the sum of two primes. e.g.: n = 44 3+41
44 (3 , 41) both are prime Write a program to enter an even number N and store all the prime
numbers upto N. Find and print the pair of prime numbers which add up to give N.
import java.util.Scanner;
public class PrimeNumbers{
public static void main(String arg[]){
int i,n,counter, j;
Scanner scanner = new Scanner(System.in);
System.out.println("Required packages have been imported");
System.out.println("A reader object has been defined ");
System.out.print("Enter the n value : ");
n=scanner.nextInt();
System.out.print("Prime numbers between 1 to 10 are ");
for(j=2;j<=n;j++){
counter=0;
for(i=1;i<=j;i++){
if(j%i==0){
counter++;
}
}
if(counter==2)
System.out.print(j+" ");
}
}
}

Output
Enter the n value : 10
Prime numbers between 1 to 10 are 2 3 5 7

Q.7. The International Standard Book Number (ISBN) is a unique numeric


book identifier which is
printed on every book. The ISBN is a 10 digit code. The ISBN is legal if:
1 x digit + 2 x digit + 3 x digit + 4 x digit + 5 x digit + 6 x digit + 7 x digit +
8 x digit + 9 x digit + 10
x digit is divisible by 11.
Example: For an ISBN 1401601499
Sum = 1x1 + 2x4 + 3x0+ 4x1 + 5x6+ 6x0+ 7x1 + 8x4 + 9x9 + 10x9 = 253
which is divisible by 11.
(i) Input the ISBN code as a 10 digit integer.
(ii) If the ISBN is not a 10 digit integer, output the message, "Illegal ISBN" and
terminate the
program.
(iii) If the number is 10 digits, extract the digits of the number and compute
the sum as
explained above.
If the sum is divisible by 11, output the message, "Legal ISBN". If the sum is
not divisible by 11,
output the message, "Illegal ISBN".

import java.util.Scanner;

public class KboatISBNCheck


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the ISBN: ");
long isbn = in.nextLong();

int sum = 0, count = 0, m = 10;


while (isbn != 0) {
int d = (int)(isbn % 10);
count++;
sum += d * m;
m--;
isbn /= 10;
}

if (count != 10) {
System.out.println("Illegal ISBN");
}
else if (sum % 11 == 0) {
System.out.println("Legal ISBN");
}
else {
System.out.println("Illegal ISBN");
}
}
}

OUTPUT

Enter the ISBN number: 1401601499


Legal ISBN
Enter the ISBN number: 1401601500
Illegal ISBN
Q.10. Design a class to overload a function triangle() as follows:
(a) boolean triangle (int a, int b, int c) with three integer arguments and
return true if the
triangle is equable otherwise false.
(A triangle is said to be an 'Equable Triangle' if the area of triangle is equal to
its
perimeter).
(b) double triangle (double a, double b, double c) with three double
arguments and
calculate and returns the area of triangle (using the Heron's formula):
formula A = √𝑠(𝑠 − 𝑎)(𝑠 − 𝑏)(𝑠 − 𝑐)
where s = (a + b + c)/2)
(c) void triangle (float a, float b, float c) with three float arguments. It checks
whether the
triangle formed by the given sides is a right angled triangle or not.

(b)
import java.util.Scanner;

public class KboatOverload


{
double area(double a, double b, double c) {
double s = (a + b + c) / 2;
double x = s * (s-a) * (s-b) * (s-c);
double result = Math.sqrt(x);
return result;
}

double area (int a, int b, int height) {


double result = (1.0 / 2.0) * height * (a + b);
return result;
}

double area (double diagonal1, double diagonal2) {


double result = 1.0 / 2.0 * diagonal1 * diagonal2;
return result;
}
}
Q.9. Write a class with the name Perimeter using function overloading that computes the
perimeter of a square, a rectangle and a circle.
Formula: Perimeter of a square = 4*s
Perimeter of a rectangle 2*(1+b)
Perimeter of a circle = 2*π * r

import java.util.Scanner;

public class Perimeter


{
public double perimeter(double s) {
double p = 4 * s;
return p;
}

public double perimeter(double l, double b) {


double p = 2 * (l + b);
return p;
}

public double perimeter(int c, double pi, double r) {


double p = c * pi * r;
return p;
}

public static void main(String args[]) {


Scanner in = new Scanner(System.in);
Perimeter obj = new Perimeter();

System.out.print("Enter side of square: ");


double side = in.nextDouble();
System.out.println("Perimeter of square = " +
obj.perimeter(side));

System.out.print("Enter length of rectangle: ");


double l = in.nextDouble();
System.out.print("Enter breadth of rectangle: ");
double b = in.nextDouble();
System.out.println("Perimeter of rectangle = " +
obj.perimeter(l, b));

System.out.print("Enter radius of circle: ");


double r = in.nextDouble();
System.out.println("Perimeter of circle = " + obj.perimeter(2,
3.14159, r));
}
}

OUTPUT

You might also like