0% found this document useful (0 votes)
10 views36 pages

Computer 1

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)
10 views36 pages

Computer 1

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/ 36

INDEX

. Number in words

. Calendar of any month

. Factorial

. Fibonacci series

. Spiral matrix

. Linear search

. Binary search

. Selection sort

. Bubble sort

. Decimal to Binary no.

. Star pattern

. Palindrome number

. Frequency of each string character

. Word search in string

. String in alphabetical order

. No. of vowels and consonants

. Word count

. Replacing vowels with *

. Sum of all matrix elements

. Sum of matrix column elements

. Sum of matrix diagonal elements

. Celsius to Fahrenheit

. Decimal to roman numerical

. Date program

. Magic square
.
PROGRAM: Number in words
import java.io.*;

class Num2words{

public static void main(String args[ ]throws IOException){

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.println(“enter any no.(less than 99)”);

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));
}}

PROGRAM: Fibonacci series


import java.io.*;
class Fibonacci{
public static void main(String args[]) throws IOException {
Fibonacci obj = new Fibonacci();
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("enter no of term =");
int n = Integer.parseInt(br.readLine());
System.out.println();
for(int i=1;i<=n;i++) {
int f = obj.fib(i);
System.out.print(f+" ");}}
public int fib(int n) {
if(n<=1)
return n;
else
return (fib(n-1) +fib(n-2));
}}

PROGRAM: Spiral matrix


import java.io.;

class SpiralMatrix{

public static void main(String[] args) throws IOException ){


int a[][],r,c,k1=2,k2=3,p=0,co=0,re=0;

BufferedReader br = new BufferedReader(new


InputStreamReader(System.in));

System.out.println("enter the dimension of matrix A x A =");


int l = Integer.parseInt(br.readLine());

a=new int[l][l];

r=l/2;c=r-1;

if(l%2==0){

System.out.println("wrong entry for spiral path");

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];

static BufferedReader br =new BufferedReader(new


InputStreamReader(System.in));

public LinearSearch(int nn){

n=nn;

public void input() throws IOException{

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){


int flag=-1;

for(int i=0; i<n ; i++){


if(a[i] == v)
flag =i;}

if(flag== -1 )

System.out.println("not found");

else

System.out.println(v+" found at position - "+flag);}

public static void main(String args[]) throws IOException{

LinearSearch obj = new LinearSearch(10);

obj.input();

obj.display();

System.out.println("enter no. to be searched”);

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;}

public void input() throws IOException{

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){

int l=0;int u = n-1;int m;int flag=-1;


while( l<=u && flag == -1){

m = (l+u)/2;

if(a[m] == v)
flag = m;

else if(a[m] < v)


l = m+1;

else u = m-1;}
if(flag== -1 )

System.out.println("not found");

else

System.out.println(v+" found at position - "+flag);}

public static void main(String args[]) throws IOException{

BinarySearch obj = new BinarySearch(10);

obj.input();

obj.display();

System.out.println("enter no. to be searched -");

int v = Integer.parseInt(br.readLine());

obj.search(v); }}
PROGRAM: Selection
sort
import java.io.*;

class SelectionSort{

int n,i;int a[] = new int[100];


public SelectionSort(int nn){

n=nn;}
public void input() throws IOException {

BufferedReader br =new BufferedReader(new InputStreamReader(System.in));


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 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();

System.out.print("Before sorting - ");

x.display();
System.out.print("After sorting - ");
x.sort();
x.display();}}

PROGRAM: Bubble sort


import java.io.*;

class BubbleSort{

int n,i;int a[] = new int[100];


public BubbleSort(int nn){

n=nn;}
public void input() throws IOException{

BufferedReader br =new BufferedReader(new InputStreamReader(System.in));


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 sort(){

int j,temp;
for(i=0 ; i<n-1 ; i++){

for(j=0 ; j<n-1-i ; j++){

if(a[j] > a[j+1]){


temp = a[j];
a[j] =a[j+1];
a[j+1] = temp;}}}}

public static void main(String args[]) throws IOException{

BubbleSort x = new BubbleSort(5);


x.input();

System.out.print("Before sorting - ");

x.display();
System.out.print("After sorting - ");
x.sort();

x.display();

}}

PROGRAM: Decimal into binary


Import java.io.*;
class Dec2Bin{

int n,i;
int a[] = new int[100];
static BufferedReader br =new BufferedReader(new
InputStreamReader(System.in));

public Dec2Bin(int nn){

n=nn;}
public void dectobin(int no) {

int c = 0;int temp = no;


while(temp != 0){
a[c++] = temp % 2;
temp = temp / 2;}

System.out.println("Binary eq. of "+no+" = ");

for( i = c-1 ; i>=0 ; i--)


System.out.print( a[ i ] ); }
public static void main(String args[])throws IOException{

Dec2Bin obj = new Dec2Bin(30);


System.out.println("enter decimal no.”);

int no = Integer.parseInt(br.readLine());

obj.dectobin(no);

}}

PROGRAM: Star pattern


import java.io.;

class Pattern{

public static void main (String args[]) throws IOException {

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{

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();

StringBuffer sb = new StringBuffer(s);

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;}}

public static void main(String args[]) throws IOException{

BufferedReader br =new BufferedReader(new InputStreamReader(System.in));


System.out.println("enter string");

String str = br.readLine();

Frequency x = new Frequency();

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(){

str = "";l =0;

}
public void readword() throws IOException{

System.out.println("enter word - ");

BufferedReader br =new BufferedReader(new InputStreamReader(System.in));


str = br.readLine();

l = str.length();}

public void arrange() {

int i,j;char temp;


for(i=0;i<l;i++){
c[i]= str.charAt(i);}
for(i=0;i<l-1;i++){
for(j=0;j<li;j++){
if(c[j]>c[j+1]){
temp = c[j];c[j] = c[j+1];c[j+1] = temp; }}}}
public void display(){
System.out.println();
for(int i=0;i<l;i++){
System.out.print(c[i]);}}
public static void main(String args[]) throwsIOException{ Alpha obj = new Alpha();
obj.readword();
obj.arrange();
obj.display(); }}.

PROGRAM: No. of vowels &


consonants
import java.io.*;

class Vowels{

public static void main(String args[])throws IOException{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));


System.out.println("Enter a string");

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));
}}

PROGRAM: Replace vowels


with*
import java.io.*;
class VowelReplace{
public static void main(String args[])throws IOException{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println(“Enter a String”);
StringBuffer a=new StringBuffer(br.readLine());
System.out.println("Original String -"+a);
int z=0;
for(z=0;z<a.length();z++){
if(a.charAt(z)=='a'||a.charAt(z)=='e'||a.charAt(z)=='i'||
a.charAt(z)=='o'||a.charAt(z)=='u')
a.setCharAt(z,'');}
System.out.println("New String -"+a);
}}

PROGRAM: Sum all elements


matrix
Import java.io.*;
class MatrixSum{
public static void main(String args[])throws IOException { int a[]
[]=new int[5][5];
BufferedReader aa=new BufferedReader(new
InputStreamReader(System.in));
int x,y,z,Sum=0;
System.out.println("Enter the array");
for(x=0;x<5;x++){
for(y=0;y<5;y++){
z=Integer.parseInt(aa.readLine());
a[x][y]=z;

}}
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];

}}

