Saturday, 21 September 2013

SMITH number

import java.io.*;//a java program to check whether the number is a smith number or not
class smith//class name
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n,m,d,e,i,j,c,s=0,p=0,l;//variables required
System.out.println("Enter the number to check whether the number is smith or not : -");
n=Integer.parseInt(br.readLine());
m=n;
while(m>0)//the sum of the digit is found out
{
d=m%10;
s=s+d;
m=m/10;
    }
    for(i=1;i<=n;i++)//the for loop is run to find out the prime factors of the number
    {
        c=0;
        if(n%i==0)
        {
            for(j=1;j<=i;j++)//number of factors is calculated
            {
                if(i%j==0)
                {
                    c++;
                }
            }      
                if(c==2)//if there are two factors ,the number is prime
                {
                    l=i;
                    while(l>0)//the sum of the prime factors is found out
                    {
                        e=l%10;
                        p=p+e;
                        l=l/10;
                    }
                    n=n/i;
                    i=1;
                }    
        }
}
    if(s==p)//If the sum of prime factors is equal to the sum of the digits,the number is smith
    {
     System.out.println("The number is smith number");
    }
    else
    {
        System.out.println("The number is  not smith number");
    }
}
    }

Wednesday, 29 May 2013

Nested Prime

import java.io.*;
class Nestedprime
{
    public static void main(String args[])throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int i,m,n,d,c,j;
        System.out.println("Enter the number to check the prime numbers upto the range u wish");
        n=Integer.parseInt(br.readLine());
        for(i=1;i<=n;i++)
        {
            c=0;
            for(j=1;j<=i;j++)
            {
                if(i%j==0)
                {
                    c++;
                }
            }
       
            if(c==2)
            {
                System.out.println(i);
           }
        }  
    }
}

Nested Palindrome

import java.io.*;
class NestedPalindrome
{
    public static void main(String args[])throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int i,m,n,d,s;
        System.out.println("Enter the number to check the armstrong numbers upto the range u wish");
        n=Integer.parseInt(br.readLine());
        for(i=1;i<=n;i++)
        {
            m=i;
            s=0;
            while(m>0)
            {
                d=m%10;
                s=s*10+d;
                m=m/10;
            }
          if(s==i)
        {
            System.out.println(i);
        }
    }
    }
}
           
        

Nested Armstrong

import java.io.*;
class NestedArmstrong
{
    public static void main(String args[])throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int i,m,n,d,s;
        System.out.println("Enter the number to check the armstrong numbers upto the range u wish");
        n=Integer.parseInt(br.readLine());
        for(i=1;i<=n;i++)
        {
            m=i;
            s=0;
            while(m>0)
            {
                d=m%10;
                s=s+d*d*d;
                m=m/10;
            }
          if(s==i)
        {
            System.out.println(i);
        }
    }
    }
}
           
        

Sunday, 2 December 2012

Number of spacesand words in the string


import java.io.*;
class Space_count{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s;
int l,i,c=0;
System.out.println("Enter the String");
s=br.readLine();
l=s.length();
for(i=0;i<l;i++)
{
if(s.charAt(i)==' ')
{
c++;
}
}
System.out.println("The number of spaces in the string"+c);
System.out.println("The number of words in the string"+(c+1));
}
}

To count the number of palindrome words in the string


import java.io.*;
class string46
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s,k="",p="";
int i,l,c=0,c1=0,j;
    System.out.println("Enter the string : - ");
    s=br.readLine();
    s=s+" ";
    l=s.length();
for(i=0;i<l;i++)
{
if(s.charAt(i)!=' ')
{
k=k+s.charAt(i);
}
else
{
for(j=k.length()-1;j>=0;j--)
{
p=p+k.charAt(j);
    }
if(k.equals(p)==true)
{
c1++;
}
p="";
k="";
}
}
System.out.println("The number of palindrome words in the upper string are : - "+c1);  
}
}

The each palindrome word in the string


import java.io.*;
class string41
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s,k="",p="";
int i,l,c=0,c1=0,j;
    System.out.println("Enter the string : - ");
    s=br.readLine();
    s=s+" ";
    l=s.length();
System.out.println("The each palindrome word in the upper string are as follows : - ");  
for(i=0;i<l;i++)
{
if(s.charAt(i)!=' ')
{
k=k+s.charAt(i);
}
else
{
for(j=k.length()-1;j>=0;j--)
{
p=p+k.charAt(j);
    }
if(k.equals(p))
{
System.out.println(k);
}
c1=0;
p="";
k="";
}
}
}
}