Sunday, February 6, 2011

Smith Number And BlueJ Program

What is a Smith number



It is a composite number in which, the sum of its digits is equal to the sum of the digits in its prime factors. In 378 the sum of the digits is 3+7+8=18. The prime factors of 378 are 2, 3, 3, 3, 7. Here all the prime factors are single digit numbers and their sum is 2+3+3+3+7-18. So 378 is a
smith number. Again if we take the case of 22, the sum of the digits is 2+2=4 and the prime factors are 2 and 11. Since 11 is a two digit number, the sum of the digits of the factors is 2+1+1=4. So 22 is a Smith number.

The first few Smith numbers are: 4, 22, 27, 58, 85, 94, 121, 166, 202, 265, 274, 319, 346,
 355, 378, 382, 391, 438, 454, 483, 517, 526, 535, 562,  576, 588, 627, 634, 636, 645, 648, 654, 663, 666, 690, 706, 728, 729, 762, 778, 825, 852, 861, 895, 913, 915,  922, 958, 985, 1086

 How to proceed on this program to check Smith number

Firstly sum of the digits of the entered number is to be stored in a variable. Next step is to find out the factors and checking the numbers whether they are prime or not. If found prime factor then the sum of the digits of the factor number is stored in another counter variable and the same process is followed for all prime factor numbers. Whenever any prime factor is found, the original entered number is tried with the same number and the original number is divided by the prime factor number.

 Codes of the BlueJ program on Smith number checking

 import java.io.*;
class smith1
{
 int no,n,j,sum=0,sumfactors=0,i;
 boolean bool;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void takeNumber() throws Exception
{
 System.out.println("Enter the number:");
n=Integer.parseInt(br.readLine());
no=n;
sum=sumOfDigits(n);
for(i=1;i< =n;i++)
{
if(n%i==0)
{
bool=prime(i);
if(bool)
{
j=i;
System.out.print(" "+j);
 while(j >0)
 {
  sumfactors=sumfactors+j%10;
  j=j/10;
  }
  n=n/i;
  i--;
}
}
}
if(sum==sumfactors)
System.out.println(no+ " is a smith number.");
else
System.out.println(no+ " is not a smith number.");
}
private int sumOfDigits(int n)
{
 int s=0;
 while(n >0)
 {
  s=s+n%10;
  n=n/10;
 }
 return s;
}
private boolean prime(int n)
{
 int i;
 for(i=2;i< n;i++)
 {
  if(n%i==0)
  break;
 }
 if(i==n)
 return true;
 else
 return false;
 }
 public static void main(String args[])throws Exception
 {
  smith1 obj=new smith1();
  obj.takeNumber();
  }
  }


 Technical analysis of the Smith number checking program

Two variables ‘sum’ and ‘sumfactors’ are used in this program to store sum of the digits of the entered number and sum of the digits of the prime factors of the entered number. Three functions are defined in this program to perform the job of checking Smith number. Function ‘takeNumber()’ takes the number from user, invokes the other function ‘int sumOfDigits()’ which returns the sum of the digits and store in variable ‘sum’. Next the factors are generated and passed to another function ‘boolean prime()’ to check whether the number is prime or not. If any factor is not prime, the process of finding the next factor is continued. If the factor is found to be prime, sum of the digits of the factor number is calculated and stored in the other counter variable ‘sumfactors’. In such case, the process of finding prime factor number is continues with the same factor and the ntered number is reduced by dividing the entered number by the prime factor. At the end both the entered number and the sum of the digits of prime factor numbers are checked .

Related PostBlueJ Programs on Number

