0% found this document useful (0 votes)
25 views

Ui 22 Cs 57

This document contains algorithms for 5 C programs: 1) Swapping two numbers with and without a function. It uses simple arithmetic operations without a third variable. 2) Converting Fahrenheit to Celsius using the formula Celsius = (Fahrenheit - 32) * 5/9. 3) Finding the size of data types (int, float, double, char) using the sizeof operator. 4) Adding two complex numbers by separately adding the real and imaginary parts. 5) Printing prime numbers from 1 to n by checking if a number is divisible by any number from 2 to num/2.

Uploaded by

ui22cs57
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)
25 views

Ui 22 Cs 57

This document contains algorithms for 5 C programs: 1) Swapping two numbers with and without a function. It uses simple arithmetic operations without a third variable. 2) Converting Fahrenheit to Celsius using the formula Celsius = (Fahrenheit - 32) * 5/9. 3) Finding the size of data types (int, float, double, char) using the sizeof operator. 4) Adding two complex numbers by separately adding the real and imaginary parts. 5) Printing prime numbers from 1 to n by checking if a number is divisible by any number from 2 to num/2.

Uploaded by

ui22cs57
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/ 9

LAB-3

1.) C Program to Swap Two Numbers with function and without function,
without using third variable.

ALGORITHM (WITHOUT FUNCTION)


Read two integers a and b from the user.
Display the values of a and b before swapping.
Swap the values of a and b using the following steps:
a. Add a and b and store the result in a. (a = a + b)
b. Subtract the original value of b from the new value of num1 and store
the result in b. (b = a - b)
c. Subtract the original value of b from the new value of num1 and store
the result in a. (a = a - b)
Display the values of a and b after swapping.
This approach uses simple arithmetic operations to achieve the swapping
without using a third variable.
OUTPUT
ALGORITHM (WITH FUNCTION)
Read two integers a and b from the user.
Display the values of a and b before swapping.
Swap the values of a and b using the following steps:
Call the swap function in main function

a. Add a and b and store the result in a. (a = a + b)


b. Subtract the original value of b from the new value of num1 and store
the result in b. (b = a - b)
c. Subtract the original value of b from the new value of num1 and store
the result in a. (a = a - b)
Display the values of a and b after swapping.
This approach uses simple arithmetic operations to achieve the swapping
without using a third variable.
CODE
OUTPUT

2.) C Program to Calculate Fahrenheit to Celsius

ALGORITHM

Start the program.


Declare variables fahrenheit and celsius to store the temperature values in
Fahrenheit and Celsius respectively.
Read the value of fahrenheit from the user.
Calculate the value of celsius using the formula: celsius = (fahrenheit - 32) *
5 / 9.
Display the converted temperature in Celsius.
End the program.
CODE
OUTPUT

3.) C Program to Find the Size of int, float, double, and char.

ALGORITHM
Start the program.
Use the sizeof operator to determine the size of each data type:
int, float, double, and char.
Display the size of each data type in bytes.
End the program.
4.) C Program to Add Two Complex Numbers.
ALGORITHM
Start the program.
Declare variables to store the real and imaginary parts of both
complex numbers, as well as variables to store the real and imaginary
parts of the result.
Read the real and imaginary parts of the first complex number from
the user.
Read the real and imaginary parts of the second complex number
from the user.
Add the real parts of both complex numbers and store the result in
the variable for the real part of the result.
Add the imaginary parts of both complex numbers and store the
result in the variable for the imaginary part of the result.
Display the sum of the complex numbers in the format: real_part +
imaginary_part i..
OUTPUT
OUTPUT

5.) Write a c program to print prime numbers from 1 to n.


ALGORITHM
Start the program.
Declare variables num and i for looping.
Read the value of n from the user to specify the range up to
which prime numbers need to be printed.
Loop from num = 2 to n (inclusive):
a. Inside the loop, initialize a variable isPrime as 1 to assume the
current number is prime.
b. Loop from i = 2 to num/2 (inclusive):
Check if num is divisible by i.
If it is divisible, set isPrime to 0 and break out of the loop.
c. If isPrime is still 1, then print num as it is a prime number.
End the program.

CODE
OUTPUT

You might also like