0% found this document useful (0 votes)
3 views22 pages

While Loop

The document contains a collection of Java programs demonstrating various algorithms using while and nested loops. Each program includes source code, output, and a variable description table (VDT) detailing the purpose of each variable. The programs cover topics such as summing even digits, checking for prime digits, removing zeros, and printing different patterns.

Uploaded by

kanoriavedansh
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)
3 views22 pages

While Loop

The document contains a collection of Java programs demonstrating various algorithms using while and nested loops. Each program includes source code, output, and a variable description table (VDT) detailing the purpose of each variable. The programs cover topics such as summing even digits, checking for prime digits, removing zeros, and printing different patterns.

Uploaded by

kanoriavedansh
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

WHILE LOOP

PROGRAM 1

sum of even digits and product of odd digits

import [Link].*;
class even_odd_digit
{
public static void main()
{
int copy,n,d,s=0,p=1;
Scanner sc=new Scanner([Link]);
[Link]("enter a number");
n=[Link]();
copy=n;
while(copy!=0)
{d=copy%10;
copy=copy/10;
if(d%2==0)
s=s+d;
if(d%2==1)
p=p*d;
}
[Link]("sum="+s);
[Link]("product="+p);
}
}
OUTPUT 1

OUTPUT 2

Variable Description Table (VDT)

Variable Name Data Type Description


N int Stores the number entered by the user
Copy int Stores a copy of the original number for processing
D int Stores each digit extracted from the number
S int Stores the sum of even digits
P int Stores the product of odd digits
Sc Scanner Used to take input from the user
PROGRAM 2

//check whether all digits of a number are prime or not

import [Link].*;
class Digit_Prime{
public static void main()
{ int c1=0,c2=0,n,copy,d;
Scanner sc=new Scanner([Link]);
[Link]("enter a value ");
n=[Link]();
copy=n;
while(copy!=0)
{d=copy%10;
copy=copy/10;
c2=c2+1;
if(d==2||d==3||d==5||d==7)
c1=c1+1;
}
if(c1==c2)
[Link]("all digits are prime");
else
[Link]("all digits are not prime ");
}
}
OUTPUT 1

OUTPUT 2

Variable Description Table (VDT)

Variable Name Data Type Description


N int Stores the number entered by the user
copy int Stores a copy of the original number for digit extraction
D int Stores each digit of the number
c1 int Counts the number of prime digits
c2 int Counts the total number of digits
sc Scanner Used to take input from the user
PROGRAM 3

remove all zero from a number

import [Link].*;
class removezero{
public static void main()
{
int copy,n,d,rev=0;
Scanner sc=new Scanner([Link]);
[Link]("enter a value ");
n=[Link]();
copy=n;
while(copy!=0)
{d=copy%10;
copy=copy/10;
if(d!=0)
rev=rev*10+d;
}
copy=rev;
int rev2=0;
while(copy!=0)
{d=copy%10;
copy=copy/10;
rev2=rev2*10+d;
}
[Link]("after removing zero "+rev2);
}
}
OUTPUT 1

OUTPUT 2

Variable Description Table (VDT)


Variable Name Data Type Description
N Int Stores the number entered by the user
copy Int Stores a copy of the number for processing
D Int Stores each digit extracted from the number
rev Int Stores the reversed number after removing zeros
rev2 Int Stores the final number after re-reversing
sc Scanner Used to take input from the user
PROGRAM 4

Automorphic Number

import [Link].*;

class Automorphic

{public static void main()

{int copy,n,d,s=0,p=1,c=0;

Scanner sc=new Scanner([Link]);

[Link]("enter a number");

n=[Link]();

copy=n;

while(copy!=0)

{d=copy%10;

copy=copy/10;

c++;

}p=(int)[Link](10,c);

int sq=n*n;

int a=sq%p;

if(a==n)

[Link]("Automorphic "+n);

else

[Link]("Not automorphic "+n);}


OUTPUT 1

OUTPUT 2

Variable Description Table (VDT)


Variable Name Data Type Description
n Int Stores the number entered by the user
Copy Int Stores a copy of the number for digit counting
d Int Stores each digit of the number
c Int Stores the count of digits in the number
p Int Stores 10 raised to the power of number of digits
sq Int Stores the square of the number
a Int Stores the last digits of the square
sc Scanner Used to take input from the user
PROGRAM 5

Jexpo number

import [Link].*;

class Jexpo

{void main()

{int copy,n,d,c=0,s=0,fd,ld,num=0;

Scanner sc=new Scanner([Link]);

[Link]("Enter a value ");

n=[Link]();

num=n;

ld=n%10;

n=n/10;

copy=n;

while(copy>9)

{d=copy%10;

copy=copy/10;

s=s+d;

}fd=copy;

if(ld!=0 && fd!=0 && s==0)

[Link]("Jexpo number "+num);

else
[Link]("Not Jexpo number "+num);}

OUTPUT 1

OUTPUT 2

VARIABLE DESCRIPTION TABLE


Variable Data Description
Name Type
N int Stores the number entered by the user
num int Stores the original number
copy int Stores the number excluding first and last digits for
processing
D int Stores each digit extracted from the number
S int Stores the sum of the middle digits
Fd int Stores the first digit of the number
Ld int Stores the last digit of the number
Sc Scanner Used to take input from the user
NESTED LOOP

PROGRAM 6
* Print following pattern
*
a
ba
cba
dcba
where line number 4
*/
import [Link].*;
class pat1
{
public static void main()
{int n;
char p='a',q='a';
Scanner sc=new Scanner([Link]);
[Link]("Enter line number");
n=[Link]();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{[Link](p);
p--;
}
q++;/
p=q;
[Link]();
}
}
}
OUTPUT1

OUTPUT 2

VARIABLE DESCRIPTION TABLE


Variable Name Data Description
Type
N int Stores the number of lines to be printed
P char Stores the character to be printed in the pattern
Q char Stores the starting character for each line
I int Controls the number of rows in the pattern
J int Controls the number of characters printed in each
row
Sc Scanner Used to take input from the user
PROGRAM 7

* Print following pattern


#
&&
###
&&&&
#####
where line number 5
import [Link].*;
class pat2
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter line number");
int n=[Link]();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{if(i%2==1)
[Link]("#");
else
[Link]("&");
}
[Link]();
}
}
}
OUTPUT 1

