You are on page 1of 11

Sum

/* PROGRAM TO PRINT SUM OF THE SERIES 2^3+(2^3+4^3)......n */

import java.io.*;
class Sum
{
public static void main(String args[])throws IOException
{
BufferedReader ab=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter n");
int n=Integer.parseInt(ab.readLine());
double s=0,pow,sum=0;
for(int i=2;i<=n;i=i+2)
{
pow=Math.pow(i,3);
s=s+pow;
sum = sum +s;
}
System.out.println("Sum of the series="+s);
}
}

Prime

/* TO FIND THE PRIME DIGITS OF A NUMBER */

import java.io.*;
class Prime
{
public static void main(String args[])throws IOException
{
System.out.println("enter a number");
BufferedReader ab=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(ab.readLine());
System.out.println("the prime numbers are ");
int f,d,no=n;
while(n!=0)
{f=0;
d=n%10;n=n/10;
for(int i=2;i<d;i++)
{
if(d%i==0)
{
f=1;break;}
}if(f==0)
System.out.print(" "+d);
}
}
}

Abbreviations

/* Program to accept name and print the initials along with surnme*/
import java.io.*;
class Abbreviations
{
public static void main(String args[])throws IOException
{
BufferedReader ab=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a string");
String s=ab.readLine();

int len=s.length();
System.out.println("The name is:");

for(int i=0;i<len;i++)
{
char ch=s.charAt(i);
if(i<=(s.lastIndexOf(' ')))
{
if(Character.isUpperCase(ch))
System.out.print(ch+".");

}
}

System.out.print(s.substring((s.lastIndexOf(' ')+1),len));
}
}

Numbers1

/* TO FIND THE:
1.DIGITS OF A NUMBER
2.NEON NUMBER
3.BUZZ NUMBER
4.REVERSE OF A NUMBER
5.PALINDROME NUMBER
6.ARMSTRONG NUMBER
7.FACTORS OF THE NUMBER AND PERFECT NUMBER*/

class Numbers_1
{
void digits(int n1)
{
int d,no=n1;
System.out.println("The digits of the number");
while(n1!=0)
{
d=n1%10;
n1=n1/10;
System.out.print(d+" ");
}
}

void neon(int n2)


{
int d,s=0,sq=n2*n2;
while(sq!=0)
{
d=sq%10;
sq=sq/10;
s=s+d;
}
if(s==n2)
System.out.println(n2+" is a neon number");
}

void buzz(int n3)


{
if(n3%10==7 && n3%7==0)
System.out.println(n3+" is a buzz number");
}

void reverse(int n4)


{
int r=0,d,no=n4;
while(n4!=0)
{
d=n4%10;
n4=n4/10;
r=r*10+d;
}
System.out.println("The reverse of "+no+" is "+r);
}

void palindrome(int n5)


{
int no=n5,r=0,d;
while(n5!=0)
{
d=n5%10;
n5=n5/10;
r=r*10+d;
}
if(r==no)
System.out.println(no+" is a palindrome number");
}

void armstrong(int n6)


{
int s=0,f,no=n6,d;
while(n6!=0)
{
f=1;
d=n6%10;
n6=n6/10;
for(int i=1;i<=d;i++)
f=f*i;
s=s+f;
}
if(no==s)
System.out.println(no+" is an armstrong number");
}

void perfect(int n7)


{
System.out.println("The factors of the number are ");
int s=0;
for(int i=1;i<n7;i++)
{
if(n7%i==0)
{
System.out.print(i+" ");
s=s+i;
}
}
System.out.println();
if(s==n7)
System.out.println(n7+" is a perfect factorial");
}

public static void main(String args[])


{
Numbers_1 ob=new Numbers_1();
ob.digits(4567);
System.out.println();
ob.neon(9);
System.out.println();
ob.buzz(567);
System.out.println();
ob.reverse(890);
System.out.println();
ob.palindrome(12321);
System.out.println();
ob.armstrong(153);
System.out.println();
ob.perfect(6);
}
}

Strings

/* PROGRAM TO FIND THE NUMBER SPACES,CHARACTERS,WORDS,VOWELS


AND NUMBER OF EACH VOWEL*/

import java.io.*;
class Strings
{
public static void main(String args[])throws IOException
{
System.out.println("enter a string");
BufferedReader ab=new BufferedReader(new InputStreamReader(System.in));
String s1=ab.readLine();
int spaces=0;
int vowels=0,a=0,e=0,i=0,o=0,u=0;
int length=s1.length();
String s=s1.toLowerCase();
for(int j=0;j<length;j++)
{
char ch=s.charAt(j);
if(ch==' ')
spaces++;
else if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
{
vowels++;
if(ch=='a')
a++;
else if(ch=='e')
e++;
else if(ch=='i')
i++;
else if(ch=='o')
o++;
else if(ch=='u')
u++;
}
}
System.out.println("Number of spaces = "+spaces);
System.out.println("Number of words = "+(spaces+1));
System.out.println("Number of characters = "+(length-(spaces+1)));
System.out.println("Number of vowels = "+vowels);
System.out.println("Number of a's = "+a);
System.out.println("Number of e's = "+e);
System.out.println("Number of i's = "+i);
System.out.println("Number of o's = "+o);
System.out.println("Number of u's = "+u);
}
}

Series

/*PROGRAM TO PRINT THE SUM OF TE SERIES*/

import java.io.*;
class Series
{
double s=0;
void series1(int n)
{
int b=1;
for(int i=2;i<=n;i++)
{
b=b+i;
System.out.print("x/"+b+" ");
s=s+b;
}
System.out.println();
System.out.println(s);
}

void series2(int n)
{
int b=0;
for(int i=1;i<=n;i++)
{
b=b+i;
s=s+b;
}
System.out.println(s);
}
void series3()
{
int s=0;
for(int i=1,j=16;j>=2;i++,j=j-2)
s=s+(i*j);
System.out.println(s);
}

void series4()
{
for(int i=1;i<=10;i++)
{
if(i%2==0)
s=s-(i/(i+1));
else
s=s+(i/(i+1));
}
}

public static void main(String args[])

{
Series ob=new Series();
ob.series1(10);
ob.series2(10);
ob.series3();
ob.series4();

}
}

Bonaza

/* MENU DRIVEN PROGRAM TO INPUT PRICE AND CATEGORY AND DISPLAY


THE TOTAL DISCOUNT AND PRICE AFTER GETTING THE DISCOUNT */

import java.io.*;
class Bonaza
{
public static void main(String args[])throws IOException
{
BufferedReader ab=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter price of the articles purchased");
double price=Double.parseDouble(ab.readLine());
System.out.println("Enter category:0-for ground floor,1 for first floor,2-for
second floor,3-for third floor");
int choice=Integer.parseInt(ab.readLine());
double dis=0;
switch(choice)
{
case 0:
dis=10.0/100*price;
break;
case 1:
dis=2.0/100*price;
break;
case 2:
dis=5.0/100*price;
break;
case 3:
dis=7.5/100*price;
break;
default:
System.out.println("Wrong choice");
break;
}
System.out.println("Total discount="+dis);
System.out.println("Total price="+(price-dis));
}
}

Patterns

/* PROGRAM TO PRINT A TRIANGLE AND AN INVERTED TRIANGLE*/

class Patterns
{
void pattern1(int n)
{
System.out.println("The triangle is");
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(" "+i);
}
System.out.println();
}
}

void pattern2(int n)
{
System.out.println("The inverted triangle is");
for(int i=n;i>=1;i--)
{
for(int j=i;j>=1;j--)
{
System.out.print(" "+i);
}
System.out.println();
}
}

public static void main(String args[])


{
Patterns ob=new Patterns();
ob.pattern1(5);
System.out.println();
ob.pattern2(5);
}
}

Electricity
/* PROGRAM TO DISPLAY THE ELECTRICITY */

