Thursday 4 August 2011

PROGRAMS 7



 PROGRAM
  To enter the first day of a month and the number of days
      in that month & print the list of Sundays in that month.

ALGORITHM
Step 1:Start
Step 2:Declare fd,nd,n[][]=new int[6][7].
Step 3:Make a void function input().
Step 4:Input no. of days & first day.
Step 5:Make a function calc()
Step 6:Generate a loop from 0 to 6 in a.
Step 7:Generate a loop from 0 to 7 in b.
Step 8:If a==0 and b==fd-1, c++.
Step 9:If c>0 and c<=nd, n[a][b]=c++.
Step 10:Make the main() function.
Step 11:Call input() and calc().
Step 12:Print the dates that are at 7th column & is greater than zero.
Step 13:End

PROGRAM
import java.util.*;
class CalcSun
{
Scanner ob=new Scanner(System.in);
int fd,nd,n[][]=new int[6][7];
void input()throws Exception


 {
System.out.println("Enter no.of days in the month:");
nd=ob.nextInt();
System.out.println("Enter I day's no.if 1 is for monday:");
fd=ob.nextInt();
}
void calc()
{
int a,b,c=0;
for(a=0;a<6;a++)
{
for(b=0;b<7;b++)
{
if(a==0&&b==fd-1)
c++;
if(c>0&&c<=nd)
n[a][b]=c++;
}
}
}
void main()throws Exception
{
input();
calc();
System.out.println("List of sundays is:");
for(int h=0;h<6;h++)
if(n[h][6]!=0)

System.out.println(n[h][6]);
}
}


   
VARIABLE DESCRIPTION
Variable
Type
Use
a
Integer
Loop variable
b
Integer
Loop variable
c
Integer
Counter
n[][]
Integer
Array to store calendar
nd
Integer
Store no of days
fd
Integer
Store first day
h
Integer
Loop variable


INPUT & OUTPUT:
                             


                   




                        
** PROGRAM**
  Q.To enter the number of rows & then print the matrix of that
      order in the following form-
                                       1        2       3       4
                                     12 13     14     5
                                    11 16     15     6
                                   10  9       8       7

ALGORITHM
Step 1:Start
Step 2:Enter tha no of rows/column 'n' that is odd.
Step 3:Create a 2-D array a[][] of rows & columns n.
Step 4:Assign the central element as 0.
Step 5:Assign ar[0][0]=1.
Step 6:Declare c=0,r=0,d=1,v=n.
Step 7:Make a loop to end when central element is not 0(Step 8 to 19).
Step 8:Make a loop until c<v.
Step 9:In loop,a[r][c]=++d & c++.
Step 10:After loop,c-- & r++.
Step 11:Make a loop until r<v.
Step 12:In loop,a[r][c]=++d & r++.
Step 13:After loop,c-- & r--.
Step 14:Make a loop until c>=n-v.
Step 15:In loop,a[r][c]=++d & c--.
Step 16:After loop,c++,v-- & r--.
Step 17:Make a loop until r>=n-v.
Step 18:In loop,a[r][c]=++d & r--.

Step 19:After loop,c++ & r++.
Step 20:Print the array.
Step 21:End.


PROGRAM
class Spiral
{
public static void main()
{
int n;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("ENTER THE NUMBER");
n=Integer.parseInt(br.readLine());
int a[][]=new int[n][n],c=0,r=0,d=1,v=n;
a[r][c]=1;
a[n/2][n/2]=0;
for(c=1;a[n/2][n/2]==0;c++,r++)
{
System.out.println("IF THE INPUT IS "+n+" THEN OUTPUT IS");
while(c<v)
{
a[r][c]=++d;
c++;
}
c--;r++;
while(r<v)

{
a[r][c]=++d;
r++;
}
c--;r--;
while(c>=n-v)
{
a[r][c]=++d;
c--;
}
v--;c++;r--;
while(r>=n-v)
{
a[r][c]=++d;
r--;
}
}
for(r=0;r<n;r++)
{
for(c=0;c<n;c++)
System.out.print(a[r][c]+"\t");
System.out.println();
}
}
}

               


