Computer Project
Computer Project
Write a program to accept a number and check whether it is a Zippo number or not
Ex-5003
(The number which contain zero other than in first and last place)
Algorithm
Step 1: Start
Step 2: Accept a number N
Step 3: Extract the right most digit in rd
Step 4: Calculate and store the sum of the digit in (S)
Step 5: Calculate the sum of the right most digit and the left most digit
S1=rd+ld
Step 6: Check whether S=S1 and rd!=0,ld!=0. If true go to the Step 7 else go to
Step 8
Step 7: Print N, is a Zippo number and go to Step 9
Step 8: Print N, is not a Zippo Number
Step 9: Stop.
Program:
import java.util.*;
class zippo
{
public static void main(String args [])
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter a Number");
int n=sc.nextInt();
int s1=0,s=0,ld=0,rd=0,a=n;
rd=n%10;
while(a>0)
{
ld=a%10;
s=s+ld;//calculating sum of digits
a=a/10;
}
s1=rd+ld;
//calculating sum of first and last digit
if((s==s1)&&(ld!=0)&&(rd!=0))
{
System.out.println(n+" is a Zippo Number");
}
else
{
System.out.println(n+" is not a Zippo Number");
}
}
}
Variable Description
Name Type Description
S Int To store the sum of the digits
S1 Int To store the sum of the first and last digit
ld Int To accept number from user
rd Int To store left most digit
N Int To store right most digit
Output:
Assignment 2
Question 2:
Write a java program to accept a number and convert it into its binary equivalent.
Example 23=10111
Algorithm:
Step 1: Start.
Step 2: Accept a number n.
Step 3: Declare and initialize the variables b= ”” , r=0;
Step 4: repeat the steps 5 to step 7 while n>0.
Step 5: set r=n%2.
Step 6: concatenate b=r+b.
Step 7: update n=n/2.
Step 8: print “Binary equivalent ”, b.
Step9: Stop
Program:
import java.util.*;
class binary
{
public static void main(String args [])
{
Scanner sc=new Scanner (System.in);
//accepting a number
System.out.println("Enter a number");
int n=sc.nextInt();
int r=0;String b="";
//calculating binary equivalent
while(n>0)
{
r=n%2;
b=String.valueOf(r)+b;
n=n/2;
}
System.out.println("Binary equivalent = "+b);
}
}
Variable Description
Output: