Computer Science, asked by wwwyashgudiyal, 4 months ago

Design a class to overload a method Number() as follows:
(i) void Number (int num , int d) - To count and display the frequency of a digit in a number.
Example: num = 2565685 d = 5 Frequency of digit 5 = 3
(ii) void Number (int n1) - To find and display the sum of even digits of a number.
Example: n1 = 29865 Sum of even digits = 16
Write a main method to create an object and invoke the above methods.
to perform the follow
user's choice:​

Attachments:

Answers

Answered by sanjeevanaravindan
5

Answer:

It's Below

Explanation:

public class Overload

{

   void Number(int num, int d) {

       int f = 0;

       

       while (num != 0) {

           int x = num % 10;

           if (x == d) {

               f++;

           }

           num /= 10;

       }

       

       System.out.println("Frequency of digit " + d + " = " + f);

   }

   

   void Number(int n1) {

       int s = 0;

       

       while (n1 != 0) {

           int x = n1 % 10;

           if (x % 2 == 0) {

               s = s + x;

           }

           n1 /= 10;

       }

       

       System.out.println("Sum of even digits = " + s);

   }

   

   public static void main(String args[]) {

       Overload obj = new Overload();

       obj.Number(2565685, 5);

       obj.Number(29865);

   }

}

Answered by dreamrob
0

Program in C++

#include<iostream>

using namespace std;

class Overload

{

public:

 void Number(int num, int d)

 {

  int count = 0;

  while(num != 0)

  {

   int digit = num % 10;

   if(digit == d)

   {

    count++;

   }

   num = num / 10;

  }

  cout<<"Frequency of the digit "<<d<<" = "<<count;

 }

 void Number(int n1)

 {

  int sum = 0;

  while(n1 != 0)

  {

   int digit = n1 % 10;

   if(digit % 2 == 0)

   {

    sum = sum + digit;

   }

   n1 = n1 / 10;

  }

  cout<<"Sum of even digits = "<<sum;

 }

};

int main()

{

Overload o;

int ch;

cout<<"Enter 1: To count and display the frequency of a digit in a number\nEnter 2: To find and display the sum of even digits of a number\nEnter your choice : ";

cin>>ch;

if(ch == 1)

{

 int num,d;

 cout<<"Enter number : ";

 cin>>num;

 cout<<"Enter a digit (0-9): ";

 cin>>d;

 if(d >= 0 && d <= 9)

 {

  o.Number(num , d);

 }

 else

 {

  cout<<"Invalid Digit";

 }

}

else if(ch == 2)

{

 int num;

 cout<<"Enter number : ";

 cin>>num;

 o.Number(num);

}

else

{

 cout<<"Invalid choice";

}

return 0;

}

Output 1:

Enter 1: To count and display the frequency of a digit in a number

Enter 2: To find and display the sum of even digits of a number

Enter your choice : 1

Enter number : 2565685

Enter a digit (0-9): 5

Frequency of the digit 5 = 3

Output 2:

Enter 1: To count and display the frequency of a digit in a number

Enter 2: To find and display the sum of even digits of a number

Enter your choice : 2

Enter number : 29865

Sum of even digits = 16

Output 3:

Enter 1: To count and display the frequency of a digit in a number

Enter 2: To find and display the sum of even digits of a number

Enter your choice : 1

Enter number : 23456

Enter a digit (0-9): 23

Invalid Digit

Output 4:

Enter 1: To count and display the frequency of a digit in a number

Enter 2: To find and display the sum of even digits of a number

Enter your choice : 4

Invalid choice

Similar questions