40
VARIABLE DESCRIPTION
Variable
Type
Use
a[][]
Integer
Array to store numbers
r
Integer
To store row no.
c
Integer
To store column no.
d
Integer
To store current number
n
Integer
store array size
v
Integer
Counter



   

INPUT & OUTPUT

                               




**PROGRAM**
Q.To print each digit of an entered number in words, and that too in reverse order. You have to use recursion in your program

ALGORITHM
Step 1:Start.
Step 2:Declare int n.
Step 3:Make default constructor to initialize n=0.
Step 4:Make a void function inpnum() to enter value of n.
Step 5:Make a void function extDigit() to input a number a.
Step 6:If a<=9,call NumToWords(a).
Step 7:Else call NumToWords(a%10) & call extDigit(a/10)
Step 8:Make function NumToWords() to input a number.
Step 9:Using switch case,check the input number for 0 to 9.
Step 10:Correspondingly print its number name
Step 11:Make main() function & call inpNum() & extDigit().
Step 12:End.

PROGRAM
import java.util.*;
class Convert
{
int n;
Convert()
{
n=0;
}

public void inpNum()throws Exception
{
Scanner ob=new Scanner(System.in);
System.out.println("Enter any number:");
n=ob.nextInt();
}
public void extDigit(int a)
{
if(a>9)
{
NumToWords(a%10);
extDigit(a/10);
}
else
NumToWords(a);
}
public void NumToWords(int x)
{
String s="";
switch(x)
{
case 1:
s="one";
break;
case 0:
s="zero";
break;
case 2:

s="two";
break;
case 3:
s="three";
break;
case 4:
s="four";
break;
case 5:
s="five";
break;
case 6:
s="six";
break;
case 7:
s="seven";
break;
case 8:
s="eight";
break;
case 9:
s="nine";
break;
}
System.out.println(s);
}
public void main()throws Exception
{

inpNum();
extDigit(n);
}
}

VARIABLE DESCRIPTION
Variable
Type
Use
a
Integer
Store Location
n
Integer
Store Number
s
String
Store Number Name

INPUT & OUTPUT

                                                 





**PROGRAM**
Q.To input no of rows & print the spiral matrix as shown-
                     1
              2     5     4
                     3

ALGORITHM
Step 1:Start.
Step 2:Enter tha no of rows/column 'n' that is odd.
Step 3:Create a 2-D array ar[][] of rows & columns n.
Step 4:Assign the central element as 0.
Step 5:Assign q=n-1,r=0,c=n/2,d=1 & p=0.
Step 6:Generate a loop until central element is zero.
Step 7:Make another loop in it until r<=q.
Step 8:ar[r][c]=d++;
Step 9:Increment r & check whether it is less than or equal to n/2.
Step 10:If true,c-- else c++.
Step 11:End inner loop and r-=2,p++.
Step 12:Make another loop until r>=p.
Step 13:ar[r][c]=d++;
Step 14:Decrement r & check whether it is less than n/2.
Step 15:If true,c-- else c++.
Step 16:End inner loop & r+=1,p-=1.




Step 17:End outer loop.
Step 18:Print the array elements if they are not zero.
Step 19:End.



PROGRAM
class Circular
{
public static void main()
{
int n;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("ENTER THE NUMBER");
n=Integer.parseInt(br.readLine());
int ar[][]=new int[n][n];
int r=0,c=n/2,d=1,p=0,q=n-1;
ar[c][c]=0;
System.out.println("IF THE INPUT IS "+n+" THEN OUTPUT IS");
while(ar[n/2][n/2]==0)
{
while(r<=q)
{
ar[r][c]=d++;
r++;
if(r<=n/2)
c--;

else
c++;
}
r-=2;
p++;
while(r>=p)
{
ar[r][c]=d++;
r--;
if(r<n/2)
c--;
else
c++;
}
r+=1;
q-=1;
}
for(r=0;r<n;r++)
{
for(c=0;c<n;c++)
{
if(ar[r][c]!=0)
System.out.print(ar[r][c]+"\t");
else
System.out.print("\t");
}
System.out.println();
}
}}
VARIABLE DESCRIPTION
Variable
Type
Use
ar[][]
Integer
Array to store no.
n
Integer
To store array length
r
Integer
To store row no.
c
Integer
To store column no.
d
Integer
Counter
p
Integer
Temporary
q
Integer
Temporary

