1CSM1 PPSC T5 Week4 2024 25 Solutions
1CSM1 PPSC T5 Week4 2024 25 Solutions
Tutorial 5 (T5)
(UNIT – II)
U24AI104 –
Course Code – Name Programming for Branch – Section CSE (AI & ML)
Problem Solving with C
Tutorial Sheet posted on Tutorial class scheduled 15.10.2024 (B1) and
04.10.2024
CourseWeb on on 17.10.2024 (B2)
Conditional Operator, Decision making and looping: While, for, do while loops,
Topics covered
nested loops.
Problem
Class Answer the following questions. CO CDLL
No
T5 1. Develop a C Program to print all leap years from 1900 to 2022 CO2 [Ap]
(inclusive).
Solution: #include <stdio.h>
int main() {
int year;
return 0;
}
2. Develop a C program to print the following arithmetic examples. CO2 [Ap]
1*8+1=9
12 * 8 + 2 = 98
123 * 8 + 3 = 987
1234 * 8 + 4 = 9876
12345 * 8 + 5 = 98765
123456 * 8 + 6 = 987654
1234567 * 8 + 7 = 9876543
12345678 * 8 + 8 = 98765432
123456789 * 8 + 9 = 987654321
1 * 9 + 2 = 11
12 * 9 + 3 = 111
123 * 9 + 4 = 1111
1234 * 9 + 5 = 11111
12345 * 9 + 6 = 111111
123456 * 9 + 7 = 1111111
1234567 * 9 + 8 = 11111111
12345678 * 9 + 9 = 111111111
123456789 * 9 + 10 = 1111111111
Solution: #include <stdio.h>
int main() {
int i;
// For multiplier 8
printf("Arithmetic examples with multiplier 8:\n");
for (i = 1; i <= 9; i++) {
// Calculate the left-hand side and right-hand side of the
equation
int left = (i * 10 + (i - 1)) * 8 + i;
printf("%d * 8 + %d = ", i * 10 + (i - 1), i);
// Generate the right-hand side result
for (int j = i; j >= 1; j--) {
printf("%d", j);
}
printf("\n");
}
printf("\n");
// For multiplier 9
printf("Arithmetic examples with multiplier 9:\n");
for (i = 1; i <= 10; i++) {
// Calculate the left-hand side and right-hand side of the
equation
int left = (i * 10 + (i - 1)) * 9 + (i + 1);
printf("%d * 9 + %d = ", i * 10 + (i - 1), i + 1);
// Generate the right-hand side result
for (int j = i + 1; j >= 1; j--) {
printf("%d", 1);
}
printf("\n");
}
return 0;
}
3. Develop a C program that checks to see if a number N is prime. A CO2 [Ap]
simple approach checks all numbers from 2 up to N, but after some
point numbers are checked that need not be checked. For example,
numbers greater than √N need not be checked. Write a program that
checks for primality and avoids those unnecessary checks.
Solution: #include <stdio.h>
#include <math.h> // Include math.h for the sqrt function
int main() {
int number, is_prime = 1; // Assume the number is prime initially
printf("Enter a number to check if it is prime: ");
scanf("%d", &number);
if (number <= 1) {
is_prime = 0; // Numbers less than or equal to 1 are not prime
} else if (number <= 3) {
is_prime = 1; // 2 and 3 are prime numbers
} else if (number % 2 == 0 || number % 3 == 0) {
is_prime = 0; // Exclude multiples of 2 and 3
} else {
// Check for factors from 5 to sqrt(number)
for (int i = 5; i <= sqrt(number); i += 6) {
if (number % i == 0 || number % (i + 2) == 0) {
is_prime = 0; // Found a factor, not prime
break; // No need to check further
}
}
}
return 0;
}
4. Create a program that prompts for a positive number greater than 2 CO2 [Ap]
(check this condition), then keeps taking the square root of this
number until the square root is less than 2. Print the value each time
the square root is taken, along with the number of times the operation
has been completed.
For example:
Enter an integer greater than 2: 20
1: 4.472
2: 2.115
3: 1.454
int main() {
double number;
int main() {
int number, originalNumber, remainder, count = 0, result = 0;
return 0;
}
6. For this assignment, you will be implementing the so-called “Russian CO2 [Ap]
Peasant” or “Ancient Egyptian” method for multiplication. It looks a
little odd, but just think of it as an algorithm, a recipe for doing
multiplication in a way other than what you learned in grade school.
Here is an example:
A B Comment
34 19 Add A to the product, B is odd
68 9 Add A to the product, B is odd
136 4 Ignore this A value, B is even
272 2 Ignore this A value, B is even
544 1 Add A to the product, B is odd
Sum up all the A values that had odd B values and you get:
34+68+544 = 646 => Final product.
Solution: #include <stdio.h>
int main() {
int A, B; // Variables to store the two integers
int product = 0; // Variable to store the final product
return 0;
}
Course Faculty:
Dr. Narasimha Reddy Soora
HoD and Professor,
Dept of CSE (AI & ML)