Page 415 - Computer science 868 Class 12
P. 415

The output of the preceding program is as follows:


























                  Program 7      A class called Addup is defined to find the sum of positive integers which addup to form a
                                 given number. For example, if the number is 4 then the output will be:
                                 4
                                 3 1
                                 2 2
                                 2 1 1
                                 1 1 1 1
                                 The class description is given below.
                                 Class name                     :  Addup
                                 Data Members
                                 int n                          :  To store number
                                 Member Methods
                                 void accept()                  :  Accept any number ‘n’
                                 void display(int a, int g, String s)  :   Recursive method used to calculate and display sum of
                                                                   all natural numbers which add up to ‘n’
                                 void call()                    :   Calls  display  (int,int,String)  to  perform the  above
                                                                   operation
                                 static void main()             :   Creates object and calls other methods to perform the
                                                                   above operations

                   1      import java.util.*;

                   2      public class Addup {
                   3        int n;

                   4        void accept() // input number
                   5        { Scanner sc=new Scanner(System.in);

                   6         System.out.println("Enter number");
                   7         n=sc.nextInt();







                                                                                                                       413
                                                                                                           Recursion   413
   410   411   412   413   414   415   416   417   418   419   420