Exercise 2
Exercise 2
Exercise 2
5. Write a program to check whether an input integer is an odd or even number. Use
character O/E to indicate that number is odd/even.
Logic:
• scanf("%d", &a); reads the integer value from the user and stores it in the
variable a.
• The expression a % 2 == 0 checks if a is divisible by 2. The modulus operator %
returns the remainder when a is divided by 2.
If the remainder is 0 (a % 2 == 0), the number is even.
If the remainder is not 0, the number is odd
• If the condition a % 2 == 0 is true, the code executes printf("%d is even\n",a);,
printing that the number is even.
• If the condition is false, the code executes printf("%d is odd\n",a);, printing that
the number is odd.
Code:
Output:
DY24SBBU0BID001 5
Programming in C Date of performance: 05/09/2024
Exercise 2
6. Write a program to swap two numbers from the user, using a temporary variable.
Logic:
• a and b are the two numbers that the user will input.
• temp is a temporary variable used to store the value of one number during the
swap.
• scanf("%d", &a); reads the first number and stores it in a.
• scanf("%d", &b); reads the second number and stores it in b.
• First, the value of a is stored in the temp variable. This is done because we
are about to overwrite a with the value of b, and we don't want to lose the
original value of a.
• Next, a is assigned the value of b. This completes the first part of the swap.
• Finally, b is assigned the value stored in temp (which is the original value
of a), completing the swap.
• The program prints the numbers after swapping, showing the updated values
of a and b.
Code:
Fig 6.1: ”Swap two numbers using a temporary variable” Code.
Output:
Fig 6.2: “Swap two numbers using a temporary variable” Output.
DY24SBBU0BID001 6
Programming in C Date of performance: 05/09/2024
Exercise 2
7. Write a program to swap two numbers from the user without using a temporary
variable.
Logic:
• The program first takes two numbers (a and b) as input from the user using
scanf().
• a = a + b;
This adds the two numbers and stores the result in a. Now, a holds the sum
of the two numbers.
• b = a - b;
Since a now contains the sum of both numbers, subtracting b from it will
give the original value of a. Now, b holds the original value of a.
• a = a - b;
In this step, subtracting the new value of b (which is the original value of a)
from the sum stored in a will give the original value of b. Now, a holdsthe
original value of b.
Output:
Fig 7.2: “Swap two numbers without using a temporary variable” Code.
DY24SBBU0BID001 7
Programming in C Date of performance: 05/09/2024
Exercise 2
8. Write a program to calculate sum of digits of any 5-digit number from the user.
Logic:
• The program first prompts the user to enter a 5-digit number using
scanf(),which is stored in the variable num.
• Extract the first digit (digit1): digit1 = num / 10000;
This divides the number by 10000, giving the first
digit
• Extract the second digit (digit2): digit2 = (num / 1000) % 10;
This divides the number by 1000 (removing the last 3 digits), and the modulus
operation (% 10) extracts the second digit.
• It gets repeated for digit3, digit4 and digit5 with 100, 10 and 1 respectively
• Summing the Digits: After extracting all five digits, the program sums
them:sum = digit1 + digit2 + digit3 + digit4 + digit5;
For num = 57321, the sum will be: sum = 5 + 7 + 3 + 2 + 1 = 18;
• Finally, the program prints the sum of the digits using printf().
Code:
Fig 8.1: “Sum of any 5-digits number” Code.
Output:
Fig 8.2: “Sum of any 5-digits number” Code.
DY24SBBU0BID001 8
Programming in C Date of performance: 05/09/2024
Exercise 2
Logic:
• Input a 5-Digit Number:
The program prompts the user to enter a 5-digit number using scanf(),
which isstored in the variable num.
• Extracting the Digits: The digits of the number are extracted one by one
usingthe modulus (%) and division (/) operators.
• Reversing the Number: After extracting all the digits, the program
reconstructsthe reversed number by rearranging the digits in reverse order:
reversed_num = digit1 * 10000 + digit2 * 1000 + digit3 * 100 + digit4 *
10 +digit5;
• Finally, the program prints the reversed number using printf().
Code:
Fig 9.1: “Reverse any 5-digit number” Code.
Output:
Fig 9.2: “Reverse any 5-digit number” Output.
DY24SBBU0BID001 9
Programming in C Date of performance: 05/09/2024
DY24SBBU0BID001 10