You are on page 1of 12

Java Programs

1.)To input 2 numbers and display their sum and difference *Coding on action performed by sum button:int a=Integer.parseInt(jTft1.getText()); int b=Integer.parseInt(jTft2.getText()); int c=a+b; jtf3.setText(Sum=+c); *Coding on action performed by difference button:int d=a-b; jtf4.setText(Difference=+d);

2.)To check whether a person is eligible for voting or not:int age=Integer.parseInt(jTf1.getText()); if(age>18) jTf2.setText(You are eligible for Voting); else jTf2.setText(You are not eligible for Voting);

3.)To create a login Window:*Coding on action performed by login button:String uname=jTf1.getText();

String pass=new String(jPf1.getPassword()); if((pass.compareTo(hello)==0)&&(uname.compareTo(informatic s)==0)) jTextArea1.setText(WELCOME); else jTextArea1.setText(INVALID USERNAME OR PASSWORD);

4.)To check whether number is prime or not:int a,count=0; a=Integer.parseInt(jTf1.getText()); for(int i=2;i<100;i++) { if(a%i==0) count++; } if(count==1) jTf2.setText("THE NUMBER IS PRIME");

5.)To convert a decimal into Binary:long dec,rem,i=1,sum=0; dec=Long.parseLong(jTf1.getText()); do { rem=dec%2; sum=sum + (i*rem);

dec=dec/2; i=i*10; }while(dec>0); jTextArea1.append(Binary of the give number is: +sum);

6.)To reverse a string:String s=jtf1.getText(); String p= for(int i=0;i<s.length();i++) {p=s.charAt(i)+p; } jTf2.setText(p);

7.)To find Factorial of a given number:int n=Integer.parseInt(jTf1.getText()); int p=1;


for(int i=1;i<=n;i++) { p=p*i; } jTf2.setText(Factorial=+p); 8.)To search for a character in a string:-

int f=0; String c=jTf1.getText(); String sr=jtf2.getText(); for(int i=0;i<sr.lenght();i++) {if(s.equalsIgnoreCase(sr.charAt(i))=true) break; f=1;} if(f==0) jTtf3.setText(Character is Absent); else jTf3.setText(First occurrence ofCharacter is at position+(i+1));

9.)To input day number and print dayname:int d=Integer.parseInt(jTtf1.getText()); String s=; switch(d) { case 1: s=Monday; break; case 2: s=Tuesday; break; case 3: s=Wednesday; break; case 4: s=Thursday; break; case 5: s=Friday;

break; case 6: s=Saturday; break; case 7: s=Sunday; break; default: s=Invalid day number } jTf2.setText(+s);

10.)To print a pattern:int i,j; for(i=1;i<=5;i++) { for(j=1;j<=i;j++) jTextArea1.append(*); jTextArea1.append(\n) }

11.)To calculate simple interest:-

double p=Double.parseDouble(jTf1.getText()); double r=Double.parseDouble(jTf2.getText()); double p=Double.parseDouble(jTf3.getText()); double i=(p*r*t)/100; jTf4.setText(Interest=+i);

12.)To search for an element in a 1-d array:-

int f=0; int ar[]={1,9,3,8,7,6,4,5,2,10}; int n=Integer.parseInt(jTf1.getText()); for(int i=0;i<10;i++) { if(a[i]==n) {f=1; break;} } if(f==1) jTextArea1.setText(Element found at position +(i+1)); else jTextArea1.setText(Element not present);

13.)To print sum of the digits of an integer:String s=jTf1.getText(); int a=0,b; for(int i=0;i<s.length();i++) {a=a+Integer.parseInt(s.charAt(i)); } jTf2.setText(Sum of digits = +a);

14.)To check whether an integer is perfect or not:int n=Integer.parseInt(jTf1.getText()); int sum=0; for(int i=1;i<n;i++) {i f(n%i==0) sum=sum+i; } if(sum==n) jTf2.setText(GIVEN NO IS PERFECT); else jTf2.setText(GIVEN NO IS NOT PERFECT);

15.)To check whether a string is palindrome or not:String w=; String s=jTf1.getText(); for(int i=0;i<s.length();i++) {w=s.charAt(i)+w; } if(s.equalsIgnoreCase(w)===true) jTf2.setText(GIVEN STRING IS PALINDROME); else

jTf2.setText(GIVEN STRING IS NOT PALINDROME);

16.)To find sum of the series:1+1/2+1/3+1/4+...:double sum =0; int n=Integer.parseInt(jTf1.getText()); for (int i=1;i<=n;i++) sum=sum+(1/i); jTf2.setText(Sum of series = +sum);

17.)To find the sum of the series :x+(x^3/3)+(x^5/5)+...n termsdouble sum=0; int n=Integer.parseInt(jTf1.getText()); int x=Integer.parseInt(jTf2.getText()); for(int i=1;i<=n;i=i+2) sum=sum+((Math.pow(x,i))/i); jTf3.setText(Sum of series = +sum);

18.)To search for a word in a sentence:import java.util.*; int f=0,i=1; String sen=jTf1.getext(); String wor=jTf2.getext(); StringTokenizer str=new StringTokenizer(sen);

while(str.hasMoreTokens()==true) {if(str.nextToken().equalsIgnoreCase(w)==true) {f=1; break;} i++;} if(f==1) jTf3.setText(WORD PRESENT AT POSITION = +i); else jTf3.setText(WORD NOT PRESENT = );

19.)To input three sides and check whether a triangle is possible or not:int a=Integer.parseInt(jTf1.getText()); int b=Integer.parseInt(jTf2.getText()); int c=Integer.parseInt(jTf3.getText()); int d=a+b; int e=b+c; int f=c+a; if((d>c)&&(e>a)&&(f>b)) jTextArea1.append(Triangle is possible); else jTextArea1.append(Triangle is not possible);

20.)To print the Fibonacci series till n terms:-

21.)To print the sum of the elements of a 3x3 matrix:int ar[][]={{1,2,3},{4,5,6},{7,8,9}}; int s=0; jTextArea1.append(Matrix : \n); for(int i=0;i<3;i++) { for(int j=0;j<3;j++) {jTextArea1.append(ar[i][j]); s=s+ar[i][j];} jTextArea1.append(\n); } jTextArea2.append(Sum of the elements \n of the matrix = +s);

22.)To check whether a number is Armstrong or not:-

String n=jTf1.getText(); int s=0; for(int i=0;i<n.length();i++) {s=s+(Math.pow(Integer.parseInt(s.charAt(i)),3)); } if(s==(Integer.parseInt(n))==true) jTf2.setText(GIVEN NO IS ARMSTRONG); else jTf2.setText(GIVEN NO IS NOT ARMSTRONG);

23.)To check whether an year is leap or not:-

24.)To calculate wages of a worker: wages=d*no of working days ,where d=100 for males and d=120 for females:int da=Integer.parseInt(jTf2.getText()); int d; String name=jTf1.getText(); if(jrbtn1.isSelected()==true)

d=100; else if(jrbtn2.isSelected()==true) d=120; int wage=d*da; jTf3.setText(+wage);

25.)To check whether a repeater should be installed between2 buildings depending on their distance(if distance >100m,repeater should be installed):int dis=Integer.parseInt(jTf1.getText()); if(dis>100) jTextArea1.setText(Repeater should be installed between building A and B); else if(dis<100) {if(dis>0) jTextArea1.setText(No use of repeater between Building A and B); else jTextArea1.setText(ERROR---DISTANCE CANNOT BE NEGATIVE); }

You might also like