Computer Project (1) (3)
Computer Project (1) (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)
// 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
// the frequency of a
// digit in a number
class GFG
{
// 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.*;
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 s1=0;
for(int x=i;s1<num;x++)
s1=s1+x;
return (s1);
System.out.println("Enter a number");
int n=sc.nextInt();
int s;
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
import java.util.Scanner;
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
(b)
import java.util.Scanner;
import java.util.Scanner;
OUTPUT