Sheet 1 Solution
Sheet 1 Solution
Fall 24
Sheet 1
Intro to C Programming
Question 1:
Solution:
Question 2:
Write a program that reads two integers from the user then dis plays their sum, product, difference,
quotient and remainder.
Solution:
#include <stdio.h>
int main() {
int num1, num2, sum, product, difference, quotient, remainder;
// Performing calculations
sum = num1 + num2;
product = num1 * num2;
difference = num1 - num2;
quotient = num1 / num2;
remainder = num1 % num2;
CSE021: Introduction to Computer Programming
Fall 24
// Displaying results
printf("Sum: %d\n", sum);
printf("Product: %d\n", product);
printf("Difference: %d\n", difference);
printf("Quotient: %d\n", quotient);
printf("Remainder: %d\n", remainder);
return 0;
}
Question 3:
Write a program that displays the numbers 1 to 4 on the same line. Write the program using the
following methods.
Solution:
#include <stdio.h>
int main() {
printf("1 2 3 4\n");
printf("%d %d %d %d\n", 1, 2, 3, 4);
printf("%d ", 1);
printf("%d ", 2);
printf("%d ", 3);
printf("%d\n", 4);
return 0;
}
CSE021: Introduction to Computer Programming
Fall 24
Question 4:
Write a program that reads two integers from the user then displays the larger number followed by
the words “is larger.” If the numbers are equal, display the message “These numbers are equal.” Use
only the single-selection form of the if statement.
Solution:
#include <stdio.h>
int main() {
int num1, num2;
if (num1 == num2)
printf("These numbers are equal.\n");
return 0;
}
Question 5:
Write a program that inputs three different integers from the keyboard, then displays the sum, the
average, the product, the smallest and the largest of these numbers. Use only the single-selection
form of the if statement. The screen dialogue should ap pear as follows:
CSE021: Introduction to Computer Programming
Fall 24
Solution:
#include <stdio.h>
int main() {
int num1, num2, num3;
int sum, product, largest, smallest;
float average;
return 0;
}
CSE021: Introduction to Computer Programming
Fall 24
Question 6:
Write a program that reads an integer and determines and dis plays whether it’s odd or even. Use
the remainder operator. An even number is a multiple of two. Any multiple of two leaves a
remainder of zero when divided by 2.
Solution:
#include <stdio.h>
int main() {
int number;
if (number % 2 != 0)
printf("%d is odd.\n", number);
return 0;
}