0% found this document useful (0 votes)
23 views9 pages

Java Cia 2 16 Marks

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

Java Cia 2 16 Marks

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

1)

import java.util.*;
class Main{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int m=sc.nextInt();
int n=sc.nextInt();
int a[][]=new int[m][n];
int i,j,s=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=sc.nextInt();
if(i==j)
s+=a[i][j];
}
}
System.out.println(s);
}
}

2)
import java.util.*;
class Main{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a[]=new int[n];
int sum=0;
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
sum+=a[i];
}
System.out.println(sum);
}
}

3)
import java.util.*;
class Main{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(a[i]==a[j])
{
System.out.println("The first repeating element is "+a[i]);
return;
}
}
}
System.out.println("There are no repeating elements");
}
}

4)
import java.util.*;
class Main{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
for(int i=0;i<n;i++)
{
int s=0;
for(int j=i+1;j<n;j++)
{
s+=a[j];
}
System.out.print(s+" ");
}
}
}

5)
import java.util.*;
class Main{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt(),i,c=0;
int a[]=new int[n];
for(i=0;i<n;i++)
a[i]=sc.nextInt();
int r=sc.nextInt();
int e=r*2;
int t=0;
for(i=0;i<n;i++)
{
t+=a[i];
if(t>=e)
{
System.out.println("The number of bowls required to feed the rat =
"+(i+1));
c++;
break;
}
}
if(c==0)
System.out.println("The biscuits in all bowls are not enough to feed the
rats");
}
}
6)
import java.util.*;
class TEAM
{
public static void main(String []args)
{
Scanner s=new Scanner(System.in);
String x=s.nextLine();
int len=x.length();
for(int i=0;i<len;i++)
{
if(i<=len-2)
{
if(x.charAt(i)==x.charAt(i+1))
System.out.print(x.charAt(i)+"*");
else
System.out.print(x.charAt(i));
}
else
System.out.print(x.charAt(i));
}
}
}

7)
import java.util.*;
class Main{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String a=sc.next();
String b=new StringBuilder(a).reverse().toString();
if(a.equals(b))
System.out.println(a+" :"+"palindrome");
else
System.out.println(a+" :"+"not a palindrome");
}
}

8)
import java.util.*;
class main
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
String s=in.nextLine();
String a[]=s.split(" ");
int n=a.length;
for(int i=n-1;i>=0;i--)
{
System.out.print(a[i]+" ");
}
}
}

9)
import java.util.*;
class main
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
String s=in.nextLine();
int n=s.length();
boolean l=false,u=false,lo=false,d=false,spl=false;
for(int i=0;i<n;i++)
{
char z=s.charAt(i);
if(Character.isDigit(z))
d=true;
if(Character.isUpperCase(z))
u=true;
if(Character.isLowerCase(z))
lo=true;
if(n>8)
l=true;
if(!Character.isDigit(z)
&& !Character.isLetter(z)
&& !Character.isWhitespace(z) )
spl=true;
}
if(l&&u&&lo&&d&&spl)
System.out.print("Valid password");
else
System.out.print("Invalid password");
}
}

10)
import java.util.*;
class main
{
public static void main(String args[])
{
Scanner in = new Scanner (System.in);
int freq[]=new int[150];
String s = in.nextLine();
for(int i=0;i<s.length();i++)
freq[(int)s.charAt(i)]++;
for(int i=96;i<123;i++)
{
if(freq[i]>=1)
System.out.println((char)i+"-"+freq[i]);
}
}
}

11)
import java.io.*;
import java.util.*;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
class CreateFile
{
public static void main(String[] args) throws IOException
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a[]=new int[n];
FileWriter f=new FileWriter("output.txt");
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
f.write(a[i]);
}
f.close();
FileReader r=null;
try
{
r=new FileReader("output.txt");
}
catch(FileNotFoundException e)
{
System.out.println("File is not found");
}
int k,max=r.read(),min=r.read();
while((k=r.read())!=-1)
{
if(k>=max)
max=k;
if(k<=min)
min=k;
}
System.out.println(min+max);
}
}

