wap to check a no. is Armstrong or not.
Solution:-
import java.io.*;
public class fourteen
{
public static void
main()throws IOException
{
BufferedReader d=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter the value
of n:-");
int n=Integer.parseInt(d.readLine());
int m=n,s=0,r;
while(m>0)
{
r=m%10;
s=s+(r*r*r);
m=m/10;
}
if(n==s)
System.out.println("Number is
Armstrong Number");
else
System.out.println("Number is not
Armstrong Number");
}
}
Output
Enter the value of n:-
153
Number is Armstrong Number
Variable
Description
Variable
|
Type
|
Purpose
|
n
|
Integer
|
Storing
number for checking
|
m
|
Integer
|
Duplicate
copy of n
|
s
|
Integer
|
For
doing the sum of digit’s cube
|
r
|
Integer
|
For
storing the digit of number
|
wap to accept the age of n employees &
count the numbers of persons in the following age group
(i) 26 – 35 (ii) 36 – 45 (iii) 46 – 55
Solution:-
import
java.io.*;
public
class thirteen
{
public
static void main()throws IOException
{
BufferedReader d=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter the value of
n:-");
int n=Integer.parseInt(d.readLine());
int age,a=0,b=0,c=0;
for(int i=1;i<=n;i++)
{
System.out.println("Enter the age
of a person:-");
age=Integer.parseInt(d.readLine());
if(age>=26&&age<=35)
a++;
if(age>=36&&age<=45)
b++;
if(age>=46&&age<=55)
c++;
}
System.out.println("Age Group 26- 35
:- "+a);
System.out.println("Age Group 36- 45
:- "+b);
System.out.println("Age Group 46- 55
:- "+c);
}
}
Output
Enter
the value of n:-
6
Enter
the age of a person:-
36
Enter
the age of a person:-
54
Enter
the age of a person:-
27
Enter
the age of a person:-
52
Enter
the age of a person:-
38
Enter
the age of a person:-
29
Age
Group 26- 35 :- 2
Age
Group 36- 45 :- 2
Age
Group 46- 55 :- 2
Variable
Description
Variable
|
Type
|
Purpose
|
i
|
Integer
|
Counter
Variable
|
age
|
Integer
|
For
storing age of a person
|
n
|
Integer
|
Number
of person
|
a
|
Integer
|
For
counting no. of person
|
b
|
Integer
|
For
counting no. of person
|
c
|
Integer
|
For
counting no. of person
|
Write a
program to display the following
*
***
*****
***
*
public class tweleb
{
public void main()
{
for(int i=1;i<=5;i=i+2)
{
for(int j=1;j<=i;j++)
System.out.print("*");
System.out.println();
}
for(int i=3;i>=1;i=i-2)
{
for(int j=1;j<=i;j++)
System.out.print("*");
System.out.println();
}
}
}
Output
*
***
*****
***
*
Variable
Description
Variable
|
Type
|
Purpose
|
i
|
Integer
|
Counter
Variable
|
j
|
Integer
|
Counter
Variable
|
Write a
program to display the following
56789
6789
789
89
9
Solution:-
public class twele
{
public void main()
{
for(int i=5;i<10;i++)
{
for(int j=i;j<10;j++)
System.out.print(j);
System.out.println();
}
}
}
Output
56789
6789
789
89
9
Variable Description
Variable
|
Type
|
Purpose
|
i
|
Integer
|
Counter
Variable
|
j
|
Integer
|
Counter
Variable
|
//x
– x3/3! + x5/5! – x7/7! ……………………xn/n!
Solution:-
public
class seven
{
public
void main(int n,int x)
{
double
sum=0;
int
f;
double
p;
int
sign=1;
for(int
i=1;i<=n;i=i+2)
{
p=Math.pow(x,i);
f=fact(i);
sum=sum+(p/f)*sign;
sign=sign*-1;
}
System.out.println("Sum
is :- " + sum);
}
public
int fact(int a)
{
int x=1;
for(int i=1;i<=a;i++)
x=x*i;
return x;
}}
Output
n=5
x=2
Sum
is :- 0.9333333333333333
Variable
Description
Variable
|
Type
|
Purpose
|
n
|
Integer
|
Storing
number of terms in series
|
x
|
Integer
|
Storing
value of x in series
|
i
|
Integer
|
Counter
Variable
|
f
|
Integer
|
Storing
factorial of given integer
|
sum
|
double
|
Storing
result
|
p
|
double
|
Storing
power of given number
|
a
|
integer
|
Passing
argument in function fact()
|
x
|
integer
|
Returning
value from function fact()
|
No comments:
Post a Comment