[go: up one dir, main page]

0% found this document useful (0 votes)
41 views49 pages

Assignment-4

xyze

Uploaded by

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

Assignment-4

xyze

Uploaded by

AniToo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 49

ASSIGNMENT-3

Name-Vinay
Class-10th-A
Roll no.-7
ASSIGNMENT-4
Name-Vinay
Class-10th-A
Roll no.-7
SOLVED
/**

* Write a program to find the sum of the series:

S= 1 + 1/2! + 1/3! + 1/4! + ........... to 1/n!

*/

import java.util.*;

public class Prg1_134

public static void main(String args[])

Scanner in = new Scanner(System.in);

int a,b,n;

double s=0;

System.out.println("Enter the value of n");

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;

System.out.println("The sum of the series = "+s);

}
/**

* Write a program to display all the prime numbers between 1 and 100.

(Prime number is a number which is divisible by 1 and the number itself

only.)

*/

public class Prg2_134

public static void main(String args[])

int a,b;

System.out.println("The prime numbers between 1 and 100 are:");

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);

}
/**

* Write a program to input a number and print whether the number is a

special number or not. A number is said to be special number if the sum

of the factorial of the digits of the number is the same as the original

number.

Sample Input: 145 is a special number because 1! + 4! + 5! = 1 + 24 +

120 = 145, where ! stands for the factorial of the number and the factorial

value of a number is the product of all integers from 1 to that number.

For example, 5!=1*2*3*4*5 = 120

*/

import java.util.*;

public class Prg3_135

public static void main(String args[])

int m,n,i,d,f=1,s=0;

Scanner in = new Scanner(System.in);

System.out.println("Enter a number :");

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)

System.out.println("The sum of factorial of digits ="+s);

System.out.println(n +" is a special number ");

else

System.out.println("The sum of factorial of digits ="+s);

System.out.println(n+" is not a special number ");

}
/**

* Write a program to display twenty simultaneous Prime Numbers, starting

from a given number, entered by the user.

Sample Input : 8

Sample Output: 11, 13, 17, 19,.........

*/

import java.util.*;

public class Prg4_136 {

public static void main(String rags[]) {

Scanner in = new Scanner(System.in);

int a,b,c,n,k=1;

System.out.println("Enter the first number");

n=in.nextInt();

System.out.println("Twenty prime numbers from "+n +":");

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);

}
/**

* A happy number is defined as:

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

number ends with 1 then it is called a happy number. Write a program

to input a number and check whether it a Happy Number or not. The

program displays a message accordingly.

Sample Input : 31

Solution : 31 ⇒ 3² + 1² = 10 --> 1² + 0 = 1

Sample Output: A Happy Number

*/

import java.util.*;

public class Prg5_137

public static void main(String args[])

Scanner in = new Scanner(System.in);

int num, sum=0,d;

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

System.out.println("Not a Happy Number");

}
/**

* The International Standard Book Number (ISBN) is a unique numeric

book identifier which is printed on every book. The ISBN is a 10 digit

code. The ISBN is legal if:

1xdigit, + 2xdigit, + 3xdigit, + 4xdigit, + 5xdigit, + 6xdigit, + 7xdigit,

+ 8xdigit, + 9xdigit, + 10xdigit,, is divisible by 11.

For example, For an ISBN 1401601499

Sum 1

1x1 + 2x4 + 3x0 + 4x1 + 5x6 + 6x0 + 7x1 + 8x4 + 9x9 + 10x9

= 253 which is divisible by 11.

(1) Input the ISBN code as a 10 digit integer.

(ii) If the ISBN is not a 10 digit integer, output the message, "Illegal

ISBN" and terminate the program.

(iii) If the number is 10 digits, extract the digits of the number and

compute the sum as explained above.

If the sum is divisible by 11, output the message, "Legal ISBN". If the

sum is not divisible by 11, output the message, "Illegal ISBN".

*/

import java.util.*;

public class Prg6_138