INPUT & OUTPUT
                                                                                                                                         
                                          







**PROGRAM**

 Q.To print the prime factors of a number.

ALGORITHM
Step 1:Start.
Step 2:Make a function prime() to accept a value & return integer.
Step 3:Return the next prime after the input number.
Step 4:Make function main() that accepts a value.
Step 5:Print n+"=".
Step 6:Generate loop with starting value of a=1,b=2 & condition that a is greater than 1.
Step 7:If a%b=0, divide a by b and print b.
Step 8:Else b=prime(b).
Step 9:End.

PROGRAM
class PrimeFac
{
public static void main()
{
int n,a,b=2;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("ENTER THE NUMBER");
System.out.println("IF THE INPUT IS "+n+" THEN OUTPUT IS");
n=Integer.parseInt(br.readLine());

System.out.print(n+"=");
for(a=n;a>1;)
{
if(a%b==0)
{
a/=b;
System.out.print(b+"*");
}
else
b=prime(b);
}
}
public static int prime(int c)
{
int a,b=0,d,e=0;
for(a=c+1;b==0;a++)
{
e=0;
for(d=1;d<=a;d++)
{
if(a%d==0)
e++;
}
if(e==2)
b++;
}
return a-1;
}
}



VARIABLE DESCRIPTION
Variable
Type
Use
a
Integer
Loop variable
b
Integer
Counter
d
Integer
Next no. to be searched
e
Integer
No. of factors
n
Integer
Store number

INPUT&OUTPUT
                                                
                             



**PROGRAM**
Q.To enter the total no. of days in month & the first day of the month and print the calendar of the month.

ALGORITHM
Step 1:Start
Step 2:Declare an array or 6 rows & 7 columns.
Step 3:Make a void function input() to input the no of days in month & first day.
Step 4:Make funtion void calc()
Step 5:Make a loop from 0 to 5 in a.
Step 6:Make a loop from 0 to 6 in b.
Step 7:If a==0 and b==fd-1,c++.
Step 8:If c>0 and c<=nd,n[a][b]=c++.
Step 9:End both loops.
Step 10:Make the main() function.
Step 11:Print the names of days in one row.
Step 12:Print those elements of array that aare not 0.
Step 13:End

PROGRAM
import java.util.*;
class Calender
{

Scanner ob=new Scanner(System.in);                           
int fd,nd,n[][]=new int[6][7];
void input()throws Exception
{
System.out.println("Enter no.of days in the month:");
nd=ob.nextInt();
System.out.println("Enter I day's no.if 1 is for monday:");
fd=ob.nextInt();
}
void calc()
{
int a,b,c=0;
for(a=0;a<6;a++)
{
for(b=0;b<7;b++)
{
if(a==0&&b==fd-1)
c++;
if(c>0&&c<=nd)
n[a][b]=c++;
}
}
}
void main()throws Exception
{
String s[]={"MON","TUE","WED","THU","FRI","SAT","SUN"};
input();
calc();
for(int y=0;y<7;y++)
System.out.print(s[y]+"\t");
System.out.println();
for(int h=0;h<6;h++)
{
for(int t=0;t<7;t++)
{
if(n[h][t]!=0)
System.out.print(n[h][t]);
System.out.print("\t");
}
System.out.println();
}
}

VARIABLE DESCRIPTION
Variable
Type
Use
a
Integer
Loop variable
b
Integer
Loop variable
c
Integer
Counter
h
Integer
Loop Variable
t
Integer
Loop Variable
n[][]
Integer
Array to store dates
y
Integer
Loop variable

 


INPUT & OUTPUT


                                           



**PROGRAM**
Q.To enter a number & print the pair of twin primes that   
    lies closest to the given number.

ALGORITHM
Step 1:Start.
Step 2:Make a function prime() that checks if a number is prime or not.
Step 3:If it is prime, it should return true else false.
Step 4:Make main() & enter n.
Step 5:If n is prime & n+2 is prime,print them & goto step .
Step 6:If n-1 & n+1 are both prime print them & goto step .
Step 7:Generate a loop from 1 to infinite in c,incrementing it by 1.
Step 8:If n-c+2 & n-c both are prime, print them & break the loop.
Step 9:If n+c & n+c+2 both are prime,print them & break the loop.
Step 10:End.

PROGRAM
class Twin
{
public static boolean prime(int n)
{
int a,b=0;
for(a=1;a<=n;a++)
{
if(n%a==0)
b++;
}

if(b==2)
return true;
else
return false;
}
public static void main(int n)
{
int a,b,c;
if(prime(n)&&prime(n+2))
System.out.println("P1="+n+" P2="+(n+2));
else if(prime(n-1)&&prime(n+1))
System.out.println("P1="+(n-1)+" P2="+(n+1));
else
{
for(c=1;;c++)
{
a=n-c;
b=n+c;
if(prime(a)&&prime(a+2))
{
System.out.println("P1="+a+" P2="+(a+2));
break;
}
if(prime(b)&&prime(b+2))
{
System.out.println("P1="+b+" P2="+(b+2));
break;
}

}
}
}
}

VARIABLE DESCRIPTION
Variable
Type
Use
a
Integer
Loop variable
b
Integer
Counter
c
Integer
Loop variable
n
Integer
To store entered number


INPUT & OUTPUT:
                                               
           

** PROGRAM**
Q.A class ISCScores defines the scores of a candidate in 6 subjects &   another class Best4 defines the best four subjects.The details of
        both classes are:
       Class Name - ISCScores
      Data Mmebers-
      int n[6][2]-  to store marks of six subject and code(numeric)
     Member Methods-
    ISCScores( ) - constructor to accept the marks
    int point( ) - to return the point in each subject according
                     to the  following-
                    Marks >= 90 - 1
                    80-89 - 2
                   70-79 - 3
                   60-69 - 4
                  50-59 - 5
                  40-49 - 6
                 below 40 – 7

