0% found this document useful (0 votes)
38 views

Program 6 Algorithm: Void Input

The document describes a program to check if a number is an Emirp number. An Emirp number is a prime number that remains prime when its digits are reversed. The program defines an Emirp class with data members to store the number, its reverse, and a divisor. The Emirp constructor initializes these members. The isprime method checks if a number is prime, and isEmirp method reverses the number, checks if original and reversed forms are both prime, and prints the result. The main method creates an Emirp object and calls its methods to check if the input number is an Emirp.

Uploaded by

RINKU AGRAWAL
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Program 6 Algorithm: Void Input

The document describes a program to check if a number is an Emirp number. An Emirp number is a prime number that remains prime when its digits are reversed. The program defines an Emirp class with data members to store the number, its reverse, and a divisor. The Emirp constructor initializes these members. The isprime method checks if a number is prime, and isEmirp method reverses the number, checks if original and reversed forms are both prime, and prints the result. The main method creates an Emirp object and calls its methods to check if the input number is an Emirp.

Uploaded by

RINKU AGRAWAL
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Program 6

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;

public telecall(int a,int b,String s)

phno=a;

n=b;

name=s;

public void compute()

double m;

if(n<=100) m=500; else if(n>100&&n<=200) m=500+(n-100)*1; else if(n>200&&n<=300)


m=1000+100+(n-200)*1.2;

else

m=1120+(n-300)*1.5;

amt=m;

void dipdata()

System.out.println("Phone number "+phno);

System.out.println(" Name "+name);

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

are both prime numbers. Thus 13 is an Emirp number.

Design a class Emirp to check if a given number is Emirp number or not. Some of the

members of the class are given below:

Class Name :

Data Members/ instance variables:

rev

Member functions:

Emirp

: stores the number

: stores the reverse of the number

: stores the divisor

Emirp (intnn): to assign n = nn rev = 0 and f = 2 intisprime (int x): to check the number is

primeand return 1 if prime otherwise return 0.

void is Emirp (): reverse the given number and check if both the

original number and the reverse number are prime,

by invoking the function isprime(int) and display


result with an appropriate message. Specify the class Emirp giving details of the

constructor(int), intisprime(int) and void Emirp(). Define main() function to create an object

and call the methods to check for Emirp


Algorithm
Step1: Start
Step2: Initialize the instance variables n,rev,f
Step3: End
EMIRP(INT NN)
Step1: Start
Step2: Assign values to instance variables
Step3: End
Is prime(int x)
Step1: Start
Step2: To check whether(n=x),if then return 1
Step3: Else if(n%x=0//n==1),if then return 0
Step4: Else isprime(x+1)
Step5 :End
Void isemirp()
Step1: Start
Step2: Copy the value of x to n
Source code

import java.util. Scanner;

public class Emirp

int n,rev,f;

Emirp(int nn)

n=nn;

rev=0;

f=2; }

int isprime(int x)

if(n==x)

return 1;

else if (n%x==0 ||n == 1)

return 0;
}

else

return isprime(x+1);

void isEmirp()

int x=n;

while(x!=0)

{ rev=(rev* 10) + x%10;

x=x/10; }

int ans1=isprime(f);

n=rev;

f=2;

int ans2=isprime(f);

if(ans1==1 && ans2==1)

System. out.println(n+" is anEmirp number");

else

System.out.println(n+" is not an Emirp number"); }

public static void main()

{
Scanner sc=new Scanner(System.in);

System.out.println("\n Enter a number");

int x=sc.nextInt();

Emirp obj = new Emirp(x); obj.isEmirp();

You might also like