public static void main(String args[])

Scanner in = new Scanner(System.in);

int n,n1,i,r,sum=0,c=0;

System.out.println("Enter a 10 digit code");

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)

System.out.println("A valid ISBN code");

else

System.out.println("Not a valid ISBN code");

}
/**

* Write a program to display the Floyd's Triangle:

2 3

4 5 6

7 8 9 10

11 12 13 14 15

*/

public class Prg7_139

public static void main(String args[])

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();

}
/**

* Write a program in Java to display the given pattern

1234567

12345

123

*/

public class Prg8_139

public static void main(String args[])

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();

}
/**

* Write a program to display the given pattern:

54321

4321

321

21

*/

public class Prg9_140

public static void main(String args[])

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;

}
/**

* Write a program to display the given pattern:

12345

22345

33345

44445

55555

*/

public class Prg10_140

public static void main(String args[])

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;

}
/**

* Write a program in java to display the given pattern.

1234554321

1234 4321

123 321

12 21

1 1

*/

public class Prg11_141

public static void main(String args[])

int i,j,k,p,d;k=1;p=5;d=1;

System.out.println("The pattern of Triangle:");

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;

}
/**

* Write a program in java to display the given pattern.

1AAAAA

22BBBB

333CCC

4444DD

55555E

*/

public class Prg12_142

public static void main(String args[])

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
/**

* Q-1 Write programs to find the sum of the following series:

(a) S = 1 + (3/2!) + (5/3!) + (7/4!) + ....... to n

*/

import java.util.*;

public class Q1a_145

public static void main(String args[])

Scanner in = new Scanner(System.in);

System.out.print("Enter n: ");

int n = in.nextInt();

double sum = 0.0;

for (int i = 1, j = 1; i <= n; i++, j = j + 2) {

double f = 1;

for (int k = 1; k <= i; k++) {

f *= k;

sum += j / f;

System.out.println("Sum=" + sum);

}
/**

* Q-1 Write programs to find the sum of the following series:

(b) S = a + (a/2!) + (a/3!) + (a/4!) + ....... + (a/n!)

*/

import java.util.*;

public class Q1b_145

public static void main(String args[])

Scanner in = new Scanner(System.in);

System.out.print("Enter a: ");

int a = in.nextInt();

System.out.print("Enter n: ");

int n = in.nextInt();

double sum = 0;

for (int i = 1; i <= n; i++)

double f = 1;

for (int j = 1; j <= i; j++)

f *= j;

sum += a / f;

System.out.println("Sum=" + sum);

}
/**

* Q-1 Write programs to find the sum of the following series:

(c) S = a - (a/2!) + (a/3!) - (a/4!) + ....... to n

*/

import java.util.*;

public class Q1c_145

public static void main(String args[])

Scanner in = new Scanner(System.in);

System.out.print("Enter a: ");

int a = in.nextInt();

System.out.print("Enter n: ");

int n = in.nextInt();

double sum = 0;

for (int i = 1; i <= n; i++) {

double f = 1;

for (int j = 1; j <= i; j++)

f *= j;

if (i % 2 == 0)

sum -= a / f;

else

sum += a / f;

System.out.println("Sum=" + sum);

}
/**

* Q-1 Write programs to find the sum of the following series:

(d) S = (a/2!) - (a/3!) + (a/4!) - (a/5!) + ....... + (a/10!)

*/

import java.util.*;

public class Q1d_145

public static void main(String args[])

Scanner in = new Scanner(System.in);

System.out.print("Enter a: ");

int a = in.nextInt();

double sum = 0;

