Assignment-4
Assignment-4
Name-Vinay
Class-10th-A
Roll no.-7
ASSIGNMENT-4
Name-Vinay
Class-10th-A
Roll no.-7
SOLVED
/**
*/
import java.util.*;
int a,b,n;
double s=0;
n=in.nextInt();
for(a=1;a<=n;a++)
int f=1;
for(b=1;b<=a;b++)
f=f*b;
s=s+(double)1/f;
}
/**
* Write a program to display all the prime numbers between 1 and 100.
only.)
*/
int a,b;
for(a=1;a<=100;a++)
int c=0;
for(b=1;b<=a;b++)
if(a%b==0)
c=c+1;
if(c==2)
System.out.println(a);
}
/**
of the factorial of the digits of the number is the same as the original
number.
120 = 145, where ! stands for the factorial of the number and the factorial
*/
import java.util.*;
int m,n,i,d,f=1,s=0;
n=in.nextInt();
m=n;
while(m!=0)
d=m%10;
for(i=1;i<=d;i++)
f=f*i;
s=s+f;
f=1;
m=m/10;
}
if(s==n)
else
}
/**
Sample Input : 8
*/
import java.util.*;
int a,b,c,n,k=1;
n=in.nextInt();
do {
c=0;
for(a=1;a<=n;a++) {
if(n%a==0)
c=c+1;
if(c==2) {
System.out.println(n+" ");
k=k+1;
n=n+1;
while(k<=20);
}
/**
Take a positive number and replace the number by the sum of the squares
of its digits. Repeat the process until the number equals 1 (one). If the
Sample Input : 31
Solution : 31 ⇒ 3² + 1² = 10 --> 1² + 0 = 1
*/
import java.util.*;
System.out.println("Enter a number");
num = in.nextInt();
sum=num;
do
num=sum;sum=0;
do
d=num%10;
sum=sum+d*d;
num=num/10;
}
while(num>0);
while(sum>9);
if(sum==1)
System.out.println("Happy Number");
else
}
/**
Sum 1
1x1 + 2x4 + 3x0 + 4x1 + 5x6 + 6x0 + 7x1 + 8x4 + 9x9 + 10x9
(ii) If the ISBN is not a 10 digit integer, output the message, "Illegal
(iii) If the number is 10 digits, extract the digits of the number and
If the sum is divisible by 11, output the message, "Legal ISBN". If the
*/
import java.util.*;
int n,n1,i,r,sum=0,c=0;
n=in.nextInt();
n1=n;
while(n1>0)
{
n1=n1/10;
c++;
if(c<10|| c>10)
System.out.println("Illegal ISBN");
else
for(i=10;i>=1;i--)
r=n%10;
sum=sum+r*i;
n=n/10;
if(sum%11==0)
else
}
/**
2 3
4 5 6
7 8 9 10
11 12 13 14 15
*/
int a,b,p=0;
System.out.println("Floyd's Triangle:");
for(a=1;a<=5;a++)
for(b=1;b<=a;b++)
p=p+1;
System.out.print(p+" ");
System.out.println();
}
/**
1234567
12345
123
*/
int i,j;
System.out.println("The Pattern:");
for(i=7;i>=1;i=i-2)
for(j=1;j<=i;j++)
System.out.print(j+" ");
System.out.println();
}
/**
54321
4321
321
21
*/
int i,j,k,p=1;
System.out.println("The Pattern:");
for(i=5;i>=1;i--)
for(k=1;k<=p;k++)
System.out.print(" ");
for(j=i;j>=1;j--)
System.out.print(j);
System.out.println();
p=p+1;
}
/**
12345
22345
33345
44445
55555
*/
int a,b,c,p=2;
System.out.println("The Pattern:");
for(a=1;a<=5;a++)
for(b=1;b<=a;b++)
System.out.print(a);
for(c=p;c<=5;c++)
System.out.print(c);
System.out.println();
p=p+1;
}
/**
1234554321
1234 4321
123 321
12 21
1 1
*/
int i,j,k,p,d;k=1;p=5;d=1;
for(i=1;i<=5;i++)
for(j=1;j<=p;j++)
System.out.print(j);
for(k=1;k<=d;k++)
System.out.print(" ");
for(k=p;k>=1;k--)
System.out.print(k);
System.out.println();
p=p-1;d=d+2;
}
/**
1AAAAA
22BBBB
333CCC
4444DD
55555E
*/
int a,b,c,d,p,k;p=65;k=5;
System.out.println("The pattern:");
for(a=1;a<=5;a++)
for(b=1;b<=a;b++)
System.out.print(a+" ");
for(c=k;c>=1;c--)
System.out.print((char)p+" ");
k=k-1;
p=p+1;
System.out.println();
}
UNSOLVED
/**
*/
import java.util.*;
System.out.print("Enter n: ");
int n = in.nextInt();
double f = 1;
f *= k;
sum += j / f;
System.out.println("Sum=" + sum);
}
/**
*/
import java.util.*;
System.out.print("Enter a: ");
int a = in.nextInt();
System.out.print("Enter n: ");
int n = in.nextInt();
double sum = 0;
double f = 1;
f *= j;
sum += a / f;
System.out.println("Sum=" + sum);
}
/**
*/
import java.util.*;
System.out.print("Enter a: ");
int a = in.nextInt();
System.out.print("Enter n: ");
int n = in.nextInt();
double sum = 0;
double f = 1;
f *= j;
if (i % 2 == 0)
sum -= a / f;
else
sum += a / f;
System.out.println("Sum=" + sum);
}
/**
*/
import java.util.*;
System.out.print("Enter a: ");
int a = in.nextInt();
double sum = 0;
double f = 1;
f *= j;
if (i % 2 == 0)
sum += a / f;
else
sum -= a / f;
System.out.println("Sum=" + sum);
}
/**
*/
import java.util.*;
System.out.print("Enter a: ");
int a = in.nextInt();
System.out.print("Enter n: ");
int n = in.nextInt();
double sum = 0;
int lastPrime = 1;
if (j % k == 0) {
isPrime = false;
break;
}
if (isPrime) {
lastPrime = j;
break;
System.out.println("Sum=" + sum);
}
/**
* Q-2 Write a program to input two numbers and check whether they are twin prime numbers or not.
Hint: Twin prime numbers are the prime numbers whose difference is 2.
*/
import java.util.Scanner;
int a = in.nextInt();
int b = in.nextInt();
int c=0,f=0;;
if(a%i==0)
c++;
else
continue;
}
if (b%i==0)
f++;
if (f==2)
else
else
}
/**
* Q-3 Write a program to display all the numbers between 100 and 200 which don't contain zeros at any
position.
*/
int c = 0;
int t = i;
while (t > 0) {
if (t % 10 == 0) {
isNoZero = false;
break;
t /= 10;
if (isNoZero) {
c++;
if (c == 10) {
System.out.println();
c = 0;
}
/**
* Q-4 Write a program to display all prime palindrome numbers between 10 and 1000.
[Hint: A number which is prime as well a palindrome is said to be 'Prime Palindrome' number.]
*/
int count = 0;
while (num != 0) {
num /= 10;
if (revNum == i) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
count++;
if (count == 10) {
System.out.println();
count = 0;
}
/**
* Q-5 In an entrance examination, students have been appeared in English, Maths and Science papers.
Write a program to calculate and display average marks obtained by all the students.
Take number of students appeared and marks obtained in all three subjects by every student along with
the name as inputs.
Display the name, marks obtained in three subjects and the average of all the students.
*/
import java.util.Scanner;
System.out.print("Name: ");
in.nextLine();
names[i] = in.nextLine();
engMarks[i] = in.nextInt();
mathsMarks[i] = in.nextInt();
totalMarks += avgMarks[i];
System.out.println();
}
/**
* Q-6 Write a program in Java to enter a number containing three digits or more.
Arrange the digits of the entered number in ascending order and display the result.
Sample Output: 2, 4, 7, 9
*/
import java.util.*;
int n = in.nextInt();
int n1 = n;
int c = 0;
while (n1 != 0) {
if (n1 % 10 == i)
c++;
n1 /= 10;
System.out.println();
}
/**
* Q-7 Write a program to input a number and check whether it is 'Magic Number' or not. Display the
message accordingly.
A number is said to be a magic number if the eventual sum of digits of the number is one.
Sample Input : 55
Then, 5 + 5 = 10, 1 + 0 = 1
*/
import java.util.*;
int n = num;
while (n > 9) {
int sum = 0;
while (n != 0) {
int d = n % 10;
n /= 10;
sum += d;
n = sum;
if (n == 1)
else
* Q-8 A number is said to be Multiple Harshad number, when divided by the sum of its digits, produces
another 'Harshad Number'.
Write a program to input a number and check whether it is a Multiple Harshad Number or not.
(When a number is divisible by the sum of its digit, it is called 'Harshad Number').
21 ⇒ 2+1 = 3 ⇒ 21/3 = 7
*/
import java.util.*;
int divisor;
int count = 0;
divisor=0;
int t = dividend;
while (t > 0) {
int d = t % 10;
divisor += d;
t /= 10;
count++;
else {
break;
else
}
/**
a)
31
531
7531
97531
*/
System.out.println();
}
/**
(b)
1 2 3 4 5
6 7 8 9
10 11 12
13 14
15
*/
int a = 1;
System.out.print(a++ + "\t");
System.out.println();
}
/**
(c)
15 14 13 12 11
10 9 8 7
6 5 4
3 2
*/
int a = 15;
System.out.print(a-- + "\t");
System.out.println();
}
/**
(d)
10
101
1010
10101
*/
int a = 1, b = 0;
if (j % 2 == 0)
else
System.out.println();
}
/**
(e)
55555
4444
333
22
*/
System.out.print(" ");
System.out.println();
}
/**
(f)
12345
22345
33345
44445
55555
*/
System.out.println();
}
/**
(g)
*#
*#*
*#*#
*#*#*
*/
if (j % 2 == 0)
System.out.print("# ");
else
System.out.print("* ");
System.out.println();
}
/**
(h)
54321
5432
543
54
*/
System.out.println();
}
/**
(i)
2 3
4 5 6
7 8 9 10
11 12 13 14 15
*/
int a = 1;
System.out.print(a++ + "\t");
System.out.println();
/**
* Q-10
Write a program to generate a triangle or an inverted triangle till n terms based upon the user's choice.
Example 1:
Input: Type 1 for a triangle and
Sample Output:
22
333
4444
55555
Example 2:
Sample Output:
666666
55555
4444
333
22
*/
import java.util.*;
int ch = in.nextInt();
int n = in.nextInt();
switch (ch) {
case 1:
System.out.println();
break;
case 2:
System.out.println();
break;
default:
System.out.println("Incorrect Choice");
}
/**
* Q-11 Using the switch statement, write a menu driven program for the following:
2 3
4 5 6
7 8 9 10
11 12 13 14 15
IC
ICS
ICSE
*/
import java.util.*;
int ch = in.nextInt();
switch (ch) {
case 1:
int a = 1;
System.out.print(a++ + "\t");
System.out.println();
break;
case 2:
String s = "ICSE";
System.out.println();
break;
default:
System.out.println("Incorrect Choice");
}
/**
* Q-12 Using the switch case statement, write a menu driven program for the following:
(a) To input a number and display only those factors of the numbers which are prime.
Sample Input: 84
Sample Output: 2, 3, 7
(b) A program that displays the multiplication table from 1 to 10, as shown:
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
...................................
...................................
...................................
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
*/
import java.util.*;
int ch = in.nextInt();
switch (ch) {
case 1:
System.out.print("Enter a number: ");
int c = 0;
if(num % i == 0) {
if(i % j == 0)
c++;
if(c == 2)
break;
case 2:
System.out.println();
break;
default:
System.out.println("Incorrect Choice");