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

Computer project - Copy

The document contains a series of Java programming exercises, each requiring the implementation of specific functionalities such as checking for Keprekar numbers, converting decimal to binary, checking for anagrams, and calculating sums and averages of arrays. Additional tasks include finding the smallest and largest elements in an array, counting zeros in a matrix, sorting names with mobile user percentages, and printing various patterns. Each question is accompanied by a code snippet demonstrating the required solution.

Uploaded by

jaswanimanyata
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)
13 views

Computer project - Copy

The document contains a series of Java programming exercises, each requiring the implementation of specific functionalities such as checking for Keprekar numbers, converting decimal to binary, checking for anagrams, and calculating sums and averages of arrays. Additional tasks include finding the smallest and largest elements in an array, counting zeros in a matrix, sorting names with mobile user percentages, and printing various patterns. Each question is accompanied by a code snippet demonstrating the required solution.

Uploaded by

jaswanimanyata
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/ 19

Question 1

Write a program to check whether a number is a keprekar number or


not.
Class keprekarno.
Data members
Int num;
Member functions
keprekarno (int n)
void display ()
boolean check ()
int countD (int n)
import java. util. Scanner;
class keprekarno
{
int num;
keprekarno (int n)
{
num=n;
}

void display ()
{
System.out.println("number “+num);
boolean f= check(num);
if(f==true)
System.out.println("Keprekar “+num);
else
System.out.println("not");
}

int countD (int n)


{
int c=0, d=0, cp=n;
while (cp! =0)
{
d=cp%10;
c++;
cp/=10;
}
return c;
}

boolean check (int n)


{
int f=0, s=0;
int c= countD(n*n);
int p=(int)Math.pow (10, c/2);
if(c%2==1)
p*=10;
f=(n*n)/p;
s=(n*n) %p;
return (s+f) ==n;
}
public static void main (String args [])
{
Scanner sc=new Scanner (System.in);
System.out.println("enter the number");
int n=sc. nextInt ();
keprekarno obj=new keprekarno(n);
obj. display ();
}
}
Question 2
Write a program to input a number and convert it into binary number.
import java. util. Scanner;
class abc
{
public static void main (String args [])
{
int n=0; String s="";
int cp=0;
Scanner sc=new Scanner (System.in);
System.out.println("enter the number");
cp=n=sc. nextInt ();
while (n! =0)
{
int r=cp%2;
s=r+s;
cp=cp/2;
}
System.out.println("Decimal"+n);
System.out.println("Binary"+s);
}
}
Question 3
Write a program to enter a string and check whether it is an anagram or
not.
import java. util. Scanner;
class Anagram
{
boolean isAnagram (String f, String s)
{
if (f. length ()! =s. length ())
return false;
else
for (int i=0; i<f. length (); i++)
{
char ch=f. charAt(i);
int p=s. indexOf(ch);
if(p==-1)
return false;
s=s. substring (0, p) +s. substring(p+1);
}
return s. length () ==0;
}
public static void main (String args [])
{
String s="“, f="";
Scanner sc=new Scanner (System.in);
System.out.println("enter the strings");
s=sc. nextLine ();
f=sc. nextLine ();
Anagram obj=new Anagram ();
boolean fr=obj. isAnagram (f, s);
if(fr==true)
System.out.println("yes");
else
System.out.println("no");
}
}
Question 4
Write a program to calculate the sum of an array elements and average.
import java. util. Scanner;
class abc
{
public static void main (String args [])
{
int A [], s=0;
double avg=0.0;
Scanner sc=new Scanner (System.in);
System.out.println("enter the size");
s=sc. nextInt ();
A=new int[s];
System.out.println("Fill array");
for (int i=0; i<s; i++)
A[i]=sc. nextInt ();
for (int i=0; i<s; i++)
{
avg=avg+A[i];
}
System.out.println("Sum "+avg);
avg=avg/s;
System.out.println("Average"+avg);
}
}
Question 5
Write a program to find the smallest and largest element of the array.
import java. util. Scanner;
class abc
{
public static void main (String args [])
{
int A [], s=0, l=0, size;
Scanner sc=new Scanner (System.in);
System.out.println("enter the size");
size=sc. nextInt ();
A=new int[size];
System.out.println("Fill array");
for (int i=0; i<size; i++)
A[i]=sc. nextInt ();
s=A [0];
l=A [0];
for (int i=0; i<size; i++)
{
if(A[0]>A[i])
s=A[i];
if (A [0] <A[i])
l=A[i];
}
System.out.println("Smallest"+s);
System.out.println("Largest"+l);
}}
Question 6
Write a program to find the frequency of zeros in each row of a 5 x 5
matrix.
import java. util. Scanner;
class abc
{
public static void main (String args [])
{
int f=0, a [] [];
Scanner sc=new Scanner (System.in);
a=new int [5][5];
System.out.println("Fill the array");
for (int i=0; i<5; i++)
{
for (int j=0; j<5; j++)
a[i][j] =sc. nextInt ();
}
for (int i=0; i<5; i++)
{
f=0;
for (int j=0; j<5; j++)
{
if(a[i][j] ==0)
f++;
}
System.out.println("frequency"+f);
}
}
}
Question 7
Write a program to input a string and print the longest word of the
string.
import java. util. Scanner;
class abc
{
public static void main (String args [])
{
String str="“, w="“, large="";
Scanner sc=new Scanner (System.in);
System.out.println("enter the string");
str= sc. nextLine () +" ";
for (int i=0; i<str. length (); i++)
{
char ch=str. charAt(i);
if (ch! =' ')
{
w+=ch;
}
else
{
if (w. length ()>large. length ())
large=w;
w="";
}
}
System.out.println("largest word"+large);
}
}
Question 8
Write a program to enter the names of the places with percentage of
mobile users and sort them in descending order using bubble sort
technique.
import java. util. Scanner;
class abc
{
public static void main (String args [])
{
String name [], t1="";
double mobile [], t=0.0;
int s;
Scanner sc=new Scanner (System.in);
System.out.println("enter the size");
s=sc. nextInt ();
name=new String[s];
mobile=new double[s];
System.out.println("enter the name of the country");
for (int i=0; i<s; i++)
{
name[i]=sc. nextLine ();
}
System.out.println("enter the percentage of mobile");
for (int i=0; i<s; i++)
{
mobile[i]=sc. nextDouble ();
}
for (int i=0; i<s; i++)
{
for (int j=0; j<s-i-1; j++)
{
if(mobile[j]<mobile[j+1])
{
t=mobile[j];
mobile[j]=mobile[j+1];
mobile[j+1] =t;
t1=name[j];
name[j]=name[j+1];
name[j+1] =t1;
}
}
}
for (int i=0; i<s; i++)
System.out.println(name[i]+"\t"+mobile[i]);
}
}
Question 9
Write a program to print the following pattern
54321
5432
543
54
5
import java. util. Scanner;
class abc
{
public static void main (String args [])
{
int i, j, n;
Scanner sc=new Scanner (System.in);
System.out.println("enter the number of terms");
n=sc. nextInt ();
for (i=1; i<=n; i++)
{
for (j=n; j>=i; j--)
{
System.out.print(j+" ");
}
System.out.println();
}
}
}
Question 10
Write a program to print the following program.
9
87
654
3210
import java. util. Scanner;
class abc
{
public static void main (String args [])
{
int i, j, n, k=9;
Scanner sc=new Scanner (System.in);
System.out.println("enter the number of terms");
n=sc. nextInt ();
for (i=1; i<=n; i++)
{
for (j=1; j<=i; j++)
{
System.out.print(k+" ");
--k;
}
System.out.println();
}
}
}
Question 11
Write a program to print the following pattern.
*
* *
* * *
* * * *
import java. util. Scanner;
class abc
{
public static void main (String args [])
{
int i, j, n, k;
Scanner sc=new Scanner (System.in);
System.out.println("enter the number of terms");
n=sc. nextInt ();
for (i=1; i<=n; i++)
{
for (j=n; j>i; j--)
{
System.out.print(" ");
}
for (k=1; k<=i; k++)
{
System.out.print("*"+" ");
}
System.out.println();
}
}
}
Question 12
Write a program to accept a sentence and convert it into uppercase and
count and display the total number of words starting with a letter 'A’.
import java.util.*;
class abc
{
public static void main (String args [])
{
Scanner sc= new Scanner (System.in);
int countWord = 0;
System.out.print("Input a sentence");
String sent = sc. nextLine ();
String capSent = sent. toUpperCase ();
capSent=" "+ capSent;
for (int j=0; j<capSent.length(); j++)
{
char ch= capSent.charAt(j);
if (ch ==' ')
{
if (capSent.charAt(j + 1) == 'A’)
countWord++;
}
}
System.out.println("Output:\n" + capSent);
System.out.println("Total number of words starting with letter
'A'="+countWord);
}
}
Question 13
Write a program to accept a word and convert into lowercase if it is in
uppercase, and display the new word by replacing only the VOWELS
with the character following it.
import java.util.*;
class abc
{
public static void main (String args [])
{
String str, new Word = "";
int len, j; char ch;
Scanner sc= new Scanner (System.in);
System.out.print("Input a word = ");
str=sc. next ();
len=str. length ();
for (j=0; j<len; j++)
{
ch=str. charAt(j);
if (Character.isUpperCase(ch))
ch=Character.toLowerCase(ch);
if (ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')
{
ch++;
}
newWord =newWord + ch;
}
System.out.println("Output:\nThe new word="+newWord);
}
}
Question 14
Write a program to input 'n' and declare an array of size n, accept
integers into the array and perform the following:
 Find and print the product of single digit integers of the array.
 Find and print the sum of double digits integers of the array.
import java.util.*;
class abc
{
public static void main (String args [])
{
int prod=1, sum = 0;
Scanner sc = new Scanner (System.in);
System.out.print("Enter the size of the array = “);
int n = sc. nextInt ();
int arr [] = new int[n];
System.out.println("Input "+ n +" numbers one by one");
for (int i = 0; i<n; i++)
{
System.out.print("Input a number = ");
arr[i] = sc. nextInt ();
}
for (int j = 0; j <= n - 1; j++)
{
if(arr[j]>0 && arr[j]<=9) {
prod = prod* arr[j];}
else if(arr[j]>=10 && arr[j]<=99)
{
sum = sum + arr[j];
}
}
System.out.println("Product of single digit numbers = "+ prod);
System.out.println("Sum of double-digit numbers = "+sum);
}
}
Question 15
Define a class to declare an array of size ten (10), accept the marks
obtained by 10 students in the array Calculate and print the highest
marks and lowest marks along with average marks.

import java.util.*;
class abc
{
public static void main (String args [])
{
Scanner sc = new Scanner (System.in);
int M [] = new int [ 10];
int high, low, total_ marks = 0;
for (int j = 0; j < 10; j ++)
{
System.out.print("Enter marks for M ["+j+”] = “);
M[j] = sc. nextInt ();
}
high = M [0];
low = M [0];
for (int j=0; j < 10; j ++)
{
if(M[j] >high)
high=M[j];
else if(M[j]<low)
low=M[j];
total_marks = total_marks + M[j];
}
double avg = total_marks/10.0;
System.out.println("Output:\nHighest marks = "+high);
System.out.println("Lowest marks = " + low);
System.out.println("Average marks = " + avg);
}
}

You might also like