25 comments:

  1. sir please post the output of program on smith number

    ReplyDelete
  2. You execute the program and will get the output.

    ReplyDelete
  3. sir please post me the program on smith number by a different code from the one \you have posted earlier plz sir im nt able to understand that code

    ReplyDelete
  4. Panchi, wait for few days. By the next week I will post modified codes of smith number program.

    ReplyDelete
  5. thankyou sir im sure this time u will post me much easier and shorter code sir in isc practical paper all three questions are compulsory? plz ans me

    ReplyDelete
  6. Students have to answer any one of the three programs in ISC practical.

    ReplyDelete
  7. Enter the date from the user and a number find out the date after n days.

    ReplyDelete
  8. Enter a string from the user and find out that frequency of that word in a string and display it.

    ReplyDelete
  9. This type of program has been already posted in my blog. Please search.

    ReplyDelete
  10. I will post the program very soon.

    ReplyDelete
  11. sir can you please help me with a Program to find all the combinations that can be made with one word example: "abc"
    The combinations: abc - acb - bac - bca - etc...

    ReplyDelete
  12. Similar program is posted in my site. Please search.

    ReplyDelete
  13. unnecesary blank space inside the for loop declaration is causing error during compiling

    ReplyDelete
  14. This space is set where '<' is used otherwise this symbol '<' will be treated as HTML tag while posting in this site.

    ReplyDelete
  15. sir can u plz write a program to check whether a no. is buzz no. or not.(buzz no. ends with 7 and is divisable by 7)

    ReplyDelete
  16. class Bill
    {

    void show(int n)
    {
    if(n%10==7 || n%7==0)
    System.out.println("Number is Buzz number.");
    else
    System.out.println("Number is not a Buzz number.");
    }
    public static void main(String args[])
    {
    Bill ob=new Bill();
    ob.show(25);
    }
    }

    ReplyDelete
  17. // to chk whether a nos is a smith nos or not

    class smith
    {
    public static void main (int n)
    {
    int p=0,r,rem,qu,sum=0,q,s=0,fact,i,j,counter=0,ts=0;
    //to find the sum of the digits
    while(n>=1)
    {
    r=n%10;

    s+=r;//sum of the digits

    n=n/10;
    }

    // to check for factorials
    for(i=1;i=0){

    rem=p%10;
    p=p/10;

    sum+=rem;

    }//check for smith
    }

    if(sum==s)
    System.out.println("The nos is a smith nos");
    else

    System.out.println("The nos is not a smith nos");
    but its not running

    ReplyDelete
  18. First error for(i=1;i=0) - incomplete loop
    'rem=p%10'; from where 'p' got value and what the loop is doing here. you have to find out prime factors of the number and then the sum of the digits of the factors to be calculated

    ReplyDelete
  19. class smithNo
    {


    boolean no(int x)
    {
    int sum=0;
    int sum1=0;
    int z=x;
    while(x!=0)
    {
    sum=sum+x%10;
    x=x/10;
    }

    for(int i=1;i<=z;i++)
    {
    int c=0;
    if(z%i==0)
    {

    for(int j=1;j<=i;j++)
    {
    if(i%j==0)

    {
    c++;
    }

    if (c==2)
    {
    int p=i;
    while(p!=0)
    {
    sum1=sum1+p%10;
    p=p/10;
    }
    }
    }
    }
    }

    if(sum==sum1)

    return true;

    else

    return false;



    }
    }

    this is running but not showing proper output
    when i am entering 22 its showing false...:(

    ReplyDelete
    Replies
    1. Modified codes:

      class Enen
      {


      boolean no(int x)
      {
      int sum=0;
      int sum1=0;
      int z=x;
      while(x!=0)
      {
      sum=sum+x%10;
      x=x/10;
      }

      for(int i=1;i<=z;i++)
      {
      int c=0;
      if(z%i==0)
      {

      for(int j=1;j<=i;j++)
      {
      if(i%j==0)

      {
      c++;
      }
      }/// this was missing

      if (c==2)
      {
      int p=i;
      while(p!=0)
      {
      sum1=sum1+p%10;
      p=p/10;
      }
      }
      }
      }
      //} this was extra

      if(sum==sum1)

      return true;

      else

      return false;

      }

      }

      Delete
  20. sir plz answer me.......i have got my exams......i want to know my mistake in that praogram

    ReplyDelete
  21. Please give me a code of the programe that accept 20 Nos and find out how many are prime nos and display it.

    ReplyDelete
    Replies
    1. import java.io.*;
      class Enen
      {
      int n,i,j;
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      public void take() throws Exception
      {
      for(i=0;i<20;i++)
      {
      System.out.println("Enter the number:");
      n=Integer.parseInt(br.readLine());
      for(j=2;j<n;j++)
      {
      if(n%j==0)
      break;
      }
      if(j==n)
      System.out.println(n + " is a prime number.");
      }
      }
      public static void main(String args[]) throws Exception
      {
      Enen ob=new Enen();
      ob.take();
      }
      }

      Delete

Subscribe via email

Enter your email address:

Delivered by FeedBurner