WAP To Find The Sum of The Series S 1 + (3/2!) + (5/3!) + (7/4!) + ....... To N

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

PROJECT WORK

Project work

Pg. 135 write in shoe lace file as shown with questions as comment , prg soln and variable desc.

PROGRAM 1a

/*
*WAP to find the sum of the series
*S = 1 + (3/2!) + (5/3!) + (7/4!) + ....... to n
**/

import java.util.Scanner;

public class Program1a


{
public void computeSum()
{
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);
}
}
Variable desc
Variables Type Desc
n int Inputted number
to print series
i int Loop variable
f double Stores factorial
k int Loop variable
sum double sum
1b. 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 project1b
{
public static void main(){

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

Var desc1b

2. 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.Scanner;

public class Project2


{
public static void main() {

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

3. WAP to generate the following patterns:

a.

55555
   4 4 4 4
      3 3 3
         2 2
            1
public class Project3a
{
public void displayPattern() {
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();
}
}
}

Output

b.

12345
22345
33345
44445
55555
public class Project3b
{
public void displayPattern() {
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();
}
}
}

4. Using the switch statement, write a menu driven program for the following:

(a) To print the Floyd's triangle:


1
2   3
4   5   6
7   8   9   10
11 12 13 14 15

(b) To display the following pattern:


I
IC
ICS
ICSE

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


import java.util.Scanner;

public class Project4


{
public static void main() {
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");
}
}
}
5. Using nested loop generate a Pascal’s Triangle
/**
* Program to generate a Pascals triangle of asterisk with the given input
*/
public class Project5
{
// Method 1
// To print upper part of the pattern

private static void displayUpperPart(int size)


{
// Declaring variables for rows and columns
// respectively
int m, n;

// Outer loop for rows


for (m = size - 1; m >= 0; m--)
{

// Inner loop 1
for (n = 0; n < m; n++) {

// Printing whitespace
System.out.print(" ");
}

// Inner loop 2
for (n = m; n <= size - 1; n++)
{
// Printing star with whitespace
System.out.print("*" + " ");
}

// By now we are done with one row so new line


System.out.println();
}
}

// Method 2
// To print lower part of the pattern
private static void displayLowerPart(int size)
{
// Declaring variables for rows and columns respectively
int m, n;

// Outer loop for rows


for (m = 1; m <= size; m++) {

// Inner loop 1
for (n = 1; n < m; n++) {

// Printing whitespace
System.out.print(" ");
}

// Inner loop 2
for (n = m; n <= size; n++) {

// Printing star and whitespace


System.out.print("*" + " ");
}

// By now we are done with one row so new line


System.out.println();
}
}

// Method 3
// Main driver method
public static void main(String[] args)
{
// Declaring and initializing variable to
// size of the triangle
int size = 7;

// Calling Method 1 to
// display the upper part
displayUpperPart(size);

// Calling Method 2 to
// display lower part
displayLowerPart(size);
}
}
PROJECT =5 PROGRAMS

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy