0% found this document useful (0 votes)
14 views5 pages

Sheet 1 Solution

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)
14 views5 pages

Sheet 1 Solution

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

CSE021: Introduction to Computer Programming

Fall 24

Sheet 1
Intro to C Programming

Question 1:

Write a single C statement or line that accomplishes each of the following:

a) Display the message “Enter two numbers”.


b) Assign the product of variables b and c to variable a.
c) State that a program performs a sample payroll calculation (i.e., use text that helps to
document a program).
d) Input three integer values and place them in int variables a, b and c.

Solution:

a) printf(“Enter two numbers”);


b) a = b * c;
c) // Sample payroll calculation program
d) scanf(%d%d%d, &a, &b, &c);

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;

// Asking user to input two integers


printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);

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

a) Using one printf statement with no conversion specifications.


b) Using one printf statement with four conversion specifications.
c) Using four printf statements.

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;

// Asking user to input two integers


printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);

if (num1 > num2)


printf("%d is larger.\n", num1);

if (num2 > num1)


printf("%d is larger.\n", 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;

// Asking user to input three different integers


printf("Enter three different integers: ");
scanf("%d %d %d", &num1, &num2, &num3);

// Calculating the sum, product, and average


sum = num1 + num2 + num3;
product = num1 * num2 * num3;
average = sum / 3.0;

// Finding the largest number using single-selection if statements


largest = num1;
if (num2 > largest)
largest = num2;
if (num3 > largest)
largest = num3;

// Finding the smallest number using single-selection if statements


smallest = num1;
if (num2 < smallest)
smallest = num2;
if (num3 < smallest)
smallest = num3;

// Displaying the results


printf("Sum is %d\n", sum);
printf("Average is %.0f\n", average); // Printing average as a whole number
printf("Product is %d\n", product);
printf("Largest is %d\n", largest);
printf("Smallest is %d\n", smallest);

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;

// Asking user to input an integer


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

// Using the remainder operator to determine if the number is even or odd


if (number % 2 == 0)
printf("%d is even.\n", number);

if (number % 2 != 0)
printf("%d is odd.\n", number);

return 0;
}

You might also like