OUTPUT 2

VARIABLE DESCRIPTION TABLE

Variable Name Data Type Description


N int Stores the number of lines to be printed
I int Controls the number of rows in the pattern
J int Controls the number of symbols printed in each row
Sc Scanner Used to take input from the user
PROGRAM 8

* Print following pattern

@
*@
*@*
@*@*
*@*@*
@*@*@*

where line number 6

import [Link].*;
class pat3
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter line number");
int n=[Link]();
int c=1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{if(c%2==1)
[Link]("@");
else
[Link]("*");
c++;
}
[Link]();
}
}
}
OUTPUT 1

OUTPUT 2

VARIABLE DESCRIPTION TABLE

Variable Name Data Type Description


N int Stores the number of lines to be
printed
I int Controls the number of rows in the
pattern
J int Controls the number of symbols
printed in each row
Sc Scanner Used to take input from the user
PROGRAM 9

* Print following pattern

1
01
101
0101
10101
010101
1010101
import [Link].*;
class pat4
{
public static void main()
{
Scanner sc=new Scanner([Link]);
[Link]("Enter line number");
int n=[Link]();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{if((i+j)%2==0)
[Link]("1");
else
[Link]("0");
}
[Link]();
}
}
}
OUTPUT 1

OUTPUT 2

Variable Description Table (VDT)


Variable Name Data Type Description
N int Stores the number of lines to
be printed
I int Controls the number of rows
in the pattern
J int Controls the number of digits
printed in each row
Sc Scanner Used to take input from the
user
PROGRAM 10

* Print following pattern


*
A
Ab
AbC
AbCd
AbCdE
AbCdEf
AbCdEfG
import [Link].*;
class pat5
{
public static void main()
{
char p='A',q='A',p1='b',q1='b';
Scanner sc=new Scanner([Link]);
[Link]("Enter line number");
int n=[Link]();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{if(j%2==1){
[Link](p);
p+=2;
}
else{
[Link](p1);
p1+=2;
}
}
p=q;
p1=q1;
[Link]();
}
}
}
OUTPUT 1

OUTPUT 2

VARIABLE DESCRIPTION TABLE

Variable Name Data Type Description


P char Stores the uppercase character to be
printed at odd positions
Q char Stores the initial uppercase character for
each line
p1 char Stores the lowercase character to be
printed at even positions
q1 char Stores the initial lowercase character for
each line
N int Stores the number of lines to be printed
I int Controls the number of rows in the pattern
J int Controls the number of characters printed
in each row
sc Scanner Used to take input from the user
STUDENT DETAILS

NAME- VEDANSH KANORIA

CLASS- 9

SECTION -A

ROLL NO.-B177/2014

SERIAL NO.- 24

2ND TERM COMPUTER PROJECT


INDEX

Page No. Content


PROGRAM 1 – Sum of even digits and product of odd digits
1–2
(While Loop): Source Code, Output & VDT
PROGRAM 2 – Check whether all digits of a number are
3–4
prime (While Loop): Source Code, Output & VDT
PROGRAM 3 – Remove all zeroes from a number (While
5–6
Loop): Source Code, Output & VDT
PROGRAM 4 – Automorphic Number (While Loop):
7–8
Source Code, Output & VDT
PROGRAM 5 – Jexpo Number (While Loop): Source Code,
9–10
Output & VDT
PROGRAM 6 – Pattern: a, ba, cba, dcba (Nested Loop):
11–12
Source Code, Output & VDT
PROGRAM 7 – Pattern using # and & (Nested Loop):
13–14
Source Code, Output & VDT
PROGRAM 8 – Pattern using @ and * (Nested Loop):
15–16
Source Code, Output & VDT
PROGRAM 9 – Binary Pattern using 1 and 0 (Nested
17–18
Loop): Source Code, Output & VDT
PROGRAM 10 – Alphabet Pattern (A, Ab, AbC, …) (Nested
19–20
Loop): Source Code, Output & VDT

You might also like