COMP PROGRAMS 10TH FINALS
COMP PROGRAMS 10TH FINALS
pattern:
A
AB
ABC
ABCD
ABCDE
//program to generate a pattern
int i,j;
for(i=65;i<=69;i++)
for(j=65;j<=i;j++)
System.out.print((char)j);
System.out.println();
}
Output:
int i,j;
for(i=65;i<=69;i++)
for(j=65;j<=i;j++)
System.out.print((char)i);
System.out.println();
Output:
Variable name Data type Description
main() - -
i int To iterate through
range of rows
j int To iterate through
range of columns
import java.util.*;
char cht;
int i,j;
cht= in.next().charAt(0);
int n= (int)cht;
for(i=n+1;i<n+11;i++)
System.out.println((char)i);
}
Output:
import java.util.*;
int i,k=0,p=0,lb=0,ub=9,ns;
for(i=0;i<10;i++)
m[i]= in.nextInt();
ns= in.nextInt();
while(lb<=ub)
p=(ub+lb)/2;
if(m[p]<ns)
{
lb=p+1;
if(m[p]>ns)
ub=p-1;
if(m[p]==ns)
k=1;
break;
if(k==1)
else
}
Output:
import java.util.*;
int i,j,ld,rd,k;ld=0;rd=0;
for(i=0;i<4;i++)
for(j=0;j<4;j++) m[i]
[j]= in.nextInt();
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
System.out.print(m[i][j]+" ");
System.out.println();
for(i=0;i<4;i++)
ld= ld+m[i][i];
k=3;
for(i=0;i<4;i++)
rd= rd+m[i][k];
k=k-1;
}
Output:
import java.util.*;
int i,j,k;k=0;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
m[i][j]=in.nextInt();
}
for(i=0;i<3;i++)
for(j=0;j<3;j++)
System.out.print(m[i][j]+" ");
System.out.println();
for(i=0;i<3;i++)
for(j=0;j<3;j++)
if(m[i][j]!=m[j][i])
k=1;
if(k==0)
System.out.println("It is a Symmetric Matix");
else
Output:
import java.util.*;
int evenSum= 0;
int oddSum= 0;
for(int i=0;i<20;i++)
num[i]= in.nextInt();
if(num[i]%2==0)
evenSum+= num[i];
}
else
oddSum+= num[i];
Output-
Variable Name Data type Description
main() - -
evenSum int To store sum of all even
numbers
oddSum int To store sum of all odd
numbers
num int[] Variable of an Array of
size 20 to store the
numbers
i int Loop variable to iterate
through a range of 20
Q8) Write a program in Java to store 20 numbers in a Single
Dimensional Array (SDA). Display the numbers which are prime.
//program to display prime numbers
import java.util.*;
int i,j,c;
for(i=0;i<20;i++)
m[i]= in.nextInt();
for(i=0;i<20;i++)
c=0;
for(j=1;j<=m[i];j++)
if(m[i]%j==0)
c=c+1;
if(c==2)
System.out.print(" "+m[i]+",");
Output:
Variable Data type Description
main() - -
i int Loop variable to store
and check a number
j int Inner loop variable to
check if a number is
prime or not
c int Counting variable
m int[] Variable of an array to
store 20 numbers
import java.util.*;
int a,p;
int up=0,lc=0,d=0,spl=0;
String st;
char ch;
System.out.println("Enter string:");
st= in.nextLine();
p= st.length();
for(a=0;a<p;a++)
{
ch= st.charAt(a);
if(ch>='a'&&ch<='z')
lc= lc+1;
else if(ch>='A'&&ch<='Z')
up= up+1;
else if(ch>='0'&&ch<='9')
d= d+1;
else
spl= spl+1;
System.out.println("The Output:");
Output:
Variable name Data type Description
main() - -
a int Loop variable for
characters
p int To store length of
string
up int To store uppercase
characters
lc int To store lowercase
characters
d int To store digits
spl int To store special
characters
st String To accept string
ch char To store characters of
string
import java.util.*;
String st,sn,st1="",st2="";
int i,p;
char ch;
st= in.nextLine();
st=' '+st;
p= st.lastIndexOf(' ');
sn= st.substring(p);
for(i=0;i<p;i++)
ch= st.charAt(i);
if(ch==' ')
st1= st1+st.charAt(i+1)+'.';
}
st2= st1+sn;
System.out.println(st2);
Output:
import java.util.*;
class Unique_String
String str;
int i,j,p,k=1;
System.out.println("Enter a word:");
str= in.next();
str= str.toUpperCase();
p= str.length();
for(i=0;i<p;i++)
for(j=i+1;j<p;j++)
{
if(str.charAt(i)==str.charAt(j))
k=0;
if(k==1)
else
Output:
Variable name Data type Description
main() - -
str String To accept a word
from user
i int To iterate through
range of length of
string
j int Loop variable to
compare two
successive characters
p int To store length of
string
k int Flag variable to
detect a unique string
import java.util.*;
class Name_Telephone
String b = "";
m[i] = in.nextLine();
n[i] = in.nextLong();
in.nextLine();
if (m[i].equalsIgnoreCase(name))
k = 1;
a = i;
b = m[i];
break;
if (k == 1)
} else
}
Output:
Variable name Data type Description
main() - -
i int Loop variable to
accept 20 names and
telephone numbers
a int Copy variable for
printing the name
and number
k int Flag variable to
detect the name
entered
b String To interchange the
names in loop
m String[] Array to store 20
names
n Long[] Array to store 20
telephone numbers
import java.util.*;
int Pronic(int n)
int f=0;
for(int i=1;i<=(n-1);i++)
if(i*(i+1)==n)
f= 1;
break;
return (f);
}
public static void main(String args[])
int m= in.nextInt();
int a;
a= ob.Pronic(m);
if(a==1)
System.out.println("1");
else
System.out.println("0");
}
Output:
import java.util.*;
int base,ht;
double d1,d2;
int a,b,h;
base= in.nextInt();
ht= in.nextInt();
d1= in.nextDouble();
d2= in.nextDouble();
a= in.nextInt();
b= in.nextInt();
h= in.nextInt();
ob.calculate(base,ht);
ob.calculate(d1,d2);
ob.calculate(a,b,h);
}
Output:
import java.util.*;
class Salary
String name,add,sub;
long ph;
double sal,tax;
void accept()
name = in.nextLine();
System.out.println("Enter Address ");
add = in.next();
ph = in.nextLong();
sub = in.next();
sal = in.nextDouble();
void calculate()
if(12*sal>175000)
tax=((sal*12)-175000)*5/100;
else
tax=0;
void display()
System.out.println("Name : "+name);
System.out.println("Address : "+add);
ob.accept();
ob.calculate();
ob.display();
Output:
Variable name Data type Description
name String To accept name
add String To accept address
sub String To accept specialized
subject
ph long To accept phone
number
sal double To accept the monthly
salary
tax double To calculate and store
income tax
main() - -
Salary ob Object to call functions
import java.util.*;
String name;
int ut;
double amt=0.0,scg=0;
void accept()
{
System.out.println("Enter the Name of Consumer: ");
name = in.nextLine();
ut = in.nextInt();
void calculate()
if(ut<=100)
amt= ut*2.0;
if(ut>100&& ut<=300)
amt= 200+(ut-100)*3.0;
if(ut>300)
scg= ut*2.5/100.0;
amt= scg+(200+600+(ut-300)*5.0);
void print()
ob.accept();
ob.calculate();
ob.print();
Output:
Member methods:
1) A parameterized conductor to initialize the data
members
2) To accept the details (name of customer and price)
3) To compute the discount
4) To display the name, discount and amount to be paid
after discount.
Write a main method to create an object of the class and
call the member methods.
//Program to compute discounts on laptop
import java.util.*;
public class Laptop
String name;
double price;
name= n;
price= pr;
void accept()
System.out.println("Price: "+price);
void compute()
if(price<=25000)
dis= price*5.0/100.0;
if(price>=25001&&price<=50000)
dis= price*7.5/100.0;
if(price>=50001&&price<=100000)
dis= price*10.0/100.0;
if(price>100000)
dis= price*15.0/100.0;
void display()
amt= price-dis;
System.out.println("Customer name:"+name);
System.out.println("Discount on laptop:"+dis);
System.out.println("Amount:"+amt);
String nm;
double prc;
double disc,amo;
nm= in.nextLine();
prc= in.nextDouble();
Laptop ob= new Laptop(nm,prc);
ob.accept();
ob.compute();
ob.display();
Output :
Member methods:
1) A parameterized constructor to initialize the data
member
2) To accept the word
3) To arrange all the alphabets of word in ascending order
of their ASCII values without using the sorting
technique
4) To display the arranged alphabets.
Write a main method to create an object of the class and
call the above member methods.
//Program to arrange alphabets
import java.util.*;
String str;
int p;
Arrange(String word)
str = word;
p = str.length();
str = word;
p = str.length();
void arrangeAndDisplayAlphabets()
arr[i] = arr[j];
arr[j] = temp;
ob.arrangeAndDisplayAlphabets();
Output :
Variable name Data type Description
main() - -
str String Stores word entered
by user
p int Stores length of word
arr char[] Array of characters
i int Loop variable for
iterating through
character array
j int Loop variable for
iterating through
character array
temp char Temporary variable
used for swapping
characters
word String Accepts the word
input by user
ob Arrange Object of class
Arrange to call
functions
import java.util.*;
private int n;
void input()
n= in.nextInt();
void fact()
int f= 1;
for(int i=1;i<=n;i++)
f=f*i;
ob.input();
ob.fact();
Output:
import java.util.*;
void getdata()
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
m[i][j]= in.nextInt();
void rowsum()
for(int i=0;i<3;i++)
int r=0;
for(int j=0;j<3;j++)
r= r+m[i][j];
}
void colsum()
for(int i=0;i<3;i++)
int c= 0;
for(int j=0;j<3;j++)
c= c+m[j][i];
ob.getdata();
ob.rowsum();
ob.colsum();
}
Output: