Sakshi Gupta Computer Science Project 2016-17
Sakshi Gupta Computer Science Project 2016-17
import java.io.*;
import java.util.*;
class EN{
int i,n,l,j,c;
String w="",ns="",nw="",s="";
char ch;
n=Integer.parseInt(br.readLine());
if(n<1||n>10){
System.out.println("Invalid output");
System.exit(0);}
else{
for(i=0;i<n;i++){
System.out.println("Enter sentences");
p[i]=br.readLine();
p[i]=p[i].toUpperCase();}
for(i=0;i<n;i++){
if (i%2==0){
w=p[i];
l=w.length();
for(j=0;j<l;j++){
ch=w.charAt(j);
if(ch!=' '&&ch!='.'){
ch=(char)(ch+2);
if(ch>90){
ch=(char)(ch-26);}}
ns=ns+ch;}
p[i]=ns;
ns="";}
else{
w=p[i];
c=str.countTokens();
for(j=0;j<c;j++){
SAKSHI GUPTA COMPUTER SCIENCE PROJECT 2016-17
nw=str.nextToken();
ns=nw+""+ns;}
p[i]=ns;
ns="";}}
for(i=0;i<n;i++){
System.out.println(p[i]);}}}}
Output:
Enter sentences
Question 2: Write a program to accept a sentence and arrange all its words in
ascending order alphabetically.
import java.util.*;
class sen{
String s,temp;
int c,i,j;
System.out.println("Enter sentence");
s=sc.nextLine();
c=str.countTokens();
for(i=0;i<c;i++){
p[i]=str.nextToken();}
for(i=0;i<c;i++){
for(j=0;j<c-i-1;j++){
if(p[j].compareTo(p[j+1])>0){
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;}}}
for(i=0;i<c;i++){
System.out.print(p[i]+"");}}}
SAKSHI GUPTA COMPUTER SCIENCE PROJECT 2016-17
Output:
Question 3: Write a program to accept lower and upper range and print all prime
palindrome number and also print their frequency
import java.util.*;
class palin{
int l=0,u,i,j,f=0,c=0,t,r,rev;
String ns="";
r=sc.nextInt();
u=sc.nextInt();
for(i=l;i<=u;i++){
c=0;
rev=0;
for(j=1;j<=i;j++){
if(i%j==0){
c++;}}
if(c==2&&i>10){
t=i;
while(t!=0){
SAKSHI GUPTA COMPUTER SCIENCE PROJECT 2016-17
r=t%10;
rev=rev*10+r;
t=t/10;
if(rev==i){
System.out.println(i);
f++;}}}
System.out.println("Frequency="+f);
23
13
Frequency=0
Frequency=0
Frequency=0
Frequency=0
Frequency=0
Frequency=0
Frequency=0
SAKSHI GUPTA COMPUTER SCIENCE PROJECT 2016-17
Frequency=0
Frequency=0
Frequency=0
Frequency=0
11
Frequency=1
Frequency=1
Frequency=1
SAKSHI GUPTA COMPUTER SCIENCE PROJECT 2016-17
Question 4: Write a program to accept time(hours and minutes) in digital format and
display it in words.
import java.util.*;
class HM1{
int h,m;
String w="",s="";
System.out.println("Enter hours");
h=sc.nextInt();
System.out.println("Enter minutes");
m=sc.nextInt();
if(h<=12){
w="am";}
else if(h>12){
w="pm";
h=h-12;}
String
p[]={"one","two","three","four","five","six","seven","eight","nine","ten",
"eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen",
"eighteen","nineteen","twenty","twenty one","twenty two","twenty
three","twenty four","twenty five","twenty six","twenty seven","twenty
eight","twenty nine"};
SAKSHI GUPTA COMPUTER SCIENCE PROJECT 2016-17
if(m==0){
System.out.println(a[h-1]+""+"0clock");}
else if(m>0&&m<15){
System.out.println(a[m-1]+""+"past"+a[h-1]);}
else if(m==15){
System.out.println("quater past"+""+a[h-1]);}
else if(m>15&&m<30){
System.out.println(a[m-1]+""+"past"+""+a[h-1]);}
else if(m==30){
else if(m>30&&m<60){
System.out.println(a[(60-m)-1]+""+"to"+""+a[h]);
Enter minutes
15
quater past0
SAKSHI GUPTA COMPUTER SCIENCE PROJECT 2016-17
Question 5: Write a program to accept a day number from the user and generate the
corresponding date also accept n from the user to compute and display the future date
corresponding to n days after the generated date
Year:2008
import java.util.*;
class day{
int i,d,y,n,date=0,sum=0,mm=0;
int p[]={31,28,31,30,31,30,31,31,30,31,30,31};
String
m[]={"january","february","march","april","may","june","july","august","
september","october","november","december"};
System.out.println("Enter year");
y=sc.nextInt();
d=sc.nextInt();
p[0]=(y%4==0)?29:28;
for(i=0;i<12;i++){
sum=sum+p[i];
SAKSHI GUPTA COMPUTER SCIENCE PROJECT 2016-17
if(sum>d){
sum=sum-p[i];
mm=i;
date=d-sum;
break;}}
System.out.println("Date="+date);
System.out.println("Month="+m[mm]);
System.out.println("Year="+y);
System.out.println("Enter n");
n=sc.nextInt();
d=d+n;
if((d>365)||(d>366)){
y=y+1;
d=d-365;}
p[1]=(y%4==0)?29:28;
for(i=0;i<12;i++){
sum=sum+p[i];
if(sum>d){
sum=sum-p[i];
mm=i;
date=d-sum;
break;}}
SAKSHI GUPTA COMPUTER SCIENCE PROJECT 2016-17
System.out.println("Date="+date);
System.out.println("Month="+m[mm]);
System.out.println("Year="+y);}}
2008
20
Date=20
Month=january
Year=2008
Enter n
12
Date=3
Month=february
Year=2008
SAKSHI GUPTA COMPUTER SCIENCE PROJECT 2016-17
Ex: 666
Sum of digits=6+6+6=18
import java.util.*;
class smith{
int s=0;
while(n!=0){
s=s+n%10;
n=n/10;}
return s;}
int i=2,sum=0;
while(n>1){
if(n%i==0){
sum=sum+SumDig(i);
n=n/i;}
else{
i++;}}
return sum;}
int num;
System.out.println("Enter number");
num=sc.nextInt();
int a=obj.SumDig(num);
int b=obj.SumPrimeFact(num);
if(a==b)
System.out.println("Smith number");
else
666
Smith number
SAKSHI GUPTA COMPUTER SCIENCE PROJECT 2016-17
Member function:
void accept()- accept upper and lower limit from the user
Also write main function to generate the series of prime palindrome numbers with the
help of following functions
import java.util.*;
class primepal{
int l,u;
void accept(){
l=sc.nextInt();
u=sc.nextInt();}
int i,f=0;
for(i=0;i<=p;i++){
if(p%i==0)
f=f+1;}
if(f==2)
SAKSHI GUPTA COMPUTER SCIENCE PROJECT 2016-17
return true;
else
return false;}
int temp=z,r,rev=0;
while(temp!=0){
r=temp%10;
rev=rev*10+r;
temp=temp/10;}
return rev;}
void generate(){
int i;
for(i=l;i<=u;i++){
boolean x=isprime(i);
int y=isrev(i);
if(x==true&&(i==y))
System.out.println(i);}}
obj.accept();
obj.generate();}}
SAKSHI GUPTA COMPUTER SCIENCE PROJECT 2016-17
Member function:
void accept()- to accept upper and lower limit from the user
Ex: 13=31
Also count frequency of twisted prime number is that range and main function
import java.util.*;
class twisted{
int l,u;
void accept(){
l=sc.nextInt();
u=sc.nextInt();}
int i,c=0,x;
for(i=0;i<=c;i++){
if(x%i==0){
SAKSHI GUPTA COMPUTER SCIENCE PROJECT 2016-17
c++;}}
if(c==2){
return true;}
return false;}}
int temp=n,r,rev=0;
while(temp!=0){
r=temp%10;
rev=rev*10+r;
temp=temp/10;}
if(rev==n){
return n;}}
void generate(){
int j,b;
for(j=l;j<=u;j++){
a=isprime(j);
b=isprime(j);
c=isprime(b);
if(a==true&&c==true)
obj.accept();
obj.generate();}}
12 13
13
SAKSHI GUPTA COMPUTER SCIENCE PROJECT 2016-17
Question 9: Write a program to accept a number from the user and count frequency
of each digit.Also check whther a number is a duck number or not.A number is said to
be duck number if digit is 0 is present in the number
import java.util.*;
class duck{
int n;
System.out.println("Enter a number");
n=ob.nextInt();
long temp=n,r;
int k=0;
while(temp!=0){
r=temp%10;a[k]=r;
k++;
temp=temp/10;}
int i,flag=0;{
for(i=0;i<k;i++){
if(a[i]==0){
flag=1;
break;}}}
if(flag==1){
SAKSHI GUPTA COMPUTER SCIENCE PROJECT 2016-17
System.out.println("duck number");}
else if(flag==0)
int f=0;
for(i=0;i<=9;i++){
f=0;
for(int j=0;j<k;j++){
if(i==a[j]){
f++;}}
if(f>0)
System.out.println("Frequency of"+i+"is"+f);}}}
10
duck number
Frequency of0is1
Frequeny of1is1
SAKSHI GUPTA COMPUTER SCIENCE PROJECT 2016-17
Question 10: Write a program to accept a number and check whether it is happy
number or not.A number is said to happy if eventual sum of square of digits of a number
is equal to 1.
Ex: 28=4+64=68
import java.util.*;
class hap{
int temp=x,s=0,r;
while(temp!=0){
r=temp%10;
s=s+r*r;
temp=temp/10;}
return s;}
int n;
System.out.println("Enter number");
n=sc.nextInt();
do{
int z=obj.sum(n);
if(z<10){
if(z==1)
System.out.println("Happy number");
else
break;}
else{
n=z;}}
while(true);}}
28
Happy number
SAKSHI GUPTA COMPUTER SCIENCE PROJECT 2016-17
Question:11
SAKSHI GUPTA COMPUTER SCIENCE PROJECT 2016-17
SAKSHI GUPTA COMPUTER SCIENCE PROJECT 2016-17