0% found this document useful (0 votes)
11 views16 pages

Computer Program

The document contains multiple Java programs that demonstrate various programming concepts, including calculating sums of natural numbers, series, factorials, and checking for prime numbers. It also includes programs for finding factors, displaying Fibonacci numbers, and checking for palindromes. Each program is structured with input prompts and outputs results based on user input.

Uploaded by

harsh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views16 pages

Computer Program

The document contains multiple Java programs that demonstrate various programming concepts, including calculating sums of natural numbers, series, factorials, and checking for prime numbers. It also includes programs for finding factors, displaying Fibonacci numbers, and checking for palindromes. Each program is structured with input prompts and outputs results based on user input.

Uploaded by

harsh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

VGII LOO PS

RATION TJIRO
MS BASED ON ITE f all natur al numbers
SOLVED PROGRA nd and display the sum o
. program to fi
~w a Cl
b using 5canner ass
from 1 to n. .
• of □ll natur al numb ers l to n y
5 111
I I To find th~ ~~ ' .
import java.ut1l. ,
pu l1 l\·c cl·, ss Naturnl
("h ~

I 11·1c .,"t"tic
pu, n
void main (Strin g args[] )

~canner in == new Scam1er([Link]);


int a,n,s==O; f ")
[Link].printh1("Enter the value o n ;
11:: [Link]();
for(a==l ;a<=n;a++)
s=s+a; · ,, )
[Link].printh1("The sum of natur al numb ers up to "+ n+ 11
= +s ;
l

- - - • Write a program to find and display the sum of any ten natur
al numbers.
I I To find the sum of any ten natur al numb ers by using Scan ner
Clas s
import [Link].*;
public class Sum
I
public static void main (String args[])
{
~canner in= new Scanner([Link]);
mt a,n,s=O;
for(a=l;a<=lO;a++)
{
Sys~[Link]("Enter a numb er")·
n= [Link](); '
s=s+n;
}
[Link](''The
} sum of numb ers =="+s);

Prog. :,
{ 11 1
pu.., " void mnin(St ring nrgsl I)
.c .sl"lic

[Link] in = 1ww Scn n,wr(SyHlcm .in);


int a,n;
double s=O;
[Link]("Enlcr the vnluc of n");
n = [Link]();
for(a =l ;a<=n;a++)
s=s+l.0/[Link](2,a);
[Link]("The sum of series =" +s);
)

Write a program to find and display the sum of the given series:
s = 1 - a + a 2 - a 3 + ------------------------ + a 10
/ / A program to find the sum of series
import [Link].*;
public class Series
{
public static void main(String args[])
{
Scanner in= new Scanner([Link]);
int i,a;
double s=0;
[Link]("Enter the value of a");
a= [Link]();
for(i=0;i<=lO;i++)
{
if(i%2==0)
s=s+[Link](a,i);
else
s=s-Math. pow(a,i);

[Link]("The sum of series =" +s)·


} /

Prog. s
gram to. find and display the sum of the given series:
a a a a
- -a+ -a- -4 + •••••••••••..~•••oo••••••••••
__•._.,..,..,,., ____-__ _20... _

. vmd
public static . main
· (String args[])

{ (Sys [Link]);
Scanner in = new Scanner
int i,a;
double s=O; . ti c value of a");
Sy::-[Link]· ti 11 (" l•n\l'r
, 1

a= [Link]();
for(i =1;i<=20;i ++)
(
if(i%2==0)
s=s-(double)a /i;
else
s=s+(double) a/i;

Sys}[Link].t Prm · ="+s)·,


. tln("The sum of senes
I

. a program to find and display the sum of *the* given


Wnte * series: *
s = 1 + (1 *2) + (1 *2*3) + .. ·.. ··...... ·.. ····· + (I *2 3 4 ............ n)
/ / A program to find the sum of series
import [Link].*;
public class Series
{
public static void main(String args[])
{
Scanner in = new Scanner([Link]);
int a,n,p=l,s=O;
[Link]("Enter the value of n");
n= [Link]();
for(a=l;a<=n;a++)
{
p=p*a;
S=s+p;
}
[Link]("The sum of se . ,,
} nes ::: +s);

Write a propaui bl J~ .... ,.,._


• · --•w ~ Pt an
11
·
prune lllllDbef OllOL laiauu dis 1 lllllber and check whether it is a
( I A p~ogra_m ..to check whether a nUlnber . . "' P ay the tn
essage aceordingly. .
unport 1ava.10. ; 18 Prune or not
ublic class Pn·me
p . .
( . •d ma in( Stn ng s( ))throws lOExce pt1on
. static vo1 arg
PUbllC
( d == ne w 1n putStreamReader(System .m ) ;

Reader
1nputStream . . _rea new BufferedReader(read);
BufferedReader in -
· t ab
in , ' c=O; . tln("Enter yo ur nu b ")·
S [Link] m er ,
y rpa . .
a=lntege • rse lnt ([Link]()),
for(b=l;b<=a;b++)
(
if (a%b==0)
c=c+l;
}
if (c==2)
[Link].print1n(a+" 1s
· a pn·me nu mb er") ·
_,
else
[Link].print1n(a+" is no
t a pr im e nu mb er" );

Write a pr og ram in Java to


accept a nu mb er. Fi nd an
its factors (including 1 & ex d display all
clu din g th e nu mb er itself
Sample Input: 15, Ou tpu t: ).
Factors of 15 _= 1, 3, 5.
I I A program to find all the fac
. . tors of a nu mb er
import 1av a.1. 0.*;
public class factors
{
public static void main(String
args[ ])t hr ow s IO Ex ce pti on
l
lnputStreamReader rea d =n
ew In pu tS tre am Re ad er( Sy
~ufferedReader in =n ew Bu ste m. in) ;
mt a,n; ffe red Re ad er( rea d);
8Yst
[Link]("Enter the nu
mb er" );
n=[Link]([Link]()
);
for(a=l;a<n;a++)
{
if(n%a==O)
} [Link],prb\tlJ\{a+" is a
factor of "+n);
}
in l,crs.
, Display the grcat.."•1t f
,b

nrcrcnt nu
. . n lllinput 10 l l thclll•
Writ~ a pw1:1,1111 unbcrs n,nonS I, ,t numbers
und the snu\llcst Ill :l the s'lnol cs
tl•'4t Olll
-I I I\ pn,hrnm J• •11hy llw ~ren .,
Ill ut~ •
hnptlrt [Link].*;
public d.,:--~ Mi,,min

l · (SI
public ~t.,lk ,·t,id mall\ -
ritw
n
nrg~ID
l , •r([Link]);
S'--.n,1wr in = nL'W ~t·imnl - ,
int a,m ,n ,m ,Y,::.l),min=O; . , bcr");
.
Sv~tl'nu,[Link] n u ("E t,. the first num
n:: in .tw'\.tlnt();
n,in=n :m,1'\.=n ;
for(a=1;a<=10;a++)
l • in nine numbers");
[Link]("Ent er the remam g
•'

m= [Link]();
if(m<min)
nun=n1;
if(m>max)
n1ax=m;
}
[Link]("Gre atest Number = "+max);
[Link]("Sma llest Number = "+min);
}

Write a program in Java to find and display the sum of the factorial .
of all the natural numbers from 1 to n.
(Hint.: Factorial of 5! = 5*4*3*2*1)
S = 1! + 2! + 3! + 4! + .................................... +n!
/ / To find the sum of the factorial of numbers
import [Link].•;
public class SumFactorial
{
pu~lic static void main(String args[l) throws IOException

int i,n,f=l,s=O;
InputStreamReader read = new InputStrearnR d ( .
[Link] in = new Buff -..1n_ ea er [Link]);
e..~ader(read)·
[Link]("&\t er the n"); '
arseInt(in .read Line());
nd ntcger.p
(or (1-·-1,·i<:~n;i++)
I
( :: (~i ;
i,~sd;
I rintln(" Tlw sum of fn r lori nl of numbcrn =" +s);
[Link] I·P
I

a : :~~~ r,:~~•;•:'~'."t~~-t'.'..~i~r.l.~·y·t·~-~.'.i~~'.. l.'.J.~~mbcrs of the Fibonacci

I I A program to d isplay the first ten numbers of th e Fibonacci &eries


public class fi bonacci
I
public static void main(String args[ ])
I
int a,b,c,n;a=0;b=l;c=O;n=3;
[Link]("The Fibonacci series is ");
[Link](a);
[Link](b);
do
{
c=a+b;
[Link](c);
a=b;
b=c;
n=n+l;

while(n <=IO);
I

Write a program · J
its d" . · m ava to enter a number and display the sum of
tgits.
Sample Input : 642
II A Sample Output: 12 (6+4+2 = 12)
. program to find the . .
unport [Link].•; sum of the d1g1ts of a number
public class di .
t gits
public sta..:- .
I.&\; void [Link](St.l....
'~~ .........urWS IOException

,
l
([Link])i
Reader
.tStrearP·
I d . rend ::: new Jnptl der(read);
JnfJUlSlrenmRca Cl f{crcdHca ·
· - new 13t1
BuffcrcJ Reader m-
int a,n,s;s=O; umbcr");
. ti 11 ("Fnter o 11
[Link] . ' re·1dL1nt • ,()) ·
,
t(
n=[Link]' 111 · ' ·
111

,,vhilr (n>O)
I
a::: n°/o10;
:--=s+n;
n=n/ 10;
l . ,, ·t "+s)·,
d'11s==
sy~[Link]( The sum of the g
I

Write a program to accept a number and check whethe r the numbe r


is a palindrome or not. Display the message accordingly.
(A number is said to be palindrome if the new ·numbe r obtaine d after
reversing the digits is same as the origin~} number.
Sample Input: 707
Sample Output: 707 is a palindrome number. ·
I. I A program to check for a palindro me numb er -
. .
import 1ava.10.*;
public class palindrome
{
public static void main(String args[])thr
{ ows IOException
lnputStreamReader read = new
BufferedReader . - [Link]
int d,n,p,r=O; m- new BufferedReader(read); er([Link]);
[Link]. println("Ente
r a Dwnbe ");
n=[Link]([Link] . r
p=n; Line());
do
{
d=n%10·,
r=r''lO+d;
n=n/10;
}
while(n!=O);
if(r==p)
--..-■ \\' rite , l"1'-1
1 gr,un to dh ,pl ,1y th ~ st
nn of ro si tiv c ev en nu
n~~ati,-e od d nu n, be rs fro m be rs .in d
1n a lis t of nt nn bc rs ~ntt:-t'\.
Th~ list te m un at es w he n 'd by th\! us ~r .
th e us er en te rs ze ro fro
n, th e co ns'-1\~.
, / A rn...,gram to di sp lay
th e sun.1. of po-sitiY e eY
en .[Link] ne g~ tiv e od d
iffif'l")rt ja\" [Link].*; nu m be rs
d ass Su m

public static Yo id m. ain (S


tri ng ar gs [l)
l
Sc an ne r in = ne w Sc an
ne r(Systen1...in);
int ~1 =0 ,s2 =0 ;
wh ile (tr ue )
l
Sy ste m. ou t.p rin tln ('' En ter
a [Link] an d ze ro to te
n= [Link] xt ln t() ; rn un ar e: ") ;
if(n==O)
br ea k;
if( n> 0& &n % 2= =0 )
sl= sl+ n;
if( n< 0& &n % 2! =0 )
s2=s2+n·I
}
5 Ystem .o ut
.p rin tln (" Su m of po sit i\·
5Ystem .o e e,·e n nu n, be rs = .. +::-l)
ut .p rin tln (" Su m of ne ga ;
tiv e od d nu 1n bl 'rs = .. + :C.);
~[Link] t.p rin tln (" Pr og
ra m te m ,in ,\t es ! ");
}

Write a program to 11\p\lt tw


en ty different nu n, be ~
of those n\111\bea 'WNa Di sp lay the s\lll\
-~ 1 A • di vi sib le by 3 as w el l 5'
program to display th e su
im po rt java.u til •· m
class Sw n .,
{
public: sta tic Vo id 11\aiil\l
{
Scanner in (Systern,in
= n e w Sca )i
i n t i = l ,n ,s nner
u m=0 ;
w h i\ e ( i<=
, 20)
\
S y s tem .o u .
t .p n n t\n (" E1,ter a n u i n b e by one.·" ) ·
n =[Link]\ e r on '
nt();
i f(n %3==
0&&n%5=
su r n=su n ,+ =0)
n;
i+ + ;
)
Syste1n.o u · ,
t .p n n tu..1("S
u m of numbers== II
) + s um)•'

• W r it e a
program t
o d is p la y
number e first ·t e n o
ntered by
t h e user. dd numbe
Sample In rs startin
p u t : 10 g from a
Sample O
u t p u t : 11,
I I A prog 13, 15, ··: ·
r a m to d is ::: ·~ ·· ··:_.. ..
i m p o r t jav p la y t h e f '. .. : . · · · · · · · ·
[Link].*; irst t e n o d · , 29
class O d d d n u m bers from
_Num a given n
l umber
p u bl i c s t a
tic void m
l a in ( S tr in g
argsl1)
Scanner i
n = new S
i n t c = l ,n ; canner(Sy
[Link]);
S y s te m .o u
t.
n = in .n e x tl p r in tl n ( " E n te r a n u m
n t( ) ; ber:");
S y s te m .o u
do t. p r in tl n ( "
F ir s t t e n o
dd numb
\ fr
ers om "
if(n%21=0) +n+" are:
");
\
[Link]
[Link](n+
c++; " ");
n++;
\
else
n++;
)
while(c<=
lO);
\
)
(
I

p~:ic s!.atic \-oid main(String args0 )


(
I

Sau7Uler in = new Scanner(Sys [Link]);


int i,d,n,p,c=O,s=O;
[Link](f'Enter a numb er:");
n=[Link];
p=n;
for(i=n;n>0;n=n/10)
{
d=n%10;
c=c+l;
}
[Link]("Number of digits are:" +c);
for(i=p;p>0;p=p / 10)
{
d=p%10;
s=s+(int)[Link](d,c);
}
[Link]("The result= "+s);
}
. i rence between Compound Inttr~t
7
\\'rit~ a rn.,cram to find the di{ e . ates as soon as the user er,i,;
... Th am temun ' "=Tj,
.mJ [Link]?e lntcre5-t. e rn"'\~ 1i e as inputs from the consr..,!c.
0 (.;:t>n.)). TJ kt> rrindfal. R.1te an1.i im . -·
/I 1 , d • tcrest and simple mten.~t
O tmJ th~ ditt\?n.'nu? t,,tWl"l'll (\Jmroun ,n
imrvrt [Link].,.;
da.s.s Intert'":-t
{
fUt- li,: Stati.:: \'1-,iJ mJin{~trin~ ,l'b~(I)
{ '
&annt1r in = new ~.mner{[Link]);
int F,Lr.n=l; .
d1-1 ub lt> ,1. .d .J..si;
w hik•,n '. =0)
{
[Link]. f rintln(''Ente.r Principal:");
p= [Link].\.tlnt();
[Link]("Enter Rate:");
r== [Link].\.tlnt();
[Link](''Enter Trme:");
t== [Link].\.tlnt();
si=(p*~t) / 100.0;
a=p*~[Link]((l +.01 *r),t));
a==a-p;
d==ci-si;
[Link]("Simple interest= Rs."+si);
[Link]("Compound Interest= Rs."+ci);
[Link]('The difference between C.I and S.I = Rs." +d);
[Link]("Do you ·want to continue?");
[Link]("Enter O (zero) to terminate");
n= [Link]();
}
[Link]("Program Terminates!!");
}

Using a sv.'itch case statement, write a menu driven program to


convert a given temperature from Fahrenheit to Celsius and vice
versa. For an incorrect choice, an appropriate error message should
be displayed.
5
Hint: C = (F - 32) and F = 1.8 •C+32
9
to calculate the temperature
// A program
unport [Link].*;
public class Temperature

~• • Cornputer Applications with BlucJ-IX


~ -,..randing
1
{ tic vo id ma in( Str ing arg s(] )
ubliC sta
p {
int ch;
do ub le c,f ,cl ,fl ;
an,ner in = ne w Sc an nc r(Sys
tcm .in );
Sc ..,.., ou t pri ntl n(" En ter
sys teu •• . 1 to fin d the ll'm p.f rom J;n
·ste01..out.p rin tln (" En ter 2 hn .·n lw it to C eb iu s ") ·
5 to fin J tlw l<.•mp fro m C d ~iu ,
y t ou t pri ntl n(" En 'i to J1a hrc •nh e it ");
Sy s e 01• · ter yo ur c ho ice 1 or 2 " );
ch:::: [Link] xtl nt( );
5\\ itc h(c h)
{
cas e 1:
Sy ste [Link] t.p rin tln (" En ter tem
pe rat ur e in Fa hre nh eit :") ;
f~ [Link] xtD ou ble ();
cl= (do ub le) S / 9*(£-32);
Sy ste [Link] t.p rin tln ("T he tem
pe rat ur e in Ce lsi us is "+ cl)
br ea k; ;
cas e 2:
Sy ste m. ou t.p rin tln ("E nte r tem
pe rat ur e in Ce lsi us :") ;
c= in. ne xtD ou ble ();
fl= ( do ub le) 1.8 *c+ 32 ;
Sy ste m. ou t.p rin tln ("T he tem
pe rat ur e in Fa hre nh eit is "+
bre ak ; fl) ;
de fau lt
Sy ste m. ou t.p rin tln (" W ro ng
Ch oic e !!" );
}
}
}

Pro g. 20 W rit e a me nu dr ive n pr og


ram :
• to ac ce pt a nu mb er fro m
the us er an d to ch ec k wh eth
nu mb er or no t er it a 'BU ZZ '
• to ac ce pt an y tw o nu mb
ers an d dis pla y the ir 'G CD
No te: '.
• A BU ZZ nu mb er is the nu
mb er wh ich eit he r en ds wi
is div isi ble by 7. th dig it 7 or
• GC D (G rea tes t Co mm on
Di vis or) of tw o int eg ers is
by co nti nu ed div isi on me tho cal cul ate d
d. Di vid e the lar ge r nu mb er
sm all er, the rem ain de r the by the
n div ide s the pre vio us div
pro ce ss is rep ea ted till the iso r. Th e
rem ain de r is zer o. Th e div
res ult s in GC D. iso r then
[ICSE-20091
I I A pro gra m to ch ec k BU ZZ
Nu mb er or GC D
im po rt jav a.i o.* ;
pu bli c cla ss Ch ec k

~
Iter ativ e Con suu ccs inJa~-a ~
{ .
pubhe
. static void main(String args[])throws IOException
{
int ch; . )
InputStreamReader read =new InputStrcam Readcr(System.m
· ;
BufferedReadcr in = new 13ufferedRcader(rcacl); ,,
[Link]("l. To check a BUZZ , Numbcr )·' ,
[Link]("2. 1b find GCIJ of two numbcrS' )i
[Link]("Entcr your choice");
ch::: Integer. parseint([Link]());
switch(ch)
{
case 1:
int n;
[Link]("Enter a number");
n==[Link]([Link]());
if(n%7====0 I In%10===7)
[Link](n+"is a Buzz number");
else
[Link](n+"is not a Buzz number");
break;
case 2:
int a,b,t==O;
[Link]("Enter first number");
a=Integer. parselnt([Link]() );
[Link]("Enter second number");
b=[Link]([Link]());
while(a%b!=O)
{
t=a%b;
a=b;
b=t;
}
[Link]("GCD of two numbers ="+b);
break;
default:
[Link]("Wrong choice!!");

~~2 Understan d'mg Computer Applications with BlueJ-IX


j
1,,,a.,;a~w;;rj ce Ii•
(a) N ame two jump statem e nts and their usr:r..
Ans. The two jump s tatem ents are: {ICSE.-~Jl<JJ
(i) Break statement: The control 1· ump!) , ut f L
· ) <J a " 1'J<·k ,, 'i fl,<_,r h
is executed . - • m :::t$ t ,if) ~.1...::,;t~ rr~~r;t
(ii) Continue statement: The contr()J g<Jf•!i l-: • ·k f
. .
Ioo~, b y s k ippm -· ,ac; • <,r thi- n,·Y.t rt'- , ·
g the rest of the btatr!m1:nt ,s ,,f th,; I <><,;>.· ,,r,, Ym r;f t b~
(b) What will be the output of the following <.:<>d,;'! ..
int m=2; {KSt-~'J J<,]
int n=15;
for(int i=l;i<5;i++)
m++; - - n ;
[Link]("m="+m);
[Link]("n="+n);
Ans. m =6 n =14
(c) Analyse the following program segment and determine hr. .
• ; 11 r![Link] ti Y, """ t ,_ "
loop will be executed and what will be the output of the prr.,:;, &.r . ; :· ··\ H -

. t k-1
ln - ,I-·-2·, - ., gr n Y::-o-•• ~ ..,_.
.,. '
while(++i<6)
k *=i;
[Link](k); [ICSE-2-0lGJ
Ans. The loop will execute 3 times and the output is 60.
(d) State the difference between entry controlled loop and exit controlled loop.
[ICSE-2011]
Ans. The differences between entry controlled loop and exit controlled loop are:
Entry controlled loop Exit controlled loop
1. The loop will not execute at all if the [Link] loop will execute a1 least
condition is not satisfied. once, even if the condition is false.
2. It checks the condition at the entry 2. It checks the condition at the exit
point of the loop point of the loop.

(e) Analyse the following program segment and determine how many times the
loop will be executed and what will be the output of the program segment.
. [ICSE-201 I]
- r• L .,. t
,,.._ . ""; -.
int p = 200; 1-J ( , ·' 1 " 0 ) I U, 0) I : U ; 10 0 ,1
while(true)
, I
®
{
if(p<l00)
break;
p=p-20;
}
[Link](p); . Th output= 80
Ans. (i) The program will run for 6 times. (ii) e; . d of for statement.
t sing while mstea
(f) Rewrite the following program segmen u
int f=l,i;
for(i= 1;i<=5;i++)
{
f*::::i;
}
[Link](f); [ICSE-20
Ans. int f::::l 1·-1· 121
'- '
While(i<::::5)
{
f"'::::i;
i++;
}
[Link](f);
(g) What is the difference between a break statement and a continue statement
When they occur in a loop? [ICSE-2013]
Ans. Break statement is used for unusual termination of a loop. As soon as the break
st
atement is executed the control exits the loop leaving the remaining iterations
incomplete. '
For example,
for(int i :::: l; i < 10; i++)
{
if(i :::::::: 5)
break;
[Link](i);
}

When the continue statement is executed then the control returns back for the
next iteration without executing the rest of the statements in the loop.
For example,
for (int i = l; i < 10; i++)
{
if(i == 5)
continue;
[Link](i);
}

(h) What is an infinite loop? Write an infinite loop statement. (ICSE-2014]


Ans. A loop that never ends its iterations is said to be an infinite loop or endless
loop. An example of infinite loop statement is:
for(int i=l; i>-0; i++)
{
Statement(s)
}
OR
for( ; ; )
{
Statement(s)
}
(i) Write the output of the following program code:
[ICSE-2015]
char ch;
int X = 97;
do
{

? -@ Undentanding Comp~ter Applications with BlueJ- IX


ch = (char) x;
[Link] (ch +" ");
if( x% 10 == 0)
break;
++x;
}
while (x <= 100);
Ans, The output:
a
b
C
d
(j) Rewrite the following code snippet by using while loop and give the output.
int p=0;.
for(i=l;i<=5;i++)
{
[Link](i+" ");
p=p+l;
}
[Link](p); [ICSE MODEL]
Ans. Code snippet by using while construct:
int p=0,i=l;
while(i<=5)
{
[Link](i);
p=p+i;
i++;
}
The output is : 1 2 3 4 5
(k) Rewrite the following code snippet by using for loop. [ICSE MODEL]
int i=lO0;
while(i>0)
{
[Link](i);
i=i-2;
}
Ans. for(i=l00;i>0;i-=2)
[Link](i);

You might also like