for (int i = 2; i <= 10; i++) {

double f = 1;

for (int j = 1; j <= i; j++) {

f *= j;

if (i % 2 == 0)

sum += a / f;

else

sum -= a / f;

System.out.println("Sum=" + sum);

}
/**

* Q-1 Write programs to find the sum of the following series:

(e) S = (2/a) + (3/a2) + (5/a3) + (7/a4) + ....... to n

*/

import java.util.*;

public class Q1e_145

public static void main(String args[])

Scanner in = new Scanner(System.in);

System.out.print("Enter a: ");

int a = in.nextInt();

System.out.print("Enter n: ");

int n = in.nextInt();

double sum = 0;

int lastPrime = 1;

for (int i = 1; i <= n; i++)

for (int j = lastPrime + 1; j <= Integer.MAX_VALUE; j++)

boolean isPrime = true;

for (int k = 2; k <= j / 2; k++)

if (j % k == 0) {

isPrime = false;

break;

}
if (isPrime) {

sum += j / Math.pow(a, i);

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.

For example: (5,7), (11,13), ....... and so on.

*/

import java.util.Scanner;

public class Q2_145

public static void main(String srgs[])

Scanner in = new Scanner(System.in);

System.out.print("Enter first number: ");

int a = in.nextInt();

System.out.print("Enter second number: ");

int b = in.nextInt();

int c=0,f=0;;

for (int i = 1; i <= a; i++)

if(a%i==0)

c++;

else

continue;
}

if (c==2 && a-b==2 || b-a==2)

for (int i = 1; i <= b; i++) {

if (b%i==0)

f++;

if (f==2)

System.out.println(a + " and " + b + " are twin prime");

else

System.out.println(a + " and " + b + " are not twin prime");

else

System.out.println(a + " and " + b + " are not twin prime");

}
/**

* Q-3 Write a program to display all the numbers between 100 and 200 which don't contain zeros at any
position.

For example: 111, 112, 113, ....... , 199

*/

public class Q3_146 {

public static void main(String args[]) {

int c = 0;

for (int i = 100; i <= 200; i++) {

boolean isNoZero = true;

int t = i;

while (t > 0) {

if (t % 10 == 0) {

isNoZero = false;

break;

t /= 10;

if (isNoZero) {

System.out.print(i + " ");

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.]

For example: 11, 101, 131, 151,

*/

public class Q4_146

public static void main(String args[])

int count = 0;

for (int i = 10; i <= 1000; i++) {

int num = i, revNum = 0;

while (num != 0) {

int digit = num % 10;

num /= 10;

revNum = revNum * 10 + digit;

if (revNum == i) {

boolean isPrime = true;

for (int j = 2; j <= i / 2; j++) {

if (i % j == 0) {

isPrime = false;

break;

}
}

if (isPrime) {

System.out.print(i + " ");

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;

public class Q5_146

public static void main(String args[])

Scanner in = new Scanner(System.in);

System.out.print("Enter number of students: ");

int studentCount = in.nextInt();

String names[] = new String[studentCount];

int engMarks[] = new int[studentCount];

int sciMarks[] = new int[studentCount];

int mathsMarks[] = new int[studentCount];

double avgMarks[] = new double[studentCount];

double totalMarks = 0.0;

for (int i = 0; i < studentCount; i++) {

System.out.println("Enter details of student " + (i + 1));

System.out.print("Name: ");

in.nextLine();

names[i] = in.nextLine();

System.out.print("Marks in English: ");

engMarks[i] = in.nextInt();

System.out.print("Marks in Science: ");


sciMarks[i] = in.nextInt();

System.out.print("Marks in Maths: ");

mathsMarks[i] = in.nextInt();

avgMarks[i] = (engMarks[i] + sciMarks[i] + mathsMarks[i]) / 3.0;

totalMarks += avgMarks[i];

System.out.println();

for (int i = 0; i < studentCount; i++) {

System.out.println("Details of student " + (i + 1));

System.out.println("Name: " + names[i]);

System.out.println("English: " + engMarks[i]);

System.out.println("Science: " + sciMarks[i]);

System.out.println("Maths: " + mathsMarks[i]);

System.out.println("Average: " + avgMarks[i]);

double classAvg = totalMarks / studentCount;

System.out.println("\nAverage of all students is " + classAvg);

}
/**

* 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 Input: Enter a number 4972

Sample Output: 2, 4, 7, 9

*/

import java.util.*;

public class Q6_146

public static void main(String args[])

Scanner in = new Scanner(System.in);

System.out.print("Enter a number having 3 or more digits: ");

int n = in.nextInt();

for (int i = 0; i <= 9; i++) {

int n1 = n;

int c = 0;

while (n1 != 0) {

if (n1 % 10 == i)

c++;

n1 /= 10;

for (int j = 1; j <= c; j++) {

System.out.print(i + ", ");

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

Sample Output: Hence, 55 is a Magic Number.

Similarly, 289 is a Magic Number.

*/

import java.util.*;

public class Q7_146 {

public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.print("Enter number to check: ");

int num = in.nextInt();

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)

System.out.println(num + " is Magic Number");

else

System.out.println(num + " is not Magic Number"); } }


/**

* 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').

Sample Input: 6804

Hint: 6804 ⇒ 6+8+0+4 = 18 ⇒ 6804/18 = 378

378 ⇒ 3+7+8= 18 ⇒ 378/18 = 21

21 ⇒ 2+1 = 3 ⇒ 21/3 = 7

Sample Output: Multiple Harshad Number

*/

import java.util.*;

public class Q8_146

public static void main(String args[])

Scanner in = new Scanner(System.in);

System.out.print("Enter number to check: ");

int num = in.nextInt();

int dividend = num;

int divisor;

int count = 0;

while (dividend > 1) {

divisor=0;

int t = dividend;

while (t > 0) {

int d = t % 10;

divisor += d;
t /= 10;

if (dividend % divisor == 0 && divisor != 1) {

dividend = dividend / divisor;

count++;

else {

break;

if (dividend == 1 && count > 1)

System.out.println(num + " is Multiple Harshad Number");

else

System.out.println(num + " is not Multiple Harshad Number");

}
/**

* Q-9 Write the programs to display the following patterns:

a)

31

531

7531

97531

*/

public class Q9a_146

public static void main(String args[])

for (int i = 1; i < 10; i = i + 2) {

for (int j = i; j > 0; j = j - 2) {

System.out.print(j + " ");

System.out.println();

}
/**

* Q-9 Write the programs to display the following patterns:

(b)

1 2 3 4 5

6 7 8 9

10 11 12

13 14

15

*/

public class Q9b_146

public static void main(String args[])

int a = 1;

for (int i = 5; i > 0; i--) {

for (int j = 1; j <= i; j++) {

System.out.print(a++ + "\t");

System.out.println();

}
/**

* Q-9 Write the programs to display the following patterns:

(c)

15 14 13 12 11

10 9 8 7

6 5 4

3 2

*/

public class Q9c_146

public static void main(String args[])

int a = 15;

for (int i = 5; i > 0; i--) {

for (int j = 1; j <= i; j++) {

System.out.print(a-- + "\t");

System.out.println();

}
/**

* Q-9 Write the programs to display the following patterns:

(d)

10

101

1010

10101

*/

public class Q9d_146

public static void main(String args[])

int a = 1, b = 0;

for (int i = 1; i <= 5; i++) {

for (int j = 1; j <= i; j++) {

if (j % 2 == 0)

System.out.print(b + " ");

else

System.out.print(a + " ");

System.out.println();

}
/**

* Q-9 Write the programs to display the following patterns:

(e)

55555

4444

333

22

*/

public class Q9e_146

public static void main(String args[])

for (int i = 0; i < 5; i++) {

for (int j = i; j > 0; j--) {

System.out.print(" ");

for (int k = 5 - i; k > 0; k--) {

System.out.print((5 - i) + " ");

System.out.println();

}
/**

* Q-9 Write the programs to display the following patterns:

(f)

12345

22345

33345

44445

55555

*/

public class Q9f_146

public static void main(String args[])

for (int i = 1; i <= 5; i++) {

for (int j = 1; j < i; j++)

System.out.print(i + " ");

for (int k = i; k <= 5; k++)

System.out.print(k + " ");

System.out.println();

}
/**

* Q-9 Write the programs to display the following patterns:

(g)

*#

*#*

*#*#

*#*#*

*/

public class Q9g_147

public static void main(String args[])

for (int i = 1; i <= 5; i++) {

for (int j = 1; j <= i; j++) {

if (j % 2 == 0)

System.out.print("# ");

else

System.out.print("* ");

System.out.println();

}
/**

* Q-9 Write the programs to display the following patterns:

(h)

54321

5432

543

54

*/

public class Q9h_147

public static void main(String args[])

for (int i = 1; i <= 5; i++) {

for (int j = 5; j >= i; j--) {

System.out.print(j + " ");

System.out.println();

}
/**

* Q-9 Write the programs to display the following patterns:

(i)

2 3

4 5 6

7 8 9 10

11 12 13 14 15

*/

public class Q9i_147

public static void main(String args[])

int a = 1;

for (int i = 1; i <= 5; i++) {

for (int j = 1; j <= i; j++) {

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

Type 2 for an inverted triangle

Enter your choice 1

Enter the number of terms 5

Sample Output:

22

333

4444

55555

Example 2:

Input: Type 1 for a triangle and

Type 2 for an inverted triangle

Enter your choice 2

Enter the number of terms 6

Sample Output:

666666

55555

4444

333

22

*/

import java.util.*;

public class Q10_147 {

public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.println("Type 1 for a triangle");

System.out.println("Type 2 for an inverted triangle");


System.out.print("Enter your choice: ");

int ch = in.nextInt();

System.out.print("Enter the number of terms: ");

int n = in.nextInt();

switch (ch) {

case 1:

for (int i = 1; i <= n; i++) {

for (int j = 1; j <= i; j++) {

System.out.print(i + " ");

System.out.println();

break;

case 2:

for (int i = n; i > 0; i--) {

for (int j = 1; j <= i; j++) {

System.out.print(i + " ");

System.out.println();

break;

default:

System.out.println("Incorrect Choice");

}
/**

* Q-11 Using the switch statement, write a menu driven program for the following:

(a) To print the Floyd's triangle:

2 3

4 5 6

7 8 9 10

11 12 13 14 15

(b) To display the following pattern:

IC

ICS

ICSE

For an incorrect option, an appropriate error message should be displayed.

*/

import java.util.*;

public class Q11_147

public static void main(String args[])

Scanner in = new Scanner(System.in);

System.out.println("Type 1 for Floyd's triangle");

System.out.println("Type 2 for an ICSE pattern");

System.out.print("Enter your choice: ");

int ch = in.nextInt();
switch (ch) {

case 1:

int a = 1;

for (int i = 1; i <= 5; i++) {

for (int j = 1; j <= i; j++) {

System.out.print(a++ + "\t");

System.out.println();

break;

case 2:

String s = "ICSE";

for (int i = 0; i < s.length(); i++) {

for (int j = 0; j <= i; j++) {

System.out.print(s.charAt(j) + " ");

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.*;

public class Q12_147

public static void main(String args[])

Scanner in = new Scanner(System.in);

System.out.println("Enter 1 for prime factors");

System.out.println("Enter 2 for multiplication tables");

System.out.print("Enter your choice: ");

int ch = in.nextInt();

switch (ch) {

case 1:
System.out.print("Enter a number: ");

int num = in.nextInt();

for(int i = 2; i <= num/2; i++) {

int c = 0;

if(num % i == 0) {

for (int j = 1; j <= i; j++) {

if(i % j == 0)

c++;

if(c == 2)

System.out.print(i + " ");

break;

case 2:

for (int i = 1; i <= 10; i++) {

for (int j = 1; j <= 10; j++)

System.out.print((i * j) + " ");

System.out.println();

break;

default:

System.out.println("Incorrect Choice");

You might also like