   Class Name -Best4
   Member Methods-
void Bestsubject( ) - to display the total points & best subject using the concept of inheritance.

 ALGORITHM
Step 1:Start
Step 2:In ISCScores class, declare an array of 6 rows & 2 columns.
Step 3:In the constructor ISCScores(),accept marks in subjects & store in I column.
Step 4:Make an integer returning function Point to accept a no. x.
Step 5:Check n[x][0] for the range in which it lies & return proper grade.
Step 6:Make a class Best4 extending ISCScores.
Step 7:In constructor Best4(),call ISCScores().
Step 8:Make a function BestSubjects().
Step 9:Using bubble sort,arrange the array in descending order.
Step 10:Make a loop from 0 to 5.
Step 11:For each element,call the function Point() & store points in II column.
Step 12:Print the total of I column as total marks.
Step 13:Print the first 4 rows of II column.
Step 14:These will be the best 4 points.
Step 15:End.Step 10:Make the main() function.
Step 11:Print the names of days in one row.
Step 12:Print those elements of array that are not 0.
Step 13:End

PROGRAM
import java.util.*;
class ISCScores
{
Scanner ob=new Scanner(System.in);
int n[][]=new int[6][2];

ISCScores()throws Exception
{
for(int y=0;y<6;y++)
{
System.out.println("Enter marks:");
n[y][0]=ob.nextInt();
}
}
int Point(int x)
{
if(n[x][0]>=90)
return 1;
else if(n[x][0]>=80)
return 2;
else if(n[x][0]>=70)
return 3;
else if(n[x][0]>=60)
return 4;
else if(n[x][0]>=50)
return 5;
else if(n[x][0]>=40)
return 6;
else
return 7;
}
}

import java.util.*;
class Best4 extends ISCScores
{
Scanner ob=new Scanner(System.in);
public Best4()throws Exception
{
super();
}
void BestSubjects()
{
int a,b,c,s=0;
for(a=0;a<6;a++)
{
for(b=0;b<5-a;b++)
{
if(n[b][0]<n[b+1][0])
{
c=n[b+1][0];
n[b+1][0]=n[b][0];
n[b][0]=c;
}
}
}
for(a=0;a<6;a++)
{
s+=n[a][0];
n[a][1]=Point(a);
}
System.out.println("Total marks="+s);
System.out.println("Top 4 points are:");
for(a=0;a<4;a++)
System.out.println(n[a][1]);
}
}


VARIABLE DESCRIPTION
Variable
Type
Use
n[]
Integer
Array to store marks
y
Integer
Loop variable
x
Integer
Counter
a
Integer
Loop Variable
b
Integer
Loop Variable
c
Integer
Temporary Variable
S
Integer
Store sum
   


INPUT & OUTPUT
                           
                     

        





**PROGRAM**
Q.The distance between two points(x1,y1) and (x2,y2) is given by:
((x1-x2)^2+(y1-y2)^2)^1/2
  To determine if a point lies within a given circle the distance of the point from the centre of the circle is computed first and then this distance is compared with the radius of the circle.
A class Point defines the coordinates of a point while another class Circle represents the radius and centre of a circle. The details of both the classes
  are given below:
  Class Name-Point
 Data Members-
 double x, y-to store the x and y coordinates
 Member Methods-
 Point( )-constructor to assign 0 to x and y
 void readPoint( )-reads the coordinates of a point
 void displayPoint( )-displays the coordinates of a point