import java.io.*;
class Electricity
{
public static void main(String args[])throws IOException
{
BufferedReader ab=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the number of units consumed");
double u=Double.parseDouble(ab.readLine());
double ch=0;
if(u<=100)
ch=80.0/100*u;
else if(u>100&&u<=200)
ch=1.0*u;
else if(u>200)
ch=1.25*u;
System.out.println("The electricity bill is:"+(ch+50));
}
}

Range

/* TO FIND THE PRIME NUMBERS IN A RANGE OF 1 TA A NUMBER ENTERED*/

import java.io.*;
class Range
{
public static void main(String args[])throws IOException
{
System.out.println("enter a number");
BufferedReader ab=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(ab.readLine());
System.out.println("the prime numbers are ");
int f;
for(int i=1;i<=n;i++)
{f=0;
for(int j=2;j<i;j++)
{
if(i%j==0)
{
f=1;break;
}
}
if(f==0)
System.out.println(i);
}
}
}

Volume

/* PROGRAM TO PRINT THE VOLUME OF CUBE,CUBOID AND SPHERE */

import java.io.*;
class Volume
{
double vol;
double volume(double s)
{
vol=s*s*s;
return vol;
}
double volume(double l,double b,double h)
{
vol=l*b*h;
return vol;
}
double volume(int r)
{
vol=Math.PI*r*r;
return vol;
}

public static void main(String args[])


{
Volume ob=new Volume();
double a=ob.volume(5.0);
double b=ob.volume(5,10,20);
double c=ob.volume(7);
System.out.println("Volume of cube="+a);
System.out.println("Volume of cuboid="+b);
System.out.println("Volume of sphere="+c);

}
}

Pyramid

/* PROGRAM TO PRINT THE PATTERN */

class Pyramid
{
void pyramid1()
{
for(int i=7;i>=1;i=i-2)
{
for(int j=1;j<=i;j++)
System.out.print(j);
System.out.println();
}
}

void pyramid2()
{
int k=1;
for(int i=1;i<=4;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(k);
k++;
}
System.out.println();
}
}

public static void main(String args[])


{
Pyramid ob=new Pyramid();
ob.pyramid1();
System.out.println();
ob.pyramid2();
}
}

String1

/* PROGRAM TO FIND :
NUMBER OF UPPER CASE CHARACTERS
NUMBER OF LOWER CASE CHARACTERS
NUMBER OF SPECIAL CHARACTERS
NUMBER OF DIGITS */

import java.io.*;
class String1
{
public static void main(String args[])throws IOException
{
BufferedReader ab=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a string");
String s=ab.readLine();
int l=s.length();
int uc=0,lc=0,di=0,sp=0;
for(int i=0;i<l;i++)
{
char ch=s.charAt(i);
if(ch>='A'&&ch<='Z')
uc++;
else if(ch>='a'&&ch<='z')
lc++;
else if(ch>='0'&& ch<='9')
di++;
else
sp++;
}
System.out.println("Number of upper case characters:"+uc);

System.out.println("Number of lower case characters:"+lc);

System.out.println("Number of digits:"+di);

System.out.println("Number of special characters:"+sp);


}
}
Automorphic

/* TO FIND WHETHER A NUMBER IS AUTOMORPHIC OR NOT*/

import java.io.*;
class Automorphic
{
public static void main(String args[])throws IOException
{
System.out.println("enter a number");
BufferedReader ab=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(ab.readLine());
int d,s=0,sq=n*n,no=n;
while(n!=0)
{
d=n%10;
n=n/10;
s++;
}
if(no==sq%(int)Math.pow(10,s))
System.out.println(no+"is an automorphic number");
}
}

Perfect_factorial

/* TO FIND WHETHER A NUMBER IS A PERFECT FACTORIAL OR NOT*/

import java.io.*;
class Perfect_factorial
{
public static void main(String args[])throws IOException
{
System.out.println("enter a number");
BufferedReader ab=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(ab.readLine());
int d,no=n,f,s=0;
while(n!=0)
{f=1;
d=n%10;
n=n/10;
for(int i=1;i<=d;i++)
{
f=f*i;
}
s=s+f;}
if(s==no)
System.out.println(no+" is a prerfect factorial");
}
}

You might also like