System.out.println("sum of array element”+sum);

}}
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; }}}

PROGRAM: Sum of diagonals

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;
}}

PROGRAM: Decimal to roman


number
import java.io.*;
class Dec2Roman{
public static void main() throws IOException{
DataInputStream in=new DataInputStream(System.in);
System.out.print("Enter Number : ");
int num=Integer.parseInt(in.readLine());
String
hund[]={"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"
};
String
ten[]={"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
String unit[]={"","I","II","III","IV","V","VI","VII","VIII","IX"};
System.out.println("Roman Equivalent=
"+hund[num/100]+ten[(num/10)%10]+unit[num%10]);
}}
PROGRAM: Date from entered
day no.
Import java.io.*;
class Day2Date{
static BufferedReader br =new BufferedReader(new
InputStreamReader(System.in));
public void calc(int n, int yr) {
int a[ ] = { 31,28,31,30,31,30,31,31,30,31,30,31 } ;
String m[ ] = { "Jan", "Feb",
"Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" } ;
if ( yr % 4 == 0)
a[1] =29;int t=0,s=0;
while( t < n) {
t =t + a[s++];}
int d = n + a[--s] - t;
if( d == 1|| d == 21 || d == 31 ){
System.out.println( d + "st" + m[s] + " , "+yr);}
if( d == 2 || d == 22 ){
System.out.println( d + "nd" + m[s] + " , "+yr);}
if( d == 3|| d == 23 ){
System.out.println( d + "rd" + m[s] + " , "+yr);}
else {System.out.println( d + "th" + m[s] + " , "+yr);}}
public static void main(String args[]) throws IOException{
Day2Date obj = new Day2Date();
System.out.println( "Enter day no = ");
int n = Integer.parseInt(br.readLine());
System.out.println( "Enter year = ");
int yr = Integer.parseInt(br.readLine());
obj.calc(n,yr); }}

PROGRAM: Magic square


import java.io.*;
class MagicSquare{
public static void main(String args[])throws
IOException{BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("enter the dimension of magical square=");
int n = Integer.parseInt(br.readLine());
int arr[][]=new int[n][n],c=n/2-1,r=1,num;
for(num=1;num<=n*n;num++) {
r--;c++;
if(r==-1)
r=n-1;
if(c>n-1)
c=0;
if(arr[r][c]!=0){
r=r+2;c--; }
arr[r][c]=num;
if(r==0&&c==0){
r=n-1;c=1;
arr[r][c]=++num; }
if(c==n-1&&r==0)
arr[++r][c]=++num; }
System.out.println();for(r=0;r<n;r++){
for(c=0;c<n;c++)
System.out.print(arr[r][c]+" ");
System.out.println();
}}}

You might also like