 Class Name-Circle
 Data Members-
 double r-to store the radius
 Point o-to store the coordinates of the centre
 Member Methods-
 Circle( )-constructor
 void readCircle( )-reads the radius and centre of a circle
 void displayCircle( )-displays the radius and centre of a circle

int contains(Point p)-returns 1 if the Point p lies within the
                        current circle object, otherwise returns 0
WAP to specify the class Point and Circle giving the details of all the above methods.

ALGORITHM
Step 1:Start
Step 2:In class Point1,declare double x & y.
Step 3:In constructor Point1,assign 0 to x & y.
Step 4:In readPoint1() function,enter the value of x & y.
Step 5:In displayPoint1(),display x & y.
Step 6:Make a class Circle that extends Point1.
Step 7:Declare a Point1 object o and double r.
Step 8:Make an unparameterized constructor to give r=0 & call Point1().
Step 9:Also o=new Point1().
Step 10:Make a function readCircle() to accept radius of circle & coordinates.
Step 11:Make a function displayCircle(),display radius & coordinates.
Step 12:Make an int returning function contains() accepting a Point1 object p.
Step 13:Calculate the distance between points o and p.
Step 14:If d<=r,return 1 else 0.
Step 15:End.

                 



import java.util.*;
class Point1
{
double x,y;
public Point1()
{
x=y=0.0;
}
public void readPoint1()
{
Scanner sc=new Scanner(System.in);
x=sc.nextDouble();
y=sc.nextDouble();
}
public void displayPoint1()
{
System.out.println("The coordinates are :"+x+","+y);
}
}
import java.util.*;
class Circle extends Point1
{
double r;
Point1 o;
Circle()
{
super();

r=0.0;
o=new Point1();
}
public void readCircle()throws Exception
{
Scanner sc1=new Scanner(System.in);
System.out.println("Enter the radius & coordinates of circle");
r=sc1.nextDouble();
o.readPoint1();
}
public void displayCircle()
{
System.out.println("radius= "+r+"&  centre of circle = ");
o.displayPoint1();
}
public int Contains(Point1 p)
{
double d=Math.sqrt(Math.pow((o.x-p.x),2)+Math.pow((o.y-p.y),2));
if(d<=r)
return 1;
else
return 0;
}
public void main()throws Exception
{
readCircle();
displayCircle();
Point1 ob1=new Point1();
System.out.println("Enter coordinates of circle");
ob1.readPoint1();
int n=Contains(ob1);
if(n==1)
System.out.println("Point lies");
else
System.out.println("Point not lies");
}
}



               VARIABLE DESCRIPTION
Variable
Type
Use
x
Integer
x coordinate
y
Integer
y coordinate
r
Integer
Store radius
d
Integer
Store distance
n
Integer
Temporary

      







                       
                









          



**PROGRAM**
Q.A Class Student defines personal data of Student while another class marks defines register no., name of subject , & marks obtained by the
    Student:
    Class Name-Student
    Data Members-
    age-integer value
    name[ ],sex[ ]-array of characters
  Member Methods-
  void inpdetails( )-to accept values for data members
  void show1( )-to display the details

