0% found this document useful (0 votes)
4 views26 pages

Computer Programs12th

The document contains multiple Java programs addressing different problems, including clock conversion, equilibrium index, maximum sum of prime numbers in rows and columns, distinct prime numbers, palindrome checking, cipher encryption, balanced parentheses, and panagram validation. Each program includes methods for input handling, processing logic, and output display. The code snippets demonstrate various programming concepts and algorithms in Java.

Uploaded by

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

Computer Programs12th

The document contains multiple Java programs addressing different problems, including clock conversion, equilibrium index, maximum sum of prime numbers in rows and columns, distinct prime numbers, palindrome checking, cipher encryption, balanced parentheses, and panagram validation. Each program includes methods for input handling, processing logic, and output display. The code snippets demonstrate various programming concepts and algorithms in Java.

Uploaded by

pchauhan.181983
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Question 1- Clock conversion

import [Link].*;
public class clock
{
int h1,m1,h2;
String ap;
public void getdata()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the time in 12 hour format:");
h1=[Link]();
m1=[Link]();
[Link]();
ap=[Link]();
}
public void convert()
{
if(h1>12|| m1 > 59 ||(![Link]("AM") && ![Link]("PM")))
{
[Link]("Invalid input");
[Link](0);
}
if ([Link]("PM") && h1 != 12)
{
h2 = h1 + 12;
}
else if ([Link]("AM") && h1 == 12)
{
h2 = 0;
}
else
{
h2 = h1;
}
}
public void display()
{
[Link]("12 hour time:"+ h1+":"+m1+" "+ap);
[Link]("24 hour time:"+ h2+":"+m1);
}
public static void main()
{
clock c1=new clock();
[Link]();
[Link]();
[Link]();
}
}
Question 2 -Equilibrium index

import [Link].*;
public class eqindex
{
int a[],l;
public void accept()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter sie of array:");
l=[Link]();
a=new int[l];
if(l<3||l>50)
{
[Link]("INVALID INPUT");
[Link](0);
}
else
{
[Link]("Enter the elements of array:");
for(int i=0;i<l;i++)
{
a[i]=[Link]();
}
}
}
public void find()
{
int c=0;
[Link]("Equilibrium indices:");
for(int i=0;i<l;i++)
{
int rs=0,ls=0;
for(int j=0;j<i;j++)
{
ls=ls+a[j];
}
for(int j=i+1;j<l;j++)
{
rs=rs+a[j];
}
if(rs==ls)
{
[Link](" "+i);
c++;
}
}
if(c==0)
{
[Link]("NIL");
}
}
public static void main()
{
eqindex e=new eqindex();
[Link]();
[Link]();
}
}

Question 3-Row and column with max sum of prime numbers

import [Link].*;
public class pm
{
int a[][],m,n,pr,pc,index,ii;
public pm(int mm,int nn)
{
m=mm;
n=nn;
pr=0;
pc=0;
a=new int[m][n];
}
public void getdata()
{
Scanner sc=new Scanner([Link]);
if(m>2&&m<10&&n<10&&n>2)
{
[Link]("Enter the elements of the matrix:");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
a[i][j]=[Link]();
}
}
}
else
{
[Link]("Invalid input for row and column");
}
}
public int isPrime(int num)
{
if (num <= 1)
{
return 0;
}
for (int i=2; i<=num/2;i++)
{
if (num % i == 0)
{
return 0;
}
}
return 1;
}
public void cal()
{
for (int i = 0; i < m; i++)
{
int rowSum = 0;
for (int j = 0; j < n; j++)
{
int e=isPrime(a[i][j]);
if (e==1)
{
rowSum += a[i][j];
}
}
if (rowSum > pr)
{
pr = rowSum;
index = i;
}
}
for (int j = 0; j < n; j++)
{
int colSum = 0;
for (int i = 0; i < m; i++)
{
int e=isPrime(a[i][j]);
if (e==1)
{
colSum += a[i][j];
}
}
if (colSum > pc)
{
pc = colSum;
ii = j;
}
}
}
public void display()
{
[Link]("Matrix:");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
[Link](a[i][j]+" ");
}
[Link]();
}
[Link]("row with max sum:"+pr+"index:"+index);
[Link]("column with max sum:"+pc+"index:"+ii);
}
public static void main()
{
Scanner sc=new Scanner([Link]);
int row=[Link]();
int col=[Link]();
pm pp=new pm(row,col);
[Link]();
[Link]();
[Link]();
}
}
Question 4 - Distinct prime

