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="";
}
}
}
}

Saturday, 1 December 2012

To display each word in the string in reverse order


import java.io.*;
class string38
{
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 extracted word in reverse from are from 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);
    }
System.out.print(p+" ");
c1=0;
p="";
k="";
}
}
}
}