Java-Net Beans IDE- Control Structures Programs With Solution
|
Control statements cause
the flow of execution to advance and branch based on changes to the state of a
program. Or control statements control the order of execution in a program,
based on data values and conditional logic.
Sequence : Sequence construct means
the statements are executed sequentially.
PROGRAM
BASED ON SEQUENCE
Program to Run the
“hello, world” program on your system
private
void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
JOptionPane.showMessageDialog(null,
“hello world”);
System.out.println(“hello
world”);
}
Enter your first name in
textfield1 and last name in textfield2 and display your complete name in
textfield3
private
void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField3.setText(jTextField1.getText()+
“ “+jTextField2.getText());
}
Enter your name in text
field1 and display a message “Hello …….Welcome in Java”
private
void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String
name;
Name=jTextField1.getText();
JOptionPane.showMessageDialog(null,
“Hello”+ name + “Welcome in Java”);
}
//Addition of two numbers using Input
box
private
void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Integer fno,sno,sum;
String
n1=JOptionPane.showInputDialog("enter first no");
String
n2=JOptionPane.showInputDialog("enter second no");
fno=Integer.parseInt(n1);
sno=Integer.parseInt(n2);
sum=fno+sno;
JOptionPane.showMessageDialog(null,"the
result is:"+sum);
}
//EXTRACT THE PASSWORD
IN MESSAGEBOX
private
void jPasswordField1ActionPerformed(java.awt.event.ActionEvent evt) {
String
psw= new String(jPasswordField1.getPassword());
JOptionPane.showMessageDialog(this,psw);//
TODO add your handling code here:
}
Enter the amount and
quantity in testfield1 and textfield2 respectively and display the total amount
in textfield3
private
void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
double
qty, rate,totalamt;
qty=Double.parseDouble(jTextField1.getText());
rate=
Double.parseDouble(jTextField2.getText());
totalamt=qty
* rate;
jTextField3.setText(“”+totalamt);
}
Obtain the price and
quantity of an item, calculate the sale-value, discount and net payable amount.
Discount is calculated as 10% of the sale-value.
private
void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
double
qty, price,saleval,dis,finalamt;
qty=Double.parseDouble(jTextField1.getText());
price=
Double.parseDouble(jTextField2.getText());
saleval=qty
* rate;
dis=saleval*10/100;
finalamt=saleval
– dis;
jTextField3.setText(“”+finalamt);
}
Calculate the simple
interest
private
void
jButtonSIActionPerformed(java.awt.event.ActionEvent evt) {
double si,p,r,t;
p=Double.parseDouble(jTxtPrincipal.getText());
r=Double.parseDouble(jTxtRate.getText());
t=Double.parseDouble(jTxtTime.getText());
si=(p*r*t)/100;
JOptionPane.showMessageDialog(null,"Simple Interest="+si);// TODO add
your handling code here:
}
Calculate compound
interest
private
void
jButtonCIActionPerformed(java.awt.event.ActionEvent evt) {
double p,r,t,ci;
p=Double.parseDouble(jTxtPrincipal.getText());
r=Double.parseDouble(jTxtRate.getText());
t=Double.parseDouble(jTxtTime.getText());
ci=p*Math.pow((1+r/100),t);
JOptionPane.showMessageDialog(null,"Compound Interest="+ci);// TODO add
your handling code here:
}
Calculate the average of
three numbers
private
void
jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
double num1,num2,num3,avg;
num1=Double.parseDouble(jTxtNum1.getText());
num2=Double.parseDouble(jTxtNum2.getText());
num3=Double.parseDouble(jTxtNum3.getText());
avg=(num1+num2+num3)/3;
JOptionPane.showMessageDialog(null,"The Average of three numbers ="+avg);
}
Compute xy for given
values of x and y
private
void
jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
double x,y,res;
x=Double.parseDouble(jTxtX.getText());
y=Double.parseDouble(jTxtY.getText());
res=Math.pow(x,y);
JOptionPane.showMessageDialog(null,"Result="+res);// TODO add your
handling code here:
}
Enter the amount and
quantity in testfield1 and textfield2 respectively and display the total amount
in textfield3
private
void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
double
qty, rate,totalamt;
qty=Double.parseDouble(jTextField1.getText());
rate=
Double.parseDouble(jTextField2.getText());
totalamt=qty
* rate;
jTextField3.setText(“”+totalamt);
}
If the marks obtained by a student in
five different subjects are input through the keyboard, find out the aggregate
marks and percentage obtained by the student. (Assume that the maximum marks
that can be obtained by a student in each subject are 100)
private
void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
double
sub1,sub2,sub3,sub4,sub5,total,perc;
sub1=
Double.parseDouble(jTextField1.getText());
sub2=
Double.parseDouble(jTextField2.getText());
sub3=
Double.parseDouble(jTextField3.getText());
sub4=
Double.parseDouble(jTextField4.getText());
sub5=
Double.parseDouble(jTextField5.getText());
total =
sub1 + sub2 + sub3 + sub4 + sub5;
perc =
total / 5;
jTextField6.setText(“”+
total);
jTextField7.setText(“”+
perc);
}
Program to Read Two
Integers M and N & Swap their Values using third variable
private
void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int
m,n,sw;
m=Integer.parseInt(jTextField1.getText());
n=
Integer.parseInt (jTextField2.getText());
sw=n;
n=m;
m=sw;
jTextField1.setText(“”+m);
jTextField1.setText(“”+n);
}
Swap of two no’s without
using third variable
private
void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int x,y;
x=Integer.parseInt(jTextField1.getText());
y=
Integer.parseInt (jTextField2.getText());
x = x+y;
y = x-y;
x = x-y;
jTextField1.setText(""+x);
jTextField2.setText(""+y);
}
Program to Calculate
Area and Circumference of Circle [area = PI * rad * rad, ci = 2 * PI * rad]
private
void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
double
area,rad,ci;
final
double PI=3.14;
rad=Double.parseDouble(jTextField1.getText());
area = PI
* rad * rad;
ci = 2 *
PI * rad;
System.out.println("Area="+area);
System.out.println("ci="+ci);
}
Program to Calculate Area of Circle [area = 3.14 * radius * radius]
private void
jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
double
area,rad;
final
double PI=3.14;
rad=Double.parseDouble(jTextField1.getText());
area = PI
* rad * rad;
System.out.println("Area="+area);
}
Program to Calculate Area of Rectangle [area = length * breadth]
private
void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
double
area,length,breadth;
length=Double.parseDouble(jTextField1.getText());
breadth=Double.parseDouble(jTextField2.getText());
area =
length * breadth;
System.out.println("Area="+area);
}
Program to Calculate
Area of Square [area = a2]
private
void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
double
area,a;
a=Double.parseDouble(jTextField1.getText());
area = a
*a;
System.out.println("Area="+area);
}
Calculate
the area of a triangle [area=1/2f*base*height]
private
void jButton1ActionPerformed(java.awt.event.ActionEvent
evt) {
double base,height,area;
base=Double.parseDouble(jTxtBase.getText());
height=Double.parseDouble(jTxtHeight.getText());
area=1/2f*base*height;
jTxtArea.setText(""+area)
}
Reads customer name, customer number
and unit consumed and prints total amount to be paid
private
void jButton1ActionPerformed(java.awt.event.ActionEvent
evt) {
String
Cust_Name, Cust_No,Unit_Cons,Unit_Charge,Total_Amt;
Cust_Name=jTextField1.getText();
Cust_No=Double.parseDouble(jTextField2.getText());
Unit_Cons=
Double.parseDouble(jTextField3.getText());
Unit_Charge=
Double.parseDouble(jTextField4.getText());
Total_Amt
= Unit_Cons * Unit_Charge;
jTextField5.setText(“”+Total_Amt);
}
Calculate gross salary of a person [ gross_salary = basic + da + ta]
private
void jButton1ActionPerformed(java.awt.event.ActionEvent
evt) {
double
basic,da,ta,gross_sal;
basic=Double.parseDouble(jTextField1.getText());
da=Double.parseDouble(jTextField2.getText());
ta=Double.parseDouble(jTextField3.getText());
gross_sal
= basic+da+ta;
jTextField4.setText(“”+gross_sal);
}
Sachin’s basic salary is input through the keyboard. His dearness allowance is 40% of basic salary, and house rent allowance is 20% of basic salary. Write a program to calculate his gross salary.
private
void jButton1ActionPerformed(java.awt.event.ActionEvent
evt) {
double
basic,da,hra,gross_sal;
basic=Double.parseDouble(jTextField1.getText());
da=basic*40/100;
hra=basic*20/100;
gross_sal
= basic+da+hra;
jTextField4.setText(“”+gross_sal);
}
Program to
Solve Second Order Quadratic Equation
[desc = sqrt(b*b-4*a*c)
root1 = (-b + desc)/(2.0*a)
root2 = (-b - desc)/(2.0*a)]
private
void jButton1ActionPerformed(java.awt.event.ActionEvent
evt) {
double
desc,a,b,c,root1,root2;
b=Double.parseDouble(jTextField1.getText());
a=Double.parseDouble(jTextField2.getText());
c=Double.parseDouble(jTextField3.getText());
desc =
Math.sqrt((b*b)-4*a*c);
root1=(-b
+ desc)/ (2.0*a);
root2=(-b
- desc)/ (2.0*a);
System.out.println("Root1="+root1);
System.out.println("Root2="+root2);
}
Program for Temperature Conversion from
Fahrenheit to Celsius. [cen = 5/9 * (f-32)]
private
void jButton1ActionPerformed(java.awt.event.ActionEvent
evt) {
double
f,cen;
f=Double.parseDouble(jTextField1.getText());
cen = 5/9
* (f -32);
jTextField2.setText(“”+cen);
}
Convert temperature from degree centigrade to Fahrenheit[fahrenheit = (1.8 * celsius) + 32]
private
void jButton1ActionPerformed(java.awt.event.ActionEvent
evt) {
double f,
c;
c=Double.parseDouble(jTextField1.getText());
f = (1.8 *
c) + 32;
jTextField2.setText(“”+f);
}
Selection construct means
the execution of the statements depending upon a condition. If condition
evaluates to true, a set of statements is followed otherwise another course of
action is followed.
a)
if if statement check the
condition, if the condition evaluates true, a set of statements is executed.
Otherwise if the condition evaluates to false, the set of statements is
ignored.
Syntax: if
(condition)
Statement;
Program to check the age of a voter
for eligibility
|
||||
|
||||
if – else
Syntax: if
(condition) {
Statement
1;
}
else{
Statement
2;
}
If
statement helps to execute a block of statement based on the result of a condition.
If the condition set evaluates to true on block of statement is executed otherwise
another block is executed
|
|
|
This is a multiway branch
statement. It provides an easy way to dispatch execution to different parts of
your code based on the value of an expression. It provides a better alternative
than a large series of if-else-if statements.
Syntax:
switch (expression) {
case
value1:
statement1;
break;
case
value2:
statement2:
break;
case
value3:
statement;
break;
.
.
.
.
default:
statement;
}
The expression must be of
type byte, short, int or char. Each case value must be a unique literal. The
value of the expression is compared with each of the literal values in the case
statements. If a match is found, the statement following that case statement is
executed. If none of the constants matches the value of the expression, then
the default statement is executed. However, the default statement is
optional. If no case matches and no default is present, then no further
action is taken. The break statement is used inside the switch to
terminate a statement.
PROGRAMS
BASED ON DECISION MAKING
Program to Check if a
given Integer is Positive or Negative
private
void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int num;
num=Integer.parseInt(jTextField1.getText());
if(num<0){
JoptionPane.showMessageDialog(null,
”Number is negative”);
}
else{
JoptionPane.showMessageDialog(null,
”Number is positive”);
} }
Program to
Find the Biggest of 3 Numbers
private
void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int
num1,num2,num3;
num1=Integer.parseInt(jTextField1.getText());
num2=Integer.parseInt(jTextField2.getText());
num3=Integer.parseInt(jTextField3.getText());
if(num1>num2
&& num1>num3){
JoptionPane.showMessageDialog(null,
num1+” is greater”);
}
else
if(num2>num1 && num2>num3){
JoptionPane.showMessageDialog(null,
num2+” is greater”);
}
else
{
JoptionPane.showMessageDialog(null,
num3+” is greater”);
}
}
Program to
Find if a given Year is a Leap Year
private
void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int year;
year=Integer.parseInt(jTextField1.getText());
if(year%4==0
&& (year%400==0||year%100!=0)){
System.out.println("This is a leap year");
System.out.println("This is a leap year");
}
else{
System.out.println ("This is not a leap year");
else{
System.out.println ("This is not a leap year");
} }
Program to
Accept two Integers and Check if they are Equal
private
void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int a,b;
a=Integer.parseInt(jTextField1.getText());
b=Integer.parseInt(jTextField2.getText());
if(a>b)
{
System.out.println("a is
greater");
}
if(b>a)
{
System.out.println("b is
greater");
}
}
w.r.p
to check whether a given character is a vowel or consonant using if and switch
using - if
private
void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
char ch;
String
s=jTextField1.getText();
ch=s.charAt(0);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'){
System.out.println(“Its
Vowel”);
}
else
{
System.out.println(“Its
Vowel”);
} }
Using –switch
private
void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
char ch;
String
s=jTextField1.getText();
ch=s.charAt(0);
switch(ch){
case ‘a’:
case ‘e’:
case ‘i’:
case ‘o’:
case ‘u’:
System.out.println(“Its Vowel”);
break;
default :
System.out.println(“Its Consonant”);
}
Program to Accept the Height of a
Person & Categorize as Taller, Dwarf & Average
private
void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int
height;
height=Integer.parseInt(jTextField1.getText());
if(height>5){
System.out.println(“Taller”);
}
else
if(height>=4 && height<=5){
System.out.println(“average””);
}
else
{
System.out.println(“Dwarf”);
}
}
loop or
iterations helps to repeat a group of statements number of times under a
condition. Java supports three kinds of loop: while loop, for loop, do while
loop
The iteration construct means repetition of a set of statements depending upon a condition test. Till the time a condition is true it repeat the statements again and again as soon as the condition becomes false it terminate from the loop body.
(a) Entry-controlled/ Top-Tested/ Pre-Tested firstly initialize the expression then
it test the condition if the condition is true then it enters the loop body and
execute the statements. (e.g for, while)The general form of for loop:
for(initialization
expression; test expression; increment/decrement expression)
{
body of
the loop;
}
Note:
It is to be noted that all the parts
of the loop in the above statement are optional. In case if a programmer wants
to specify more than one initialization or increment/decrement then it has to
be separated by (,).
for(int
i=1; i<= 10; i++)
for(i=1, j
= 10; i<j; i++, j++) //
more than one initialization or increment/decrement
for(i =
10, j= 20; i>= 1 && j<= 30 ; i-- , j++) // more than one condition joined using &&
for(;
i<= 10; i++) //
initialization missing still using ;
for(;
i<= 10;) //
initialization, inc./dec. missing still using ;
Infinite Loop/Endless Loop
Example:
private
void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int i;
for(i=1;i<=10;i--){
System.out.printl(i);
}
(b) do – while/Exit-controlled/Bottom-tested/ Post-tested do while loop first execute the statement and print it then check
the condition, if condition is true then it repeats the statements until the
condition satisfy and if condition is false then it terminates from the loop body.
The do while loop always executes its body at least once.
Syntax:
do
{
statements;
}
while (condition);
Example:
private
void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int i=1;
do{
System.out.printl(i);
I++;
}while(i<=10);
(c) while
/Entry-controlled / Top-Tested/ Pre-Tested It
repeats a statement or block while condition is true. When condition becomes
false it terminates from the loop body. The curly braces are optional if only a
single statement is being repeated.
Syntax:
while
(condition)
{
statement;
+
+ / - -;
}
Example:
private
void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int i=1;
while(i<=10){
System.out.printl(i);
i++;
}
JUMP STATEMENTS
Java supports three jump
statements: break, continue, and return. These statements transfer the control to another part of
your program.
Note: java supports one other
way that you can change your program’s flow of execution through exception
handling. Through exception handling run-time errors can be trapped and handled
by your program. It is supported by the keywords try, catch, throw, throws and
finally.
The ‘break’ Keyword
Using break we can exit a
loop immediately. It can be used with for, while, do while, and switch
statement. When break keyword is used within a loop, the program control is
immediately transferred out of the loop.
Example:
Find the sum of natural numbers up to 10.
private
void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int i,sum=0;
for(i=1;i<=10;i++){ Output à
sum=sum+i; 1
if(sum==6){ 3
break;
}
System.out.println(sum);
} }
The ‘continue’ keyword
The loop does not terminate
when a continue statement is encountered. A continue statement causes control
to be transferred directly to the conditional expression that controls the
loop.
|
Example: Find the sum of natural
numbers up to 10.
private
void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int i,sum=0;
for(i=1;i<=10;i++){ Output à
sum=sum+i;
if(sum==6){
continue;
}
System.out.println(sum);
} }
|
Example-2
private void
jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int i;
for(i=0;i<10;i++){
System.out.print(i+ " ");
if(i%2==0)
continue;
System.out.println("");
} }
The ‘return’ keyword
The return statement is
used to explicitly return from a method. It causes program control to transfer
back to the caller of the method. e.g. addition of two numbers using function.
int
addition (int num1, int num2){
int
res;
res=
num1+num2;
return res;
}
PROGRAMS BASED ON LOOPING
Print First 10
Natural Numbers
private
void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int i,sum=0;
for(i=1;i<=10;i++){
sum=sum+i;
}
System.out.println(“The
sum of natural numbers=”+sum);
}
Generate a
table of any number
int
i,num,res;
n=Integer.parseInt(jTextField1.getText());
for(i=1;i<=10;i++){
res=num*i;
}
System.out.println(n
+ “x” + i + “=”+res );
}
w.r.p to find the sum of n natural numbers
int
i,n,sum=0;
n=Integer.parseInt(jTextField1.getText());
for(i=1;i<=n;i++){
sum=sum+i;
}
System.out.println(“The
sum of natural numbers=”+sum);
}
Find Factorial
of Number
int
i,n,fact=1;
n=Integer.parseInt(jTextField1.getText());
for(i=1;i<=n;i++){
fact=fact*i;
}
System.out.println(“The
Factorial of Number=”+fact);
}
FIND ALL THE LEAP YEARS
BETWEEN 2000 AND 2040
private
void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int i;
for(i=2000;i<=2040;i++)
{
if(i%4==0)
{
System.out.println(i+"");
}
}
}
COUNT THE
VOWEL AND CONSONANT FROM A GIVEN STRING
private
void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int
i,vo=0,co=0;
char ch;
String
s=jTextField1.getText();
for(i=s.length()-1;i>=0;i--){
ch=s.charAt(i);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'){
vo++;
}
else
{
co++;
}}
System.out.println("vowels"+vo);
System.out.println("conso"+co);//
TODO add your handling code here:
}
w.r.p to compute sum of all even and odd
no's from the first n natural no's
private void
jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int
n,t,s1,s2;
s1=0;
s2=0;
n=Integer.parseInt(jTextField1.getText());
for(t=1;t<=n;t++)
{
if(t%2==0)
{
s1=s1+t;
}
else
{
s2=s2+t;
}
}
System.out.println("total of even
numbers"+s1);
System.out.println("total of odd
numbers"+s2); // TODO add
your handling code here:
}
Program to
Find the Number of Integers Divisible by 5
private
void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int num,i;
num=Integer.parseInt(jTextField1.getText());
for(i=1;i<=num;i++){
if(i%5==0){
System.out.print(i); }
}
}
Program to
Reverse a Given Number
Using
while loop-
private
void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int n,r,s=0;
n=Integer.parseInt(jTextField1.getText());
while(n!=0)
{
r=n%10;
s=s*10+r;
n=n/10;
}
jTextField2.setText(""+s);
}
Using for loop-
private
void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int i,num,r,s;
num=Integer.parseInt(jTextField1.getText());
for(s=0;num>0;num=num/10){
r=num%10;
s=s*10+r;
}
System.out.println(s); // TODO add your handling code here:
}
Program to
Reverse a Number & Check if it is a Palindrome
private
void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int i,num,r,s,n;
num=Integer.parseInt(jTextField1.getText());
n=num;
for(s=0;num>0;num=num/10){
r=num%10;
s=s*10+r;
}
if(s==n){
System.out.println(“Number is
palindrome”);
}
else
{
System.out.println(“Number is not
palindrome”);
} // TODO add your handling code here:
}
w.r.p
that accept a number and print the sum of the number e.g (156=1+5+6=12)
Using For loop-
private
void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int
i,r,s=0,num;
num=Integer.parseInt(jTextField1.getText());
for(i=num;i>=0;i--){
r=num%10;
s=s+r;
num=num/10;
}
System.out.println(s);
}
Using while loop-
private
void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int
n,r,s=0;
n=Integer.parseInt(jTextField1.getText());
while(n!=0)
{
r=n%10;
s=s+r;
n=n/10;
}
jTextField2.setText(""+s); }
w.r.p
that accept a number and print the product of the number e.g. (236=2*3*6=36)
private
void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int
n,r,s=1;
n=Integer.parseInt(jTextField1.getText());
while(n!=0)
{
r=n%10;
s=s*r;
n=n/10;
}
jTextField2.setText(""+s); }
Palindrome
String
Using while loop-
private
void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int i;
String s,str="";
s=jTextField1.getText();
i=s.length()-1;
while(i>=0){
str=str+s.charAt(i);
i--;
}
if(s.equals(str)){
System.out.print("palindrome");
}
else {
System.out.print("not
palindrome"); }
}
Using for loop-
private
void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String
s=jTextField1.getText();
String
st="";
for(int
i=s.length()-1;i>=0;i--)
{
st=st+s.charAt(i);
}
if(s.equals(st))
{
System.out.println("string is
palindrome");
}
else
{
System.out.println("string is not a
palindrome");
}// TODO
add your handling code here:
}
Check Whether
Number is Perfect Or Not
(A number is called perfect if it is equal to the
sum of its factors other than the number itself)
private
void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int n,sum=0,i=1;
n=Integer.parseInt(jTextField1.getText());
while(i<n)
{
if(n%i==0) {
}
i++;
}
if(sum==n)
System.out.println("The number is
perfect");
else
System.out.println("The
number is not perfect");
}
Program to
Check whether a given Number is Armstrong
private
void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int n,r,s=0,nv;
n=Integer.parseInt(jTextField1.getText());
nv=n;
while(n!=0) {
r=n%10;
s=s+r*r*r;
n=n/10;
}
if(s==nv) {
System.out.println("The no
is Armstrong");
}
else {
System.out.println("The
no is not Armstrong"); }
}
Program to
Print Armstrong Number from 1 to 10000
private void jButton1ActionPerformed(java.awt.event.ActionEvent
evt) {
for(int
n=1;n<=9999;n++){
int r=0,s=0,num;
num=n;
while(num!=0){
r=num%10;
s=s+r*r*r;
num=num/10;
}
if(n==s)
System.out.println("Arm ="+s);
}
}
Program to
Generate Fibonacci Series of N Numbers OR Generate the Fibonacci Series
starting from any two numbers
private
void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int a,b,c,i=0,n;
n=Integer.parseInt(jTextField1.getText());
a=0;
b=1;
System.out.println(a);
System.out.println(b);
while(i<n-2)
{
c=a+b;
System.out.println(c);
a=b;
b=c;
i=i+1;
}
}
Comments
Post a Comment