English

Define a class to overload the function print as follows: void print() to print the following format 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 void print(int n)

Advertisements
Advertisements

Question

Define a class to overload the function print as follows:

void print() to print the following format
  1 1 1 1
  2 2 2 2
  3 3 3 3
  4 4 4 4
  5 5 5 5
void print(int n) To check whether the number is a lead number. A lead number is one whose sum of even digits is equal to the sum of odd digits.
 

e.g. 3669 odd digits sum = 3 + 9 = 12
                even digits sum = 6 + 6 = 12
3669 is a lead number.

Code Writing
Advertisements

Solution

public class Printclass
{
   //instance variables - replace the example below with your own public void print() 
{
    for(int i=1;i<=5;i++)
       {for(int j=1;j<=5;j++)
           System.out.print(i+" ");
       System.out.println("\n");}
}
public void print(int n)
{
   int se=0,d=0,so=0; 
   while (n>0)
   {
       d=n%10;
       if (d%2=0)
           se+=d;
       else
            so+=d;
        n/=10;
   } 
   if (se==so)
       System.out.println("Lead number");
   else
   System.out.println("Not a Lead number");
}
public static void main(String [] args)
{
     Printclass ob=new Printclass();
     ob. print(); 
     ob.print(1542);
  }
}
shaalaa.com
  Is there an error in this question or solution?
Chapter 1: Revision of Class 9 Syllabus - Exercises [Page 50]

APPEARS IN

Rupa Pandit Computer Applications [English] Class 10 ICSE
Chapter 1 Revision of Class 9 Syllabus
Exercises | Q 17. | Page 50
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×