12)
import java.io.*;
import java.util.*;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
class CreateFile
{
public static void main(String[] args) throws IOException
{
FileWriter fr=new FileWriter("s.txt");
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
fr.write(s);
fr.close();
FileReader f=new FileReader("s.txt");
int c;
String s1="";
int m=0;
while((c=f.read())!=-1)
{
if(c==' '||c=='.'||c==',')
{
if(s1.length()>m)
m=s1.length();
s1="";
}
else
s1+=(char)c;
}
System.out.print(s1);
}
}

13)
import java.util.*;
import java.io.*;
class Main
{
public static void main(String args[])throws IOException
{
Scanner ac = new Scanner(System.in);
String s = ac.nextLine();
FileWriter f = new FileWriter("s.txt");
f.write(s);
f.close();
FileReader r = new FileReader("s.txt");
int c;
while((c=r.read())!=-1)
{
String[] ss = s.split(" ");
for(String wd : ss)
{
if(!wd.matches(".*[aeiouAEIOU].*"))
{
System.out.println(wd);
}
}
break;
}
}
}

14)
import java.util.*;
class Main{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int y=sc.nextInt();
int m=sc.nextInt();
int d=sc.nextInt();
TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
Calendar cal=Calendar.getInstance();
cal.clear();
System.out.println();
cal.set(Calendar.YEAR,y);
cal.set(Calendar.MONTH,m);
cal.set(Calendar.DATE,d);
System.out.println(cal.getTime());
System.out.println();
}
}

15)
import java.util.*;
import java.time.*;
class Main
{
public static void main(String args[])
{
Scanner ac = new Scanner(System.in);
String a = ac.nextLine();
String b = ac.nextLine();
LocalDate d = LocalDate.parse(a);
LocalDate dd = LocalDate.parse(b);
LocalDate cur = d;
cur=cur.plusDays(1);
int c=0;
while(!cur.isAfter(dd))
{
c++;
cur=cur.plusDays(1);
}
System.out.println("Number of days between "+a+" and "+b+" is: "+c);
}
}

16)
Refer 14)th question

17)
import java.util.*;
import java.time.*;
class Main{
public static void main(String []args)
{
Scanner in=new Scanner(System.in);
String a=in.next();
LocalDate d=LocalDate.parse(a);
int c=((d.getDayOfYear()-1)/7+1);
String p=String.format("%02d",c);
System.out.println(d.getDayOfWeek()+", Week"+p+", "+d.getMonth()+"
"+d.getDayOfMonth()+", "+d.getYear());
}
}

18)
import java.util.*;
import java.time.*;
import java.text.*;
class Main
{
public static void main(String []args)
{
Scanner s=new Scanner (System.in);
int y=s.nextInt();
int m=s.nextInt();
YearMonth d=YearMonth.of(y,m);
System.out.println(d.lengthOfMonth()+" days in "+d.getMonth()+" "+y);
}
}

19)
import java.util.*;
class Main{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
StringTokenizer r=new StringTokenizer(s, " ?,!");
String t=new StringBuilder(" ").toString();
while(r.hasMoreTokens())
t=r.nextToken()+" "+t;
System.out.println("Original string: "+s);
System.out.println("Reversed string: "+t);
}
}

20)
import java.util.*;
import java.util.StringTokenizer;
class Main{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
StringTokenizer r=new StringTokenizer(s);
String str=new StringBuilder(s).toString();
s.trim();
System.out.print("Uppercase sentence: ");
while(r.hasMoreTokens())
System.out.print(r.nextToken().toUpperCase()+" ");
}
}

21)
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String str = in.nextLine();
StringTokenizer s = new StringTokenizer(str," ");
HashSet<String> set = new HashSet<>();
while(s.hasMoreElements())
set.add(s.nextToken());
System.out.println("Number of unique elements in the string: "+set.size());
}
}

22)
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String str = in.nextLine();
int count=0;
StringTokenizer s = new StringTokenizer(str," ");
while(s.hasMoreElements())
{
String sb = s.nextToken().toLowerCase();
StringBuilder rev = new StringBuilder(sb).reverse();
if(rev.toString().equals(sb))
continue;
else
count++;

}
if(count>0)
System.out.println("The sentence is not a palindrome.");
else
System.out.println("The sentence is a palindrome!");
}
}

You might also like