import [Link].*;
public class distinct_prime
{
int m,n,a[],c;
public distinct_prime(int mm,int nn)
{
m=mm;
n=nn;
c=0;
a=new int[n-m+1];
}
public int prime(int num)
{
int r=0;
while(num!=0)
{
int l=num%10;
if(l==2||l==3||l==5||l==7)
{
r=1;
}
else
{
r=0;
break;
}
num=num/10;
}
return r;
}
public int repeat(int num)
{
int count=0;
int t=num;
while (t>0)
{
count++;
t=t/10;
}
int d[]=new int[count];
for (int i=count-1;i>=0;i--)
{
d[i]=num%10;
num=num/10;
}
for(int i=0;i<count;i++)
{
int k=d[i];
for(int j=i+1;j<count;j++)
{
if(k==d[j])
{
return 0;
}
}
}
return 1;
}
public void check()
{
if(m<n)
{
for(int i=m;i<=n;i++)
{
int k=prime(i);
int b=repeat(i);
if(k==1&&b==1)
{
a[c]=i;
c++;
}
}
}
}
public void display()
{
[Link]("THE DISTINCT PRIME INTEGERS ARE: ");
for(int i=0;i<c;i++)
{
[Link](a[i]+" ");
}
[Link]("FREQUENCY OF DISTINCT PRIME INTEGERS: "+c);
}
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the value of m and n:");
int p=[Link]();
int q=[Link]();
distinct_prime p1=new distinct_prime(p,q);
[Link]();
[Link]();
}
}
Question 5 - Palindrome

import [Link].*;
public class palindrome
{
String s;
int sp=0;
public void accept()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter a sentence:");
s=[Link]();
}
public void check()
{
String ss="",rev="";
String w=[Link]();
int l=[Link]();
char last=[Link](l-1);
if (last != '.' && last != '?' && last != '!')
{
[Link]("INVALID INPUT");
[Link](0);
}
for(int i=0;i<l;i++)
{
char c=[Link](i);
if(c>='a'&&c<='z')
{
ss=ss+c;
}
if(c==' ')
{
sp++;
}
}
for (int i=[Link]()-1;i>=0;i--)
{
rev=rev+[Link](i);
}
if([Link](rev))
{
[Link]("Palindrome");
}
else
{
[Link]("Not");
}
}
public void freq()
{
String word="",mfw="";
int size=0,f=1;
String a[]=new String[sp+1];
for(int i=0;i<[Link]();i++)
{
char c=[Link](i);
if(c!=' '&&c!='.'&&c!='?'&&c!='!')
{
word=word+c;
}
else
{
a[size]=[Link]();
size++;
word="";
}
}
for(int i=0;i<size;i++)
{
int count=1;
for(int j=i+1;j<size;j++)
{
if(a[i].equals(a[j]))
{
count++;
a[j]="";
}
}
if(count>f)
{
mfw=a[i];
f=count;
}
}
[Link]("MOST FREQUENt WORD:"+ mfw);
if(f==1)
{
[Link]("MOST FREQUENt WORD: NONE");
}
}
public static void main()
{
palindrome p=new palindrome();
[Link]();
[Link]();
[Link]();
}
}
Question 6 - Cipher

