Syvum Home Page

Home > Discussions > For all activities

Discussion topic: Contributed Answer/Explanation to Q. 1

Viewing messages

To go to the homepage of this topic, click here.
Page 1 of 1
From: asha_g_mcaReply 1 of 11Reply         View replies (1)
Subject: Contributed Answer/Explanation to Q. 1
   JAVA
   
   class smith
   
   
   {
   
   
   void main(int  n)
   
   
   {
   
   
   int d,sumd=0,sump=0,a=n,m,c=0;
   
   
   //checking for Prime number
   
   
   for(int i=1;i<=n;i++)
   
   
    if (n%i==0) c++;
   
   
   if (c>2)
   
   
   {
   
   
   //sum of digits 
   
   
    while(a!=0)
   
   
   {d=a%10;
   
   
   a=a/10;
   
   
   sumd+=d;
   
   
   }
   
   
    
   
   
   //sum of prime factors
   
   
   for(int i=2;i<=n;i++)
   
   
   { 
   
   
   if (n%i==0) m=i;
   
   
   //checking for prime factor 
   
   
   int f=0;
   
   
   for(int j=1;j<=m;j++)
   
   
    if (m%j==0) f++;
   
   
   if (f==2)
   
   
   {
   
   
   if (m<10) sump+=m;
   
   
   else{
   
   
         a=m;
   
   
         while(a!=0)
   
   
         {d=a%10; a=a/10; sump+=d; }
   
   
      }
   
   
   } 
   
   
   }
   
   
   if (sump==sumd)
   
   
     System.out.println(n+"is a Smith number");
   
   
   else
   
   
    System.out.println(n+"is a Smith number");
   
   
   }
   
   
   else
   
   
   System.out.println("You have entered a prime number.Prime cannot be a
   smith number");
   
   
   }
   
   
   }
   
   
    
   
   
    
   
   
    
   
   
    
   
   
    
   
   
    
   
   
    
   
   
    
   
   
    
   
   
    
   
   
    

Posted at: Tue Sep 14 06:28:07 2010 (GMT)

From: asha_g_mcaReply 2 of 11Reply
Subject: Re: Contributed Answer/Explanation to Q. 1
   good

Posted at: Tue Sep 14 06:34:05 2010 (GMT)

From: debrup93Reply 3 of 11Reply
Subject: Contributed Answer/Explanation to Q. 1
   it is simply coded and easy to comprehend
   import java.io.*;class Smith{public static void main(String
   args[]){BufferedReader in=new BufferedReader(new
   InputStreamReader(System.in));int n,m,i=0;System.out.println("Enter any
   number");n=Integer.parseInt(in.readLine());m=n;String
   s="";s=in.readLine();int a[]=new int[s.length()];int
   d;while(d>1 && d{if(m%d==0){int
   k=1,s=0;while(k{if(d%k==0){s=s+k;}k++;}if(d+1==s){a[i]=d;i++;}d++;}int
   sum1=0,p=0;while(m>0){p=m%10;sum1=sum1+p;p=p/10;}for(i=0;i{int
   h,sum2=0;if(a[i]>0){h=a[i]%10;sum2=sum2+h;a[i]=a[i]/10;}}if(sum1==sum2)Sy
   stem.out.println("SMITH number");elseSystem.out.println("NOT
   SMITH number");}}

Posted at: Sat Nov 13 17:54:47 2010 (GMT)

From: prashissReply 4 of 11Reply
Subject: Contributed Answer/Explanation to Q. 3
   At first we find the highest and the second highest element in the matrix.
   After that we sort the mtrix and print it.
   
   import java.io.*;
   class matrix
   {
       public static void main()throws IOException
       {
   int m, n, max, max2, temp;
	   BufferedReader br = new BufferedReader(new
   InputStreamReader(System.in));
	   System.out.println("Enter the dimentions of the array
   ----->");
	   m=Integer.parseInt(br.readLine());
	   n=Integer.parseInt(br.readLine());
	   int A[][] = new int[m][n];
	   System.out.println("Enter the elements of the array
   ----->");
	   for(int i=0; i<m; i++)
	   {
	       for(int j=0; j<n; j++)
	       {
		   A[i][j]=Integer.parseInt(br.readLine());
	       }
	   }
	   System.out.println("The elements of the array are
   ----->");
	   for(int i=0; i<m; i++)
	   {
	       for(int j=0; j<n; j++)
	       {
		   System.out.print(A[i][j] + "\t");
	       }
	       System.out.println();
	   }
	   max = A[0][0];
	   max2 = 0;
	   for(int i=0; i<m; i++)
	   {
	       for(int j=0; j<n; j++)
	       {
		   if(max<A[i][j])
		   {
		       max = A[i][j];
		   }
	       }
	   }
	   for(int i=0; i<m; i++)
	   {
	       for(int j=0; j<n; j++)
	       {
		   if(max!=A[i][j] && max2<A[i][j])
		   {
		       max2 = A[i][j];
		   }
	       }
	   }
	   System.out.println("The max element of the array is ----->
   " + max);
   System.out.println("The 2nd max element of the array is ----->
   " + max2);
   for(int k=0; k<n; k++)
   {
	   for(int i=0; i<=n; i++)
	   {
		   for(j=i+1; j<n; j++)
		   {
			   if(A[k][i]>A[k][j])
			   {
				   temp = A[0][i];
				   A[0][i] = A[0][j];
				   A[0][j] = temp;
			   }
		   }
	   }
   }
   System.out.println("The sorted elements of the matrix are----->
   ");
   for(int i=0; i<n; i++)
   {
	   for(int j=0; j<n; j++)
	   {
		   System.out.println(A[i][j]);
	   }
	   System.out.println();
   }
   }
   }

Posted at: Sun Jan 16 12:06:22 2011 (GMT)

From: vijayrastogi2kReply 5 of 11Reply
Subject: Contributed Answer/Explanation to Q. 3
   fgdgdf

Posted at: Mon Jan 23 05:35:45 2012 (GMT)

From: namanjain121295Reply 6 of 11Reply
Subject: Contributed Answer/Explanation to Q. 1
   about smith number
   
   import java.util.*;
   
   
   class Smith
   
   
   {
   
   
   public static void main(String args[])
   
   
   {int sum1=0,sum2=0;
   
   
   Scnner X=new Scanner(System.in);
   
   
   System.out.print("Enter any number")
   
   
   int n=X.nextInt();
   
   
   for(int i=2;i<n;i++)
   
   
   {
   
   
   if(n/i==0)
   
   
   {
   
   
   sum1+=i;
   
   
   n=n/i;
   
   
   i=2;}
   
   
   
   
   
   while(n/10==0)
   
   
   {
   
   
   int n=n%10;
   
   
   sum2+=n;
   
   
   n=n/10;
   
   
   }
   
   
   if(sum1==sum2)
   
   
   System.out.println("no is smith");
   
   
   else
   
   
   System.out.print("number is not smith");
   
   
   }
   
   
   }
   
   
   Explanation-
   
   
   take any number -666
   
   
   enter into  loop
   
   
   divide by 2
   
   
   sum1=0+2 
   
   
   after that  you get
   
   
   number is n=333
   
   
   again the value of i=2
   
   
   again loop run  
   
   
   divide by 3
   
   
   sum1=2+3 
   
   
   after that  you get
   
   
   number is n=111
   
   
   again the value of i=2
   
   
    .
   
   
   .
   
   
   .
   
   
   .
   
   
   .
   
   
   .
   
   
   at the end we get the sum 
   
   
   an compare to the sum1 
   
   
   if equal we give the statement no is smith number 

Posted at: Wed Feb 22 20:36:46 2012 (GMT)

From: amit95Reply 7 of 11Reply
Subject: Contributed Answer/Explanation to Q. 1
   the same logic
   
   import java.util.*;
   
   
   class Smith
   
   
   {
   
   
       public static void main(String args[])
   
   
       {
   
   
           Scanner sc=new Scanner(System.in);
   
   
           System.out.println("enter the number");
   
   
           int n=sc.nextInt();
   
   
           int i=2,sum=0,nsum=sumD(n);
   
   
           while(n>1)
   
   
           {
   
   
               inner:for(int j=2;j<i;j++)
   
   
               if(i%j==0)
   
   
               {
   
   
                   ++i;
   
   
                   continue inner;
   
   
               }
   
   
               if(n%i==0)
   
   
               {
   
   
                   n/=i;
   
   
                   sum+=sumD(i);
   
   
                   i=2;
   
   
               }
   
   
               else
   
   
                   ++i;
   
   
           }
   
   
           if(sum==nsum)
   
   
               System.out.println("Smith number");
   
   
           else
   
   
               System.out.println("not Smith number");
   
   
       }
   
   
       static int sumD(int a)
   
   
       {
   
   
           int s=0;
   
   
           while(a>0)
   
   
           {
   
   
               s+=a%10;
   
   
               a/=10;
   
   
           }
   
   
           return s;
   
   
       }
   
   
   }        

Posted at: Fri Feb 8 16:46:12 2013 (GMT)

From: arpit159Reply 8 of 11Reply         View replies (1)
Subject: Contributed Answer/Explanation to Q. 2
   
   class string operation
   {
     public static void main(String args[])
	{ 
   String w="";    // null string
	   System.out.println ("Enter a String");
	       String str=br.readLine ();   //user input string.
   int l=str.length();
   int wl=checkvowel=checkcharecter=0; // initial zero
   
   System.out.println("Sentence  No. of Vowels	 No. of Words");
     // output outline
   
   for(i=0;i=l;i++)
      {
	char z=str.charAt (i);
	 w=w+z; 	//picking each latter (character) and adding it to form
   a word.
   if(z=='!'||z=='.'||z=='?')
      {
	wl=w.length();
    /*
   length of the word, which is form by adding each character
   */
   
   for(int j=0;j=(wl-1);j++)
    /* 
   j=(wl-1), because last character of the word w will be a special character
   (i.e. '!' , '.', '?') which is not need to be counted 
   */
   {
   char x=w.chatAt (j); //extraction of character from word 
   
   if(x=='A'||x=='a'||x=='E'||x=='e'||x=='I'||x==i||x=='O'||x=='o'||x=='u'||x==
   'U')
   {  //checking vowels.
   checkvowel++; //if vowel found in a word w, checkvowel value add up one.
   checkcharecter++;
   }
     checkcharecter++; //character present in the word is counted
	      } //closing of word loop
	 } //closing of finishing of word loop when encountered '!', '?', '.'
   
     System.out.println( checkvowel+"	  "+checkcharecter);
     w=""; //assigning null to word w, so it can form new word again
    } //closing of for loop
   
     } //closing of main
    }//closing of class

Posted at: Fri Feb 8 20:44:47 2013 (GMT)

From: arpit159Reply 9 of 11Reply
Subject: Contributed Answer/Explanation to Q. 2
   
   class string operation
   {
     public static void main(String args[])
	{ 
   String w="";    // null string
	   System.out.println ("Enter a String");
	       String str=br.readLine ();   //user input string.
   int l=str.length();
   int wl=checkvowel=checkcharecter=0; // initial zero
   
   System.out.println("Sentence  No. of Vowels	 No. of Words");
     // output outline
   
   for(i=0;i=l;i++)
      {
	char z=str.charAt (i);
	 w=w+z; 	//picking each latter (character) and adding it to form
   a word.
   if(z=='!'||z=='.'||z=='?')
      {
	wl=w.length();
    /*
   length of the word, which is form by adding each character
   */
   
   for(int j=0;j=(wl-1);j++)
    /* 
   j=(wl-1), because last character of the word w will be a special character
   (i.e. '!' , '.', '?') which is not need to be counted 
   */
   {
   char x=w.chatAt (j); //extraction of character from word 
   
   if(x=='A'||x=='a'||x=='E'||x=='e'||x=='I'||x==i||x=='O'||x=='o'||x=='u'||x==
   'U')
   {  //checking vowels.
   checkvowel++; //if vowel found in a word w, checkvowel value add up one.
   checkcharecter++;
   }
     checkcharecter++; //character present in the word is counted
	      } //closing of word loop
	 } //closing of finishing of word loop when encountered '!', '?', '.'
   
     System.out.println( checkvowel+"	  "+checkcharecter);
     w=""; //assigning null to word w, so it can form new word again
    } //closing of for loop
   
     } //closing of main
    }//closing of class

Posted at: Fri Feb 8 20:46:55 2013 (GMT)

From: mike_schofieldReply 10 of 11Reply
Subject: Contributed Answer/Explanation to Q. 1
   Its an easy one:-
   
   import java.io.*;
   
   
   public class smith_no
   
   
   {
   
   
    public static void main(String ar[])throws IOException
   
   
    {
   
   
        BufferedReader in=new BufferedReader(new
   InputStreamReader(System.in));
   
   
        System.out.println("Enter your no.");
   
   
        int n=Integer.parseInt(in.readLine());
   
   
        int a,b,s1,c,d,e,s2;
   
   
        s1=s2=0;
   
   
        e=n;
   
   
        for(a=2;a<=n;a++)
   
   
        {
   
   
            while((n%a)==0)
   
   
            {
   
   
                System.out.print(a+"..");
   
   
                if(a<10)
   
   
                s1=s1+a;
   
   
                    
   
   
                else
   
   
                {
   
   
                    d=a;
   
   
                    while(d!=0)
   
   
                    {
   
   
                        c=d%10;
   
   
                        s1=s1+c;
   
   
                        d=d/10;
   
   
                    }
   
   
                }
   
   
                n=n/a;
   
   
            }
   
   
        }
   
   
        
   
   
        while(e!=0)
   
   
        {
   
   
            c=e%10;
   
   
            s2=s2+c;
   
   
            e=e/10;
   
   
        }
   
   
            if(s1==s2)
   
   
            System.out.println("SMITH NUMBER");
   
   
            
   
   
            else
   
   
            System.out.println("NOT A SMITH NUMBER");
   
   
        
   
   
    }
   
   
   }

Posted at: Sun Feb 10 13:54:53 2013 (GMT)

From: rishisuuReply 11 of 11Reply
Subject: Contributed Answer/Explanation to Q. 1
   easy code
   
     import java.io.*;
   
   
   public class Smith
   
   
   {public static void main(int a)
   
   
   { int s=0,sum=0,t=a;int k;int d=a;
   
   
   do{k=d%10;
   
   
   sum=sum+k;
   
   
   d=d/10;}while(d>0);
   
   
   int x=2;
   
   
   while (x<=t)
   
   
   { while (t%x==0)
   
   
   {int g=prime(x);
   
   
   if(g==1)
   
   
   {int pop=sumd(x);
   
   
   s=s+pop;
   
   
   t=t/x;}
   
   
   }
   
   
   x=x+1;
   
   
   }
   
   
   if(s==sum)
   
   
   System.out.println("Smith");
   
   
   }
   
   
   
   
   
   public static int prime(int c)
   
   
   {
   
   
   int b=0;
   
   
   for(int i=1;i<=c;i++)
   
   
   {if(c%i==0)
   
   
   b++;
   
   
   }
   
   
   if(b==2)
   
   
   return 1;
   
   
   else 
   
   
   return 0;
   
   
   }
   
   
   public static int sumd(int pin)
   
   
   {int k,s=0;
   
   
   do{k=pin%10;
   
   
   s=s+k;
   
   
   pin=pin/10;}while(pin>0);
   
   
   return s;
   
   
   }
   
   
   }

Posted at: Mon Jan 27 15:56:08 2014 (GMT)

Page 1 of 1

To post to this forum, you must be signed in as a Syvum member. Please sign in / register as a member.

Contact Info © 1999-2024 Syvum Technologies Inc. Privacy Policy Disclaimer and Copyright