0% found this document useful (0 votes)
18 views10 pages

PRactice Probelms

Uploaded by

Vivaan Grover
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)
18 views10 pages

PRactice Probelms

Uploaded by

Vivaan Grover
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
You are on page 1/ 10

Pr 1.

Write a program to prompt the user to enter a number and then print their name that many
times.

java
Copy code
int times = IBIO.inputInt("Enter the number of times: ");
for (int i = 0; i < times; i++) {
IBIO.output("Your Name"); // Replace "Your Name" with the actual
name you want to print.
}

Pr 1.2

Write a program to allow the user to input the number of steps, starting point, and increment,
then print the sequence.

java
Copy code
int steps = IBIO.inputInt("Enter number of steps: ");
int start = IBIO.inputInt("Enter starting number: ");
int increment = IBIO.inputInt("Enter increment: ");

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


IBIO.output(start);
start += increment;
}

Example output if input is steps = 4, start = 3, increment = 2:

Copy code
3
5
7
9
Pr 1.3

Write a program to print the first 10 numbers, their squares, and their cubes.

java
Copy code
for (int i = 1; i <= 10; i++) {
int square = i * i;
int cube = i * i * i;
IBIO.output(i + " " + square + " " + cube);
}

Example output:

python
Copy code
1 1 1
2 4 8
3 9 27
...
10 100 1000

Pr 1.4

Write a program to display the first 100 terms of the triangular sequence (1, 3, 6, 10, 15, 21, ...).

java
Copy code
int term = 1;
int increment = 2;

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


IBIO.output(term);
term += increment;
increment++;
}
Pr 1.5

Write a program to display the first 20 powers of 2, showing both the number and its power.

java
Copy code
int value = 1;

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


IBIO.output("2^" + i + " = " + value);
value *= 2;
}

Example output:

python
Copy code
2^0 = 1
2^1 = 2
2^2 = 4
...
2^19 = 524288

Pr 1.6

Write a program to display a Fibonacci sequence based on the number of terms specified by the
user.

java
Copy code
int terms = IBIO.inputInt("Enter the number of Fibonacci terms: ");
int first = 3;
int second = 4;

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


IBIO.output(first);
int next = first + second;
first = second;
second = next;
}

Example output if terms = 6:

Copy code
3
4
7
11
18
29

Pr 2.2

Write a program that lets the user enter a number and responds with "EVEN" if the number is
even and "ODD" if the number is odd.

java
Copy code
int number = IBIO.inputInt("Enter a number: ");
if (number % 2 == 0) {
IBIO.output("EVEN");
} else {
IBIO.output("ODD");
}

Pr 2.3

Modify the previous program to use an else command to determine if the number is even or
odd.

java
Copy code
int number = IBIO.inputInt("Enter a number: ");
if (number % 2 == 0) {
IBIO.output("EVEN");
} else {
IBIO.output("ODD");
}

This solution is similar to Pr 2.2 but emphasizes the use of else.

Pr 2.4

Write a program to print out the cubes of numbers from 1 to 10, aligning them to the right as
shown in the example.

java
Copy code
for (int i = 1; i <= 10; i++) {
int cube = i * i * i;
if (i < 10) {
IBIO.output(" " + cube); // 2 spaces before single-digit
numbers
} else {
IBIO.output(" " + cube); // 1 space before double-digit
numbers
}
}

Example output:

yaml
Copy code
1
8
27
64
125
216
343
512
729
1000

Pr 2.5

Write a program to print out numbers from 1 to 100, skipping all even numbers.

java
Copy code
for (int i = 1; i <= 100; i++) {
if (i % 2 != 0) {
IBIO.output(i);
}
}

This prints only odd numbers between 1 and 100.

Pr 2.6

Write a program to print numbers from 1 to 100, omitting all even numbers and all numbers
divisible by 3.

java
Copy code
for (int i = 1; i <= 100; i++) {
if (i % 2 != 0 && i % 3 != 0) {
IBIO.output(i);
}
}

Pr 2.7

Modify the program in Pr 2.6 to use && instead of separate if statements.

java
Copy code
for (int i = 1; i <= 100; i++) {
if (i % 2 != 0 && i % 3 != 0) {
IBIO.output(i);
}
}

This is the same code as in Pr 2.6 since it already uses &&.

Pr 2.8

Write a program to count all numbers from 1 to 1,000,000 that are not divisible by 2, 3, 5, or 7,
and output the result.

java
Copy code
int count = 0;

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


if (i % 2 != 0 && i % 3 != 0 && i % 5 != 0 && i % 7 != 0) {
count++;
}
}

IBIO.output("The count is: " + count);

The expected output should be:

csharp
Copy code
The count is: 228571

Pr 3.1

Modify the given program to only accept even numbers that are larger than 0 and less than 100.

java
Copy code
int x;
do {
x = IBIO.inputInt("Enter an even number between 0 and 100: ");
} while (x <= 0 || x >= 100 || x % 2 != 0);
IBIO.output("Thank you");

This program will keep prompting the user until they enter a valid even number between 1 and
99.

Pr 3.2

Modify the program to only accept numbers greater than 1. If the number is prime, output
"Prime"; otherwise, output the smallest number that divides it.

java
Copy code
int x = IBIO.inputInt("Enter a number greater than 1: ");
int i = 2;
boolean isPrime = true;

while (i <= Math.sqrt(x)) {


if (x % i == 0) {
IBIO.output(x + " is divisible by " + i);
isPrime = false;
break;
}
i++;
}

if (isPrime) {
IBIO.output(x + " is prime");
}

This code checks if the number is prime. If it finds a divisor, it outputs the smallest one;
otherwise, it confirms the number is prime.

Pr 3.3
Modify the digit sum program to add up the cubes of the digits.

java
Copy code
int sum = 0;
int n = IBIO.inputInt("Enter a number: ");

do {
int digit = n % 10;
sum += digit * digit * digit; // add cube of digit
n /= 10;
} while (n != 0);

IBIO.output("The sum of the cubes of the digits is " + sum);

For example, if the input is 345, the output will be 33+43+53=216+64+125=4053^3 + 4^3 + 5^3
= 216 + 64 + 125 = 40533+43+53=216+64+125=405.

Pr 3.4

Write a program that starts with a given number and continues the sequence according to
specific rules, counting the steps until it reaches 1.

java
Copy code
int n = IBIO.inputInt("Enter a starting number: ");
int steps = 0;

while (n != 1) {
if (n % 2 == 0) {
n /= 2; // if even, halve the number
} else {
n = n * 3 + 1; // if odd, multiply by 3 and add 1
}
steps++;
}

IBIO.output("The sequence took " + steps + " steps to reach 1");


This implements the "Collatz conjecture" sequence (or "3n + 1" problem), where the sequence
is calculated until n reaches 1, and it outputs the number of steps taken.

You might also like