Hey welcome to my blog. I will soon be posting many more programs but here's the first one.
Program 1:
class num_design
{
public void main()
{
int s=0,j=0,a=1;
for(int i=5;i>0;i--)
{
for(j=1;j<=i;j++)
System.out.print(j);
for(int x=0;x<2*a-2;x++)
System.out.print(" ");
for(int k=j-1;k>0;k--)
System.out.print(k);
System.out.println();
a++;
}
}
}
it should give you this output:
Program 2:
A magic no. is a number whose sum of digits is 1, by repeated addition. Check for magic numbers.
//to check whether a number is a magic no(sum of digits is 1) or not.
import java.io.*;
class magical
{
public void main(int i)
{
int d=0;
do
{
d=0;
while(i!=0)
{
d+=i%10;
i/=10;
}
i=d;
}while(d>9);
System.out.println("Sum of digits = "+d);
if(d==1)
System.out.println("magic");
else
System.out.println("not magic");
}
}
Program 3:
//to perform base conversion fron binary to decimal and vice versa
import java.io.*;
class num_base_conv
{
public void dec2bin(int d)
{
int bin[] = new int[20];
int i=0;
while(d!=0)
{
bin[i]=d%2;
d/=2;
i+=1;
}
System.out.print("\nBinary = ");
for(int j=i;j>=0;j--)
System.out.print(bin[j]);
}
public void bin2dec(int B)
{
int k,d=0,i=0;
while(B!=0)
{
k=B%10;
B/=10;
d+=k*(Math.pow(2,i));
i++;
}
System.out.print("\nDecimal="+d);
}
public void main()throws IOException
{
int ch=0,I;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("For Decimal to Binary press 1");
System.out.println("For Binary to Decimal press 2");
System.out.println("Enter choice");
ch = Integer.parseInt(br.readLine());
System.out.println("Enter Input");
I = Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
dec2bin(I);
break;
case 2:
bin2dec(I);
break;
default:
System.out.println("Wrong choice");
}
System.out.print("\nThank you");
}
}
Program 4:
/**
* find the biggest word in the string
* find the frequency of each character of a string
* @author (Sounak Dey)
* @version (26/11/2010)
*/
public class Text
{
String str;
Text(String x)
{
str=x;
}
String longest()
{
String r=str+" ", word="", bigWord="";
int i, len=r.length();
char ch;
for(i=0; i<len; i++)
{
ch=r.charAt(i);
if(ch!=' ')
word+=ch;
else
{
if(word.length()>0)
{
if(word.length()>bigWord.length())
bigWord=word;
word="";
}
}
}
return bigWord;
}
void frequency()
{
String copy=str.toLowerCase();
char ch,ch1;
int count,i,n=copy.length();
for(ch='a'; ch<='z'; ch++)
{
count=0;
for(i=0; i<n; i++)
{
ch1=copy.charAt(i);
if(ch1==ch)
count++;
}
if(count>0)
System.out.println("Frequency of "+ch+" is "+count);
}
}
/*
void frequency()
{
String r="",copy=str.toLowerCase();
char ch, ch1;
int count, i, n=copy.length();
while(copy.length()>0)
{
ch=copy.charAt(0);
count=0;
for(i=0; i<copy.length(); i++)
{
ch1=copy.charAt(i);
if(ch==ch1)
count++;
else
r+=ch1;
}
if(count>0)
System.out.println("Frequency of "+ch+" is "+count);
copy=r;
r="";
}
}
*/
}
Program 5:
/**
* To change an expresion from infix to postfix.
*
* @author (Sounak Dey)
* @version (a version number or a date)
*/
import java.io.*;
public class InfixToPostFix
{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String inFix="", postFix="";
System.out.println("Enter an infix expression: ");
inFix=br.readLine();
postFix=convertTopostFix(inFix);
System.out.println("Infix expression: "+inFix);
System.out.println("Equivalent Postfix expression: "+postFix);
}
public static String convertTopostFix(String x)
{
String s=x, ps="";
CStack st= new CStack(50);
//step 1: append a ')' to s
s+=')';
//step 2: push a '(' to the stack
st.push('(');
int i;
char token;
//step 3: scan the 's' from left to right and do the following
for(i=0; i<s.length(); i++)
{
//get tokens
token=s.charAt(i);
//operand? append to the 'ps'
if(isOperand(token))
ps+=token;
//operator? repeatedly pop operators from the stack which precedence is >= the token
//then push the token
else if(isOperator(token))
{
while(isOperator(st.stackTop()) && precedence(st.stackTop())>=precedence(token))
ps+=st.pop();
st.push(token);
}
//left parenthesis: push it to the stack
else if(token=='(')
st.push(token);
//right parenthesis: repeatedly pop all elements until a left parenthesis is encountered.
//then throw the right parenthesis
else if(token==')')
{
while(st.stackTop()!='(')
ps+=st.pop();
st.pop(); //pop the '(' - throw it away
}
}
//if the expression is properly written
//then the stack will be empy
if(!st.isEmpty())
{
System.out.println("Error: some error is there in the string");
System.exit(1);
}
return ps;
}
public static boolean isOperator(char ch)
{
if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='^')
return true;
return false;
}
public static boolean isOperand(char ch)
{
if((ch>='A' && ch<='Z')||(ch>='a' && ch<='z'))
return true;
return false;
}
public static byte precedence(char ch)
{
switch(ch)
{
case '+':
case '-': return 1;
case '*':
case '/': return 2;
case '^': return 3;
}
return 0;
}
}
public class CStack
{
char []a;
int top;
CStack(int n)
{
a= new char[n];
top=-1;
}
void push(char m)
{
if(top==a.length-1)
{
System.out.println("Stack is full");
System.exit(1);
}
top++;
a[top]=m;
}
char pop()
{
char x;
if(top==-1)
{
System.out.println("Error");
System.exit(1);
}
x=a[top];
top--;
return x;
}
public char stackTop()
{
if(top==-1)
{
System.out.println("Error: stack is empty");
System.exit(1);
}
return a[top];
}
public boolean isEmpty()
{
return top==-1;
}
void display()
{
int i;
if(top==-1)
System.out.println("Stack is empty");
else
{
System.out.println();
System.out.print("Stack: (close end)");
for(i=0;i<=top;i++)
System.out.print(a[i]+" ");
System.out.print("(top)");
System.out.println();
}
}
}
Program 6:
// Name: Sounak Dey
public class CircularQ
{
private int a[];
private int rear, front;
//constructors
CircularQ()
{
a=new int[5];
rear=front=-1;
}
CircularQ(int n)
{
a=new int[n];
rear=front=-1;
}
public boolean isFull()
{
return (front==0&&rear==a.length-1)||(front==rear+1);
}
public boolean isEmpty()
{
return rear==-1;
}
public void insert(int x)
{
if(isFull())
{
System.out.println("Error: Queue overflow");
System.exit(1);
}
if(isEmpty())
front=0;
rear=(rear+1)%a.length; //increase rear circularly
a[rear]=x; //insert the element through rear
}
public int remove()
{
if(isEmpty())
{
System.out.println("Queue is underflow");
System.exit(1);
}
int x=a[front]; //collect element from front
if(front==rear) //when it contain a single element
front=rear=-1;
else
front=(front+1)%a.length; //increase front circularly
return x;
}
public void display()
{
if(isEmpty())
{
System.out.println("Queue is empty");
}
else if(front<=rear)
{
int i;
System.out.print("Queue: [F]");
for(i=front; i<=rear; i++)
System.out.print(a[i]+" ");
System.out.println("[R]");
}
else if(front>rear)
{
int i;
System.out.print("Queue: [F]");
for(i=front; i<a.length; i++)
System.out.print(a[i]+" ");
for(i=0; i<=rear; i++)
System.out.print(a[i]+" ");
System.out.println("[R]");
}
}
}
Program 7:
// Name : Sounak Dey
public class Pattern
{
public static void main(String args[])
{
int i,j;
for(i=5;i>=1;i--)
{
for(j=5;j>=1;j--)
{
if(j<=i)
{
System.out.print(i);
}
else
{
System.out.print(j);
}
}
System.out.println();
}
}
}
The output of the above program is:
Program 8:
* To count trhe number of puncuation and words in a string......
* @author (Sounak Dey)
* @version (a version number or a date)
*/
import java.io.*;
public class word
{
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s,temp="";
char x;
System.out.println("Enter a String");
s=br.readLine();
int j=0,n=0;
s=s.trim();
s=s+" ";
for(int i=0;i<s.length();i++)
{
x=s.charAt(i);
if(x==' '||x==','||x==';'||x=='.')
{
j++;
if(!temp.equals(""))
{
n++;
System.out.println(temp);
temp="";
}
}
else
temp=temp+x;
}
System.out.println("The number of words in the string ="+n);
System.out.println("The number of punchuations in the String="+j);
}
}
That's it for my 1st. I'll be back with more. Bye :)