import [Link].*;
public class cipher {
int a[], b[], c[], aa, bb;
public cipher(int m, int n)
{
aa = m;
bb = n;
a = new int[aa + 1];
b = new int[bb + 1];
c = new int[aa + 1];
}
public void getdata()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter number:");
for (int i = 1; i <= aa; i++)
{
a[i] = [Link]();
}
[Link]("Key:");
for (int i = 1; i <= bb; i++)
{
b[i] = [Link]();
}
}
public void encrypt()
{
if (aa != bb)
{
[Link]("INVALID KEY SIZE");
[Link](0);
}
for (int i = 1; i <= bb; i++)
{
int k = b[i];
if (k < 1 || k > aa)
{
[Link]("INVALID KEY DIGITS");
[Link](0);
}
for (int j = 1; j < i; j++)
{
if (b[j] == k)
{
[Link]("INVALID KEY DIGITS");
[Link](0);
}
}
}
for (int i = 1; i <= aa; i++)
{
int k = b[i];
c[k] = a[i];
}
}
public void display()
{
[Link]("Encrypted number:");
for (int i = 1; i <= aa; i++)
{
[Link](c[i]);
}
[Link]();
}
public static void main()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter size of number and key:");
int p = [Link]();
int q = [Link]();
cipher c1 = new cipher(p, q);
[Link]();
[Link]();
[Link]();
}
}
Question 7 - Balanced and depth

import [Link].*;
public class Balanced
{
public static void main()
{
Scanner sc = new Scanner([Link]);
String s = [Link]();
int len=[Link]();
char l=[Link](len- 1);
if (l!='.' && l!= '?' && l != '!')
{
[Link]("INVALID INPUT");
[Link](0);
}
for (int i=0;i<len-1;i++)
{
char ch = [Link](i);
if (![Link](ch) && ch !=' '&&
ch!='{'&&ch!='}'&&ch!='['&&ch!=']'&&ch!='('&&ch!=')')
{
[Link]("INVALID INPUT");
[Link](0);
}
}
int round=0,square=0,curly=0;
int md=0,cd=0;
int firstBracket=0;
for(int i=0;i<len;i++)
{
char ch=[Link](i);
int index = i+1;
if (ch == '(')
{
round++;
cd++;
if (firstBracket == 0)
{
firstBracket = index;
}
}
else if (ch == ')')
{
round--;
cd--;
if (round < 0)
{
[Link]("BALANCED: NO");
[Link]("ERROR AT INDEX: " + index);
return;
}
}
else if (ch == '[')
{
square++;
cd++;
if (firstBracket == 0)
{
firstBracket = index;
}
}
else if (ch == ']')
{
square--;
cd--;
if (square < 0)
{
[Link]("BALANCED: NO");
[Link]("ERROR AT INDEX: " + index);
return;
}
}
else if (ch == '{')
{
curly++;
cd++;
if (firstBracket == 0)
{
firstBracket = index;
}
}
else if (ch == '}')
{
curly--;
cd--;
if (curly < 0)
{
[Link]("BALANCED: NO");
[Link]("ERROR AT INDEX: " + index);
return;
}
}
if (cd > md)
{
md = cd;
}
}
if (round == 0 && square == 0 && curly == 0)
{
[Link]("BALANCED: YES");
[Link]("MAX DEPTH: " + md);
[Link]("INDEX: " + firstBracket);
}
else
{
[Link]("BALANCED: NO");
[Link]("ERROR AT INDEX: " + firstBracket);
}
}
}
Question 8 - panagram

import [Link].*;
public class Panagram
{
public static void main()
{
String s="";
String str="";
Scanner sc=new Scanner([Link]);
[Link]("Enter the Sentence");
s=[Link]().toLowerCase();
int l=[Link]();
int c=[Link](l-1);
int m=0;
char ch;
if(c=='.'||c=='?'||c=='!')
{
[Link]("VALID INPUT");
}
else
{
[Link]("INVALID INPUT");
[Link](0);
}
[Link]("Missing letters=");
for(ch='a';ch<='z';ch++)
{
if([Link](ch)==-1)
{
[Link](ch+" ");
m++;
str+=ch+"";
}
}
if(m==0)
{
[Link](0);
}
if(m==0)
{
[Link]("The entered string is Panagram");
}
else if(m==1)
{
[Link]("The entered string is PanagramLiporam");
[Link]("Missing="+m);
}
else
{
[Link]("NEITHER");
[Link]("Missing="+m);
}
}
}

You might also like