Chapter 9
Ans:
3 6 9 12 13
Ans:
for(int i = 0; i <= 20; i++)
{
System.out.println( i );
}
Ans:
int i = 1;
while(i <= 10)
{
System.out.println( i );
i++;
}
Ans:
int i = 1;
do
{
System.out.println( i );
i++;
}while( i <= 15);
Ans:
The value of m is = 5
Ans:
int i = 15;
do
{
System.out.println( i );
i--;
}while(i >= 1);
Ans:
int j = 1;
while( j <= 12)
{
System.out.println(j + “ * ” + j + “ = “ + j * j);
j += 2;
}
Ans:
int j = 1;
do
{
System.out.println(j + “*” + j + “ = “ + j * j);
j++;
}while(j <= 12);
Ans:
int x = 10;
while( x >= 0)
{
System.out.println(6 + “ * ” + x + “ = “ + 6 * x);
x--;
}
int j = 12;
do
{
System.out.println(j + “ * ” + j + “ = “ + (j + j) );
j--;
}while( j >= 1);
Ans:
for(int y = 10; y >= 1; y--)
{
System.out.println(y * 3);
y = y - 1;
}
Ans:
for(int n=0; n<=15; n++)
{
System.out.println(n * 10);
}
Ans:
for(int a=10, s = 0; a>0; a--)
{
s += a;
}
System.out.println(“The final value of s = “ + s);
Ans:
The values of X = 6 and Y = 9
Ans:
- Loop will execute 2 times
- The final value of Y = 2
Ans:
8 7 6 5 4 3 2 1 0
-1
Ans:
class p17
{
void factor(int num)
{
for(int i = 1; i <= num; i++)
{
if(num % i == 0)
System.out.println( i );
}
}
}
Ans:
class p18
{
void print()
{
double sum = 0;
for(int i = 1; i <= 10 ; i++)
{
double cube = Math.pow(i,3);
sum = sum + cube;
System.out.print(cube + “\t”);
}
System.out.print(sum);
}
}
Ans:
import java.util.*;
class p19
{
void factor( )
{
Scanner sc = new Scanner (System.in)
int N = sc.nextInt();
int pro = 1;
for(int i = 1; i <= N; i++)
{
if(num % i == 0)
{
System.out.print( i + “\t” );
pro = pro * i;
}
}
System.out.println(“Product of factors: “ + pro);
}
}
Ans:
public class p20
{
public static void main()
{
int n = 1, sum = 0;
while(n <= 40)
{
if(n % 2 == 0)
sum = sum + n;
n++;
}
System.out.println("Sum of all even nos between 1-40 = " + sum);
}
}
Ans:
public class MyClass
{
public static void main(String args[])
{
int n = 1, sum = 0;
while(n <= 40)
{
if(n % 2 != 0)
sum = sum + n;
n++;
}
System.out.println("Sum of all odd nos between 1-40 = " + sum);
}
}
Ans:
public class p22
{
void factorial(int n)
{
int fact = 1;
for(int i = 1; i<=n; i++)
fact = fact * i;
System.out.println("Factorial of " + n + “ is: “ + fact);
}
}
Ans:
public class p23
{
void letters()
{
for(char ch = 'A'; ch <= 'Z'; ch++)
System.out.println(ch);
}
}
Ans:
public class p24
{
void prime(int n)
{
int count = 0;
for(int i = 1; i<=n; i++)
{
if(n % i == 0)
count++;
}
System.out.println("Entered Number is: " + n);
if(count == 2)
System.out.println(n + " is a prime no" );
else
System.out.println(n + " is NOT a prime no" );
}
}
public class MyClass
{
void perfect(int n)
{
int sum = 0;
for(int i = 1; i< n; i++)
{
if(n % i == 0)
sum = sum + i;
}
System.out.println("Entered Number is: " + n);
if(sum == n)
System.out.println(n + " is a perfect no" );
else
System.out.println(n + " is NOT a perfect no" );
}
}
Ans:
public class p26
{
void series(int x, int n)
{
double sum = 0;
for(int i = 0; i<= n; i+=2)
{
sum = sum + Math.pow(x,i);
}
System.out.println("Sum of the series is: " + sum);
}
}
Ans:
public class p27
{
void series()
{
int term = 0;
for(int i = 1; i<= 12; i++)
{
term = (int)Math.pow(i,2) - 1;
System.out.print(term + "\t");
}
}
}
Ans:
public class MyClass
{
void fibo()
{
int ft = 0;
int nt = 1;
int sum = 0;
System.out.print(ft + "\t" + nt + "\t");
for(int i = 1; i<= 12; i++)
{
sum = ft + nt;
ft = nt;
nt = sum;
System.out.print(sum + "\t");
}
}
}
Ans:
public class MyClass
{
void series(int num)
{
int temp = num, sum = 0;
while(temp > 0)
{
int digit = temp % 10;
sum = sum + digit;
temp = temp / 10;
}
System.out.println("Entered No is:" + num);
System.out.println("Sum of digits is:" + sum);
}
}
Ans:
public class MyClass
{
void series(int num)
{
int temp = num, rev = 0;
while(temp > 0)
{
int digit = temp % 10;
rev = rev * 10 + digit;
temp = temp / 10;
}
System.out.println("Entered No is:" + num);
System.out.println("Reverse is:" + rev);
}
}
Ans:
public class MyClass
{
void count(int num)
{
int temp = num, nod = 0, sum = 0, smallest = num % 10, digit = 0;
while(temp > 0)
{
digit = temp % 10;
nod++;
sum = sum + digit;
if(digit < smallest)
smallest = digit;
temp = temp / 10;
}
System.out.println("Entered No is: " + num);
System.out.println("No Of Digits is: " + nod);
System.out.println("Sum Of digits is: " + sum);
System.out.println("Smallest Digit in the No is: " + smallest);
}
}
Ans:
public class p32
{
void series(int num)
{
int temp = num, rev = 0;
while(temp > 0)
{
int digit = temp % 10;
rev = rev * 10 + digit;
temp = temp / 10;
}
if( rev == num)
System.out.println(num + " is a palindrome number");
else
System.out.println(num + " is not a palindrome number");
}
}
Ans:
int n = 9;
while(n <= 999)
{
if( n >= 10 && n <= 99)
System.out.println(n);
n++;
}
Ans:
public class MyClass
{
void series(int n, int m)
{
if(n < m)
{
for(int i = n; i <= m; i++)
{
if(i % 2 != 0)
System.out.println(i);
}
}
else
System.out.println("Limit is out of range");
}
}
Ans:
void show()
{
for(char ch = ‘a’; ch <= ‘z’; ch++)
System.out.println(ch);
}
Ans:
public class p36
{
void Armstrong(long num)
{
int temp = num, sum= 0;
while(temp > 0)
{
int digit = temp % 10;
sum = sum + (int)Math.pow(digit, 3);
temp = temp / 10;
}
if( sum == num)
System.out.println(num + " is an Armstrong number");
else
System.out.println(num + " is not an Armstrong number");
}
}
Ans:
public class p37
{
void hcf_method1(int n1, int n2)
{
int hcf = 1;
int max = Math.max(n1, n2);
for(int i = 1; i <= max; i++)
{
if(n1 % i == 0 && n2 % i == 0)
hcf = i;
}
System.out.println("HCF is: " + hcf);
}
static void hcf_method2(int n1, int n2)
{
while(n1 != n2)
{
if(n1 > n2)
n1 = n1 - n2;
else
n2 = n2 - n1;
}
System.out.println("HCF is: " + n1);
}
static void hcf_Method3(int n1,int n2)
{
int temp;
while (n2 > 0)
{
temp = n2;
n2 = n1 % n2;
n1 = temp;
}
System.out.println("HCF is: " + n1);
}
}
Ans:
public class p38
{
public int findLCM(int a, int b)
{
int lcm = Math.max(a, b);
while(true)
{
if(lcm % a == 0 && lcm % b == 0)
{
break;
}
lcm++;
}
System.out.println(lcm);
}
}
Ans:
Same as Q24.