Computer 1
Computer 1
. Number in words
. Factorial
. Fibonacci series
. Spiral matrix
. Linear search
. Binary search
. Selection sort
. Bubble sort
. Star pattern
. Palindrome number
. Word count
. Celsius to Fahrenheit
. Date program
. Magic square
.
PROGRAM: Number in words
import java.io.*;
class Num2words{
int amt=Integer.ParseInt(br.readLine());
int z,g;
String
x[]=(“Ten”,”Eleven”,”Thirteen”,”Fifteen”,”Sixteen”,”Seventeen”,”Eighteen”,”
Nineteen”);
String x1[ ]=
{“One”,”Two”,”Three”,”Four”,”Five”,”Six”,”Seven”,”Eight”,”Nine”};
Stringx2[ ]=
{“Twenty”,”Thirty”,”Fourty”,”Fifty”,”Sixty”,”Seventy”,”Eighty”,”Ninety”};
z=amt%10;
g=amt/10;
if(g!=1)
System.out.println(x2[g-1]+ “ ”+x1[z]);
else
System.out.println(x[amt-9]);
}}
. .
. .
.
PROGRAM: Calendar of month
import java.io.;
class Calendar{
public void dee()throws IOException{
int i,count=0,b,d=1;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter month”);
Int month= Integer.parseInt(br.readLine()); System.out.println(“Enter Year”);
int year=Integer.parseInt(br.readLine());
String w="SMTWTFS";
int days[]={31,28,31,30,31,30,31,31,30,31,30,31};
String month1[]=
{"January","February","March","April","May","June","July","August","Septem
ber","October","November","December"};
if((year%100==0 && year%400==0) || (year%100!=0 && year%4==0))
days[1]=29;
System.out.println(“The Calendar of"+month1[month- 1] +""+year+"is");
for(i=0;i<w.length();i++)
System.out.print(w.charAt(i)+"\t");
System.out.println(" ");
for(i=1;i<year;i++)
if((year%100==0 && year%400==0) || (year%100!=0 && year%4==0))
count+=2;
else
count+=1;
for(i=0;i<month;i++)
count+=days[i];
count+=1;
count%=7;
b=7-count;
if(b!=1 || b!=7)
while(count>0){
System.out.print(' '+"\t");
count--;
}for(i=1;i<7;i++){
while(b>0 && d<=days[month 1]){
System.out.print(d+"\t");
d++;
b--;
}
b=7;
System.out.println(" ”);
}}}
PROGRAM: Factorial
import java.io.*;
class Factorial{
public static void main(String args[]) throws IOException{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("enter no =");
int n = Integer.parseInt(br.readLine());
Factorial obj = new Factorial();
long f = obj.fact(n);
System.out.println("Factorial ="+f);
}
public long fact(int n){
if (n<2)
return 1;
else
return (n*fact(n-1));
}}
class SpiralMatrix{
a=new int[l][l];
r=l/2;c=r-1;
if(l%2==0){
System.exit(0);
while(p!=(int)Math.pow(l,2)){
if(co!=0)re=1;
for(int ri=1;ri<=k1-re;ri++){
p++;c++;
if(c==l)
break;
a[r][c]=p;
if(c==l)
break;
for(int dw=1;dw<=k1-1;dw++){
p++;r++;a[r][c]=p;
}for(int le=1;le<=k2-1;le++){
p++;c--;a[r][c]=p;
}
for(int up=1;up<=k2-1;up++){
k1=k1+2;
k2=k2+2;
co++;
for(int y=0;y<1;y++)
for(int yy=0;yy<1;yy++)
System.out.print(“\t”+a[y][yy]);
System.out.println();
System.out.println();
}}}
PROGRAM: Linear searching
Import java.io.*;
class LinearSearch{
int n,i;
int a[] = new int[100];
n=nn;
System.out.println("enter elements");
for(i=0;i<n;i++){
a[i] = Integer.parseInt(br.readLine());
}}
for(i=0;i<n;i++){
System.out.print(a[i]+" ");}}
if(flag== -1 )
System.out.println("not found");
else
obj.input();
obj.display();
int v=Integer.ParseInt(br.readLine());
Obj.search(v); }}
PROGRAM: Binary
searching
Import java.io.*;
class BinarySearch{
int n,i;
int a[] = new int[100];
static BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
public BinarySearch(int nn) {
n=nn;}
System.out.println("enter elements");
for(i=0;i<n;i++){
a[i] = Integer.parseInt(br.readLine());}}
public void display(){
System.out.println();
for(i=0;i<n;i++){
System.out.print(a[i]+" ");}}
public void search(int v){
m = (l+u)/2;
if(a[m] == v)
flag = m;
else u = m-1;}
if(flag== -1 )
System.out.println("not found");
else
obj.input();
obj.display();
int v = Integer.parseInt(br.readLine());
obj.search(v); }}
PROGRAM: Selection
sort
import java.io.*;
class SelectionSort{
n=nn;}
public void input() throws IOException {
for(i=0;i<n;i++){
a[i] = Integer.parseInt(br.readLine());}}
public void display(){
System.out.println();
for(i=0;i<n;i++){
System.out.print(a[i]+" ");}}
public void sort() {
int j,temp,min;
for(i=0;i<n-1;i++){
min =i;
for(j=i+1;j<n;j++){
if(a[j]<a[min])
min =j;}
if(min!=i){
temp = a[i];a[i] =a[min];a[min] = temp;}}}
public static void main(String args[]) throws IOException { SelectionSort x =
new SelectionSort(5);
x.input();
x.display();
System.out.print("After sorting - ");
x.sort();
x.display();}}
class BubbleSort{
n=nn;}
public void input() throws IOException{
a[i] = Integer.parseInt(br.readLine());}}
public void display(){
System.out.println();
for(i=0;i<n;i++){
System.out.print(a[i]+" ");}}
int j,temp;
for(i=0 ; i<n-1 ; i++){
x.display();
System.out.print("After sorting - ");
x.sort();
x.display();
}}
int n,i;
int a[] = new int[100];
static BufferedReader br =new BufferedReader(new
InputStreamReader(System.in));
n=nn;}
public void dectobin(int no) {
int no = Integer.parseInt(br.readLine());
obj.dectobin(no);
}}
class Pattern{
int i,sp,j,k,l;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the string =");
String s = br.readLine();
l=s.length();
for(i=0;i<l;i++)
if(i==l/2)
System.out.println(s);
else {
sp=Math.abs((l/2)-i);
for(j=sp;j<l/2;j++)
System.out.print(" ");
k=0;
while(k<3){
System.out.print(s.charAt(i));
for(j=0;j<sp-1;j++)
System.out.print(“ ”);
k++;
}
System.out.println(“ ”);
}}
PROGRAM: Palindrome no.
import java.io.*;
class Palindrome{
String s = br.readLine();
sb.reverse();
String rev = new String(sb);
if(s.equalsIgnoreCase(rev))
System.out.println("Palindrome " );
else
System.out.println("Not Palindrome ");
}}
PROGRAM: Frequency of string
import java.io.*;
class Frequency{
int i,a1,l,p,j,freq;
public Frequency(){
p=0;freq=0;}
public void count(String str) {
int ii;l=str.length();
System.out.print(str); for(i=0;i<l;i++){
char a=str.charAt(i);
for(ii=0;ii<l;ii++){
char b = str.charAt(ii);
if (a==b)
freq=freq+1;}
System.out.println(a+" occurs "+freq+” times”);
freq=0;}}
x.count(str);
}}
PROGRAM: Words search in string
Import java.util.StringTokenizer;
import java.io.*;
class WordSearch{
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("enter the string=");
String s = br.readLine();
StringTokenizer st = new StringTokenizer(s," ");
System.out.println("enter the word to be searched =");
String look = br.readLine();
int flag = -1;
while(st.hasMoreElements()) {
if(look.equals(st.nextElement()))
flag=1;
}if(flag==1){
System.out.println(“the word not found”);
}else{
System.out.println(“the word found”);
}}}
PROGRAM: String in
alphabetical order
import java.io.*;
class Alpha{
String str;int l;
char c[] = new char[100];
public Alpha(){
}
public void readword() throws IOException{
l = str.length();}
class Vowels{
String a= br.readLine();
int z=a.length(),y,x=0,b=0;for(y=0;y<z;y++){
if(a.charAt(y)=='a'||a.charAt(y)=='e'||a.charAt(y)=='i'||a.charAt(y)=='o'||
a.charAt(y)=='u')
x++;
else
b++;
}
System.out.println("Number of vowels in string ="+x);
System.out.println("Number of consonants in string=”+b);
}}
PROGRAM: Word count
import java.io.*;
class NoOfWords{
public static void main(String args[])throws IOException{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter Sentence");
String a=br.readLine();
System.out.println("The string is -"+a);
int z=a.length(),y,x=0;
for(y=0;y<z;y++){
if(a.charAt(y)==' ')
x=x+1;
}
System.out.println("Number of words in string=”+(x+1));
}}
}}
System.out.println("array”);
for(x=0;x<5;x++){
for(y=0;y<5;y++){
System.out.print(a[x][y]+””);
System.out.print("\n”);
for(x=0;x<5;x++){
for(y=0;y<5;y++){
Sum=Sum+a[x][y];
}}
}}
PROGRAM: Sum of matrix column
element
import java.io.*;
class ColoumnSum{
public static void main(String args[])throws IOException{
int a[][]=new int[4][4];
BufferedReader aa=new BufferedReader(new
InputStreamReader(System.in));
int x,y,z,Sum=0;
System.out.println("Enter the array");
for(x=0;x<4;x++){
for(y=0;y<4;y++){
z=Integer.parseInt(aa.readLine()); a[x][y]=z;
}}
System.out.println("Array”);
for(x=0;x<4;x++){
for(y=0;y<4;y++){
System.out.print(a[x][y]+””); }System.out.print("\n”);
}
for(y=0;y<4;y++){
for(x=0;x<4;x++){
Sum=Sum+a[x][y];
}
System.out.println("Sum of column”+(y+1)+”is”+Sum);
Sum=0; }}}
of matrix
import java.io.*;
class DiagonalSum{
public static void main(String args[])throws IOException{
int a[][]=new int[4][4];
BufferedReader aa=new BufferedReader(new
InputStreamReader(System.in));
int x,y,z,Sum=0;
System.out.println("Enter the array");
for(x=0;x<4;x++){ for(y=0;y<4;y++){
z=Integer.parseInt(aa.readLine());
a[x][y]=z;
}}
System.out.println("Array -");
}
System.out.print(“\n);
}
y=0;
for(x=0;x<4;x++){
Sum=Sum+a[x][y];
y=y+1;}
System.out.println("Sum of diagonal is”+Sum);
Sum=0;
}}
PROGRAM: Celsius to
fahrenheit
import java.io.*;
class C2F{
public static void main(String args[])throws IOException
{ Temperature ob= new Temperature();
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter temperature in Celsius");
double temp=ob.convert(Double.parseDouble(br.readLine()));
System.out.println(“The temperature in ahrenheit is = “+temp);
}}
class Temperature extends C2F{
double convert(double celcius) {
double far=1.8*celcius+32.0;
return far;
}}