Program 6 Algorithm: Void Input
Program 6 Algorithm: Void Input
Write a program to input a three digit number and check and print whether a no is Armstrong
no or not
Algorithm
Step1: Start
Step2: Initialization of instance variables n,result=0
Step3: End
Void input()
Step1: Start
Step2: Initliaze the scanner class
Step3: Take the user input for n
Step4: End
Void calculate()
Step1: Start
Step2: Initialize the function variables m,r
Step3: Copy the value of n to m
Step4: Start the while loop where(m!=0)
To find the value of result
Step5: End
Void display()
Step1: Start
Step2: To check whether (result=n),if then print “n is an Armstrong no”
Step3: Else print “n is not an Armstrong no”
Step4: End
Public static void main()
Step1: Start
Step2: Create an object ob
Step3: Call the function Void input(),Void calculate(),Void display()
Step4: End
Source code
import java.util.*;
public class Armstrong
{
int n,result=0;
void input()
{
Scanner ob=new Scanner(System.in);
System.out.println("ENTER THE NUMBER");
n=ob.nextInt();
}
void calculate() {
int m, r;
m =n;
while (m != 0)
{
r= m % 10;
result += Math.pow(r, 3);
m /= 10;
}}
void display()
{
if(result== n)
System.out.println(n + " is an Armstrong number.");
else
System.out.println(n + " is not an Armstrong number.");
}
public static void main()
{
Armstrong ob=new Armstrong();
ob.input();
ob.calculate();
ob.display();
}
}
Program 7
A class telcall calculates the monthly phone bill of a consumer some of the the class are given
below.
Class name
Data members/instance variable:
Phno: phone number
name: name.consumer
no : number of calls made amt: bill amount
Member function/methods:
void compute()
: parameterized Constructor to assign values to data members
: to calculate the phone bill amount based on slabs given below
void dispdata(): to display the details in the specified
Number of calls
1-100
101-200
201-300
Above 300
rate
Rs. 500/-rental charge only
Rs. 1.00/-per call + rental charge
Rs.1.20/-per call + rental charge
Rs. 1.50/-per call + rental charge
The calculations need to be done as per the slabs.
Specify the class telcall giving the details of all the functions.
In the main function create an object of type telcall and display the phone bill in the
following format:
Phone number
name
XXX
total calls
amount
Algorithm
Step1: Start
Step2: Initialization of instance variables n, phno,amt,name
Step3: End
Telecall(int a,int b,string s)
Step1: Start
Step2: Assign of values to instance variables
Step3: End
Void compute()
Step1: Start
Step2: Initialization of function variable m
Step3: To calculare value of m
Step4: Copy the value of m to amt
Step5: End
Void dipdata()
Step1: Start
Step2: Print “ phone no is phno”
Step3: Print “ Name is name”
Step4: Print “Total no of call is n”
Step5: Print “Amount is amt”
Step6: End
Public static void main()
Step1: Start
Step2: Create an object obj
Step3: Call the function Void compute(),Void dipdata()
Step4: End
Source code
import java.util.*;
class telecall
{
int phno,n;
double amt;
String name;
phno=a;
n=b;
name=s;
double m;
else
m=1120+(n-300)*1.5;
amt=m;
void dipdata()
System.out.println("Total Calls"+n);
System.out.println(" Amount"+amt);
}public static void main()
{
int a,b;
String s1;
Scanner ob=new Scanner(System.in);
System.out.println("enter name");s1=ob.nextLine();
System.out.println("enter your phone no");a=ob.nextInt();
System.out.println("enter total calls");b=ob.nextInt();
telecall obj=new telecall( a, b, s1);
obj.compute();
obj.dipdata();
}}
Program 8
An Emirp number is a number which is primebackwards and forwards. Example: 13 and 31
Design a class Emirp to check if a given number is Emirp number or not. Some of the
Class Name :
rev
Member functions:
Emirp
Emirp (intnn): to assign n = nn rev = 0 and f = 2 intisprime (int x): to check the number is
void is Emirp (): reverse the given number and check if both the
constructor(int), intisprime(int) and void Emirp(). Define main() function to create an object
int n,rev,f;
Emirp(int nn)
n=nn;
rev=0;
f=2; }
int isprime(int x)
if(n==x)
return 1;
return 0;
}
else
return isprime(x+1);
void isEmirp()
int x=n;
while(x!=0)
x=x/10; }
int ans1=isprime(f);
n=rev;
f=2;
int ans2=isprime(f);
else
{
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();