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

Lab 3 OS

The document discusses how to write and execute C programs in Linux using the gcc compiler. It provides instructions on installing gcc, writing C code in an editor, compiling with gcc, and running the output file. It then gives examples of 5 C programs: 1) swapping two numbers with and without a function, 2) converting Fahrenheit to Celsius, 3) finding the sizes of data types, 4) adding two complex numbers, and 5) printing prime numbers from 1 to N. The document concludes that the learner has gained the ability to setup gcc and run C programs in Linux through the Ubuntu terminal.

Uploaded by

Prateek Jaiswal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Lab 3 OS

The document discusses how to write and execute C programs in Linux using the gcc compiler. It provides instructions on installing gcc, writing C code in an editor, compiling with gcc, and running the output file. It then gives examples of 5 C programs: 1) swapping two numbers with and without a function, 2) converting Fahrenheit to Celsius, 3) finding the sizes of data types, 4) adding two complex numbers, and 5) printing prime numbers from 1 to N. The document concludes that the learner has gained the ability to setup gcc and run C programs in Linux through the Ubuntu terminal.

Uploaded by

Prateek Jaiswal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

LAB-3

Task:Write and execute given C program in linux environment

Installation of gcc compiler:


First you have to check that gcc compiler is installed or not. To check ,just run “gcc –version”
command in terminal.

If it is not pre installed then run “sudo apt install gcc” command in terminal to install it.
After completing above command,again run “gcc –version” to check gcc compiler is installed or
not.

Now you are ready to write a c program in ubuntu terminal.

How to write and run C program?


● We write c program in ubuntu editor window.To open it run “gedit <filename>”
command. Write your c program in editor window then save it.
● After that you have to compile your program. To do that you have to run “gcc
<filename>” command.
● To execute your program run “./a.out” command in ubuntu terminal.

Q-1) C program to swap two numbers with function and without function,without using
third variable.

Algorithm:
1.start
2.Take input a,b
3.a=a+b
4.b=a-b
5.a=a-b
6.print a,b

Code:
a)swap using function:
The swap function swaps the numbers using addition and subtraction.We need to pass the
address of variables to the function using pointer ie.call by reference.Pointer is used to allow the
function to modify the original variables directly.
Output:

b)without using function:


Here we directly swap the numbers using assignment operator

Output:

Q-2)write C program to calculate fahrenheit to celsius.

Algorithm: 1.start
2.take input temperature in fahrenheit(F)
3.Use conversion formula: Celcius(C)= (F-32)*5/9
4.Print temperature in celsius
Code:
Output:

Q-3)Write C program to find the size of int,float,double, and char.


Algorithm:
1.start
2.use command sizeof(‘data type’) to display the size of int , float,char,double.
3.end
Code:

Output:

Q-4)write C program to add two complex numbers.

Algorithm: 1.start
2.Declare a1,a2,b1,b2, addR,addI
3. Enter the real and imaginary part of first complex number
4.Enter the real and imaginary part of second complex number
5. Add the real part of both complex number and store in addR.
6. Add the imaginary part of both complex number and store in addI.
7. Display addition of two complex number i.e addR+j addI
8.end
Code:

Output:

Q-5)write C program to print prime numbers from 1 to N.

Algorithm: 1.start
2.take num as input (up to which u have to print prime number)
3.now take every number ‘i’ start from i=2 to i=num
4.set a variable count to 1
5. Now check for ‘i’ is prime number or not ,if not make count=0 and break
6. If count is still 1, print i
7.end
Code:

Output:

Conclusion:

We learn how to setup gcc compiler in Linux operating system , also how to run and execute C
program files using linux as operating system through ubuntu interface/terminal.

You might also like