  Class Name-Marks
  Data Members-
  regnum,mark-integer type
  Member Methods-
  void inpdetails( )-to accept values
  void show2( )-to display employee details
  Specify the above classes using concept of inheritance

ALGORITHM
Step 1:Start
Step 2:In Students class,declare int age.
Step 3:Also declare two char arrays name[],sex[].
Step 4:Make void function inpDetails1()

Step 5:Enter the values of age,name & sex from user.
Step 6:Make a void function show1()
Step 7:Print the name,age & sex.
Step 8:In class Marks,extending Students,declare registration no,subject & marks.
Step 9:Make void function inpDetails2()
Step 10:Enter the values of subject,marks & registration no.
Step 11:Make a void function show2()
Step 12:Print the subject,its marks & registration no.
Step 13:In main function,call all the four functions.
Step 14:End.

PROGRAM
import java.util.*;
class Students
{
Scanner ob=new Scanner(System.in);
int age;
char name[],sex[];
void inpDetails1()throws Exception
{
System.out.println("Enter name,sex & age:");
String n=ob.nextLine();
String s=ob.nextLine();
age=ob.nextInt();
name=new char[n.length()];
sex=new char[s.length()];
name=n.toCharArray();

sex=s.toCharArray();
}
void show1()
{
int y;
System.out.print("Name:");
for(y=0;y<name.length;y++)
System.out.print(name[y]);
System.out.print("\nSex:");
for(y=0;y<sex.length;y++)
System.out.print(sex[y]);
System.out.println("\nAge:"+age);
}
}

import java.util.*;
class Marks extends Students
{
char sub[];
int regnum,mark;
Scanner ob=new Scanner(System.in);
void inpDetails2()throws Exception
{
System.out.println("Enter the subject:");
String s=ob.nextLine();
System.out.println("Enter registration number & marks:");
regnum=ob.nextInt();
mark=ob.nextInt();

sub=new char[s.length()];
sub=s.toCharArray();
}
void show2()
{
System.out.println("Registration Number:"+regnum);
System.out.print("Subject:");
for(int g=0;g<sub.length;g++)
System.out.print(sub[g]);
System.out.println("\nMarks:"+mark);
}
void main()throws Exception
{
inpDetails1();
inpDetails2();
show1();
show2();
}
}

VARIABLE DESCRIPTION
Variable
Type
Use
age
Integer
Store age
name[]
Character
Store name
sex[]
Character
Store sex
sub[]
Character
Store subject
mark
Integer
Store Marks
g
Integer
Loop variable
regnum
Integer
Store registration no.

INPUT & OUTPUT
                      









**PROGRAM**
Q.To enter year and the no. of days passed in that year. The program should print the date of the current day. You may assume
 no. of days is less than 365.

ALGORITHM
Step 1:Start
Step 2:Enter no. of days passed & year.
Step 3:Make an array a[12] whose each element is the no. of days x as are there in that month.
Step 4:Check if year is divisible by 100 & 400,then a[1]=29.
Step 5:Else if year is divisible by 4,a[1]=29.
Step 6:Take t=0
Step 7:Generate loop in b from 0 to 12
Step 8:If t+a[b]>=x,then r=x-t & break.
Step 9:else t=t+a[b].
Step 10:End loop.
Step 11:Print date r,month b+1,year y.
Step 12:End.

PROGRAM
class Day
{
public static void main(int x,int y)
{
int t=0,r=0,b=0;
int a[]={31,28,31,30,31,30,31,31,30,31,30,31};

if(y%100==0 && y%400==0)
a[1]=29;
else if(y%4==0)
a[1]=29;
for( b=0;b<12;b++)
{
if(t+a[b]>=x)
{
r=x-t;
break;
}
else
t=t+a[b];
}
System.out.println("Date is "+r+"-"+(b+1)+"-"+y);
}
}

VARIABLE DESCRIPTION
Variable
Type
Use
t
Integer
Temporary
b
Integer
Loop variable
a[]
Integer
Array to store no. of days in month
r
Integer
Date
y
Integer
Year
x
Integer
No. of days





INPUT & OUTPUT
                         









**PROGRAM**
Q.Write a program to enter a number n and print the nth prime.
  Also the function should print all perfect numbers below the nth prime.

ALGORITHM
Step 1:Start
Step 2:Make a function nthPrime() to input an integer.
Step 3:Generate a loop from 1 to zero & take c=1(Step 4 to 5).
Step 4:From c+1,check for any prime number and as you get it end the loop.
Step 5:After loop, c=a-1.
Step 6:After outer loop, return c.
Step 7:Make a void function PNB() to enter a number.
Step 8:Call the function nth prime & print the returned value.
Step 9:Check for every number from 1 to nth prime.
Step 10:If the sum of factors of the number is equal to the number print that it is perfect.
Step 11:Also print the factors.
Step 12:End.

PROGRAM
class NumProb
{
int nthPrime(int n)
{
int a,b=0,c,d,e=0,f;

for(c=1,f=1;f<=n;f++)
{
b=0;
for(a=c+1;b==0;a++)
{
e=0;
for(d=1;d<=a;d++)
if(a%d==0)
e++;
if(e==2)
b++;
}
c=a-1;
}
return c;
}
void PNB(int n)
{
int h=nthPrime(n),a,b,c=0;
System.out.println(n+"th prime no="+h);
for(a=1;a<h;a++)
{
c=0;
for(b=1;b<a;b++)
if(a%b==0)
c+=b;
if(a==c)
{

System.out.print(a+"=(1");
for(c=2;c<a;c++)
if(a%c==0)
System.out.print(","+c);
System.out.println(")");
}
}
}
}

VARIABLE DESCRIPTION
Variable
Type
Use
a
Integer
Loop variable
b
Integer
Loop variable
c
Integer
Loop variable
n
Integer
nth prime
f
Integer
Loop variable

 








INPUT &  OUTPUTS
                                   
                            
         

               







**PROGRAM**
Q.A complex number c=x+iy is represented by a pair of real numbers, where x is the real part & y is the imaginary part. Addition and     multiplication of two complex numbers C1=(x1,y1) and C2=(x2,y2)
are defined as-
C1+C2 = (x1+x2,y1+y2)
C1C2 = (x1x2-y1y2,x1y2+x2y1)
     A class Complex has following details:
    Class Name-Complex
   Data Members-
    float x – to store real part
   float y – to store imaginary part.
  Member Functions-
  Complex() – constructor
  void readComplex() – reads a complex number
  void showComplex() – displays a complex number
  void addComplex(Complex a, Complex b) – to add a & b
  Complex mulComplex(Complex b) – Multiply b and current object
  Specify the class And all the above described functions.

ALGORITHM
Step 1:Start
Step 2:Declare float variables x & y.
Step 3:Make an unparameterized constructor to initialize 0 to x & y.

Step 4:Make a function readComplex() to read a complex number from user.
Step 5:Make a void function showComplex() to display the current complex.
Step 6:Make a void function addComplex() that inputs two Complex objects.
Step 7:x=a.x+b.x
Step 8:y=a.y+b.y
Step 9:Make a Complex returning function mulComplex() that accepts a Complex object.
Step 10:Make a Complex object obj.
Step 11:obj.x=x*b.x-y*b.y
Step 12:obj.y=x*b.y+b.x*y
Step 13:Return obj.
Step 14:End

PROGRAM
import java.util.*;
class Complex
{
float x,y;
Scanner ob=new Scanner(System.in);
Complex()
{
x=y=0;
}
void readComplex()throws Exception
{

System.out.println("Enter real part:");
x=ob.nextFloat();
System.out.println("Enter imaginary part:");
y=ob.nextFloat();
}
void showComplex()
{
System.out.println(x+"+"+y+"i");
}
void addComplex(Complex a,Complex b)
{
x=a.x+b.x;
y=a.y+b.y;
}
Complex mulComplex(Complex b)
{
Complex obj=new Complex();
obj.x=x*b.x-y*b.y;
obj.y=x*b.y+b.x*y;
return obj;
}
public void main()throws Exception
{
Complex ob1=new Complex();
Complex ob2=new Complex();
Complex ob3=new Complex();
ob1.readComplex();
ob2.readComplex();

addComplex(ob1,ob2);
System.out.print("Their sum is ");
showComplex();
ob3=ob1.mulComplex(ob2);
System.out.print("Product is ");
ob3.showComplex();
}
}

VARIABLE DESCRIPTION
Variable
Type
Use
x
Integer
Real part
y
Integer
Imaginary part
a
Complex
Complex object
b
Complex
Complex object

INPUT&OUTPUT