Practical
Practical
Exercise 1
1. Draw a flowchart and write an algorithm to find out greatest of two integers
<DY24SBBU0BFT027> 1
Programming in C Date of performance: 30/08/2024
2. Draw a flowchart and write an algorithm to find out greatest of three integers
<DY24SBBU0BFT027> 2
Programming in C Date of performance: 30/08/2024
Logic:
#include <stdio.h> – This line includes the standard input-output library in the program.
int main() – The main function where the execution of the program begins.
printf(“Hello, World!\n”); – This function call prints “Hello, World!” followed by a new
line.
return 0; -This statement indicates that the program ended successfully.
Code:
Output:
<DY24SBBU0BFT027> 3
Programming in C Date of performance: 30/08/2024
Logic:
#include <stdio.h> This library is required for using printf() and scanf().
%d is the format specifier for integers. We pass the address of integer value using & so
that scanf() can modify the variable directly.
Code:
Output:
<DY24SBBU0BFT027> 4
Programming in C Date of performance: 30/08/2024
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:
Declare an integer variable
Accept an integer within the variable
Take modulus of the variable value with 2
If remainder is zero, the given number is even or it is odd
Check the value of remainder with conditional operator & display the
output accordingly.
Code:
Input:-
Output:
<DY24SBBU0BFT027> 5
Programming in C Date of performance: 30/08/2024
6. Write a program to swap two numbers from the user, using a temporary
variable
Logic:
Declare two integers
Accept integers within two variables
Declare a temporary variable
The swap values of both variables are stored in the temporary variable
The temporary variable value is displayed using printf in code
Code:
Output:
<DY24SBBU0BFT027> 6
Programming in C Date of performance: 30/08/2024
7. Write a program to swap two numbers from the user without using a
temporary variable.
Logic:
printf("Input value for x & y: \n"); prompts the user to input values for x and
y. scanf("%d%d", &x, &y); reads the input values from the user and stores
them in x and y.
y=xy; subtracts the new value of y (which is the original x + y) by y to get the
original value of x and stores it in y. XX y; subtracts the new value of y (original
x) from x to get the original value of y and stores it in x.
Code:
<DY24SBBU0BFT027> 7
Programming in C Date of performance: 30/08/2024
Output:
<DY24SBBU0BFT027> 8
Programming in C Date of performance: 30/08/2024
8. Write a program to calculate sum of digits of any 5-digit number from the user.
Logic:
Initialize sum = 0
Extract the last digit of the number and add it to the sum (sum)
Code:
Output:
<DY24SBBU0BFT027> 9
Programming in C Date of performance: 30/08/2024
Logic:-
The program starts by taking a 5-digit number as input from the user.
• The extracted digits are then combined in reverse order by multiplying each
digit by its respective place value (units, tens, hundreds, etc.).
Code:-
Input:-
Output:-
<DY24SBBU0BFT027> 10
Programming in C Date of performance: 30/08/2024
<DY24SBBU0BFT027> 11