0% found this document useful (0 votes)
27 views6 pages

1CSM1 PPSC T5 Week4 2024 25 Solutions

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)
27 views6 pages

1CSM1 PPSC T5 Week4 2024 25 Solutions

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

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING (AI & ML)

KAKATIYA INSTITUTE OF TECHNOLOGY & SCIENCE, WARANGAL


(An Autonomous Institute under Kakatiya University, Warangal)

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;

printf("Leap years from 1900 to 2022 are:\n");

// Loop through each year from 1900 to 2022


for (year = 1900; year <= 2022; year++) {
// Check if the year is a leap year
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
printf("%d\n", 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
}
}
}

// Display the result


if (is_prime) {
printf("%d is a prime number.\n", number);
} else {
printf("%d is not a prime number.\n", number);
}

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

Extra: Look ahead to formatting to print the values to only 3 decimal


places as shown.
Solution: #include <stdio.h>
#include <math.h>

int main() {
double number;

// Prompt for input


printf("Enter an integer greater than 2: ");
scanf("%lf", &number);

// Check if the number is greater than 2


if (number <= 2) {
printf("Please enter a number greater than 2.\n");
return 1; // Exit the program if the condition is not met
}

int count = 0; // Initialize count of operations

// Loop to take square root until the result is less than 2


while (number >= 2) {
number = sqrt(number); // Take the square root
count++; // Increment the operation count
printf("%d: %.3lf\n", count, number); // Print the count and the
square root value
}
return 0;
}
5. Write a program that prompts for a number and prints whether it is CO2 [Ap]
an Armstrong number or not. [Hint: An Armstrong number is a
number that is equal to the sum of its digits when each digit is raised
to the number of digits in the number.]
Solution: #include <stdio.h>
#include <math.h>

int main() {
int number, originalNumber, remainder, count = 0, result = 0;

// Prompt for input


printf("Enter a number: ");
scanf("%d", &number);

originalNumber = number; // Store the original number for later


comparison

// Calculate the number of digits


while (originalNumber != 0) {
originalNumber /= 10; // Remove the last digit
count++; // Increment digit count
}

originalNumber = number; // Reset original number

// Calculate the sum of the digits raised to the power of count


while (originalNumber != 0) {
remainder = originalNumber % 10; // Get the last digit
result += pow(remainder, count); // Raise to the power and add
to result
originalNumber /= 10; // Remove the last digit
}

// Check if the number is an Armstrong number


if (result == number) {
printf("%d is an Armstrong number.\n", number);
} else {
printf("%d is not an Armstrong number.\n", number);
}

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.

The algorithm is as follows. If A and B are the 2 integers (only


integers) to be multiplied, we repeatedly multiply A by 2 and divide B
by 2, until B cannot be divided any further, that is, until its value
becomes 0 (remember, this is integer division). During each step,
whenever B is an odd number, we add the corresponding A value to
the product we are generating. In the end, the sum of the A values
that had corresponding odd B values is the product. Get it?

Here is an example:

If the two integers to be multiplied are 34 and 19, the operations


would be:

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

// Prompt for input


printf("Enter two integers to multiply: ");
scanf("%d %d", &A, &B);

// Implement the Russian Peasant method


while (B > 0) {
// If B is odd, add A to the product
if (B % 2 != 0) {
product += A; // Add the current A to the product
}

// Multiply A by 2 and divide B by 2 (integer division)


A *= 2; // Double the value of A
B /= 2; // Halve the value of B (integer division)
}

// Print the final product


printf("The product is: %d\n", product);

return 0;
}
Course Faculty:
Dr. Narasimha Reddy Soora
HoD and Professor,
Dept of CSE (AI & ML)

You might also like