Computer Program
Computer Program
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[] )
- - - • 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
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);
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;
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
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
• 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
~
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!!");
. 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);
}