0% found this document useful (0 votes)
9 views11 pages

Practical

The document outlines a series of programming exercises in C, including algorithms and flowcharts for finding the greatest of two or three integers, writing a 'Hello World' program, and taking user input. It also includes tasks for checking odd/even numbers, swapping numbers with and without a temporary variable, calculating the sum of digits of a 5-digit number, and reversing a 5-digit number. Each exercise provides logic explanations and code examples for implementation.

Uploaded by

vaibhavsarvaiya5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views11 pages

Practical

The document outlines a series of programming exercises in C, including algorithms and flowcharts for finding the greatest of two or three integers, writing a 'Hello World' program, and taking user input. It also includes tasks for checking odd/even numbers, swapping numbers with and without a temporary variable, calculating the sum of digits of a 5-digit number, and reversing a 5-digit number. Each exercise provides logic explanations and code examples for implementation.

Uploaded by

vaibhavsarvaiya5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Programming in C Date of performance: 30/08/2024

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

3. Write a “Hello World” program in C.

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

4. Write a program to take input from user using scanf () function.

Logic:

scanf() to read different types of inputs from the user.

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

We use printf() to display the values entered by the user.

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:

Include the Standard Input-Output Library: #include<stdio.h> ensures that the


functions for input and output operations are available. Main Function
Definition: int main() begins the execution of the program.

Variable Declaration: int x, y; declares two integer variables x and y.

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.

Display Initial Values: printf("Before swapping the value of x & y: %d %d", x,


y); prints the values of x and y before swapping. Swap Values Using Arithmetic
Operations:

x=x+y; adds x and y and stores the result in x.

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.

Display Swapped Values: printf("\nAfter swapping the value of x & y: %d


%d", x, y); prints the values of x and y after swapping. Return Statement:
return 0; indicates that the program has executed successfully.

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:

For user input num

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

9. Write a program to reverse any 5-digit number from the user.

Logic:-

The program starts by taking a 5-digit number as input from the user.

• The last digit is extracted using the modulus operator (%10).

• The number is then divided by 10 to remove the last digit.

• This process is repeated for each digit.

• 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

You might also like