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

Operating System Concepts Lab: Saleem Mustafa

The document provides examples of C programs that perform operations like printing text, taking user input, calculating factorials and sums, determining if a number is prime, and using recursion. It also includes instructions for compiling and executing each program in the terminal on Ubuntu. The examples are intended as practice questions to help learn C language programming and running programs from the command line.

Uploaded by

Arbaz Khan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views

Operating System Concepts Lab: Saleem Mustafa

The document provides examples of C programs that perform operations like printing text, taking user input, calculating factorials and sums, determining if a number is prime, and using recursion. It also includes instructions for compiling and executing each program in the terminal on Ubuntu. The examples are intended as practice questions to help learn C language programming and running programs from the command line.

Uploaded by

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

Operating System Concepts

Lab

Prepared by
Saleem Mustafa
[email protected]
1. Print “Hello World” in C language and compile on terminal
#include <stdio.h>

int main()
{
printf( "Hello World" ); //Printing output on Screen

return 0;
}
On Terminal type for Output:
gcc filename.c –o
./a.out

2. Input and Output


#include <stdio.h>

int main()
{
printf( "My name is Rayaan\n"); //Printing output on Screen
printf( "Superior University Lahore");
return 0;
}
On Terminal type for Output:
gcc filename.c –o output
./output

3. Input and Output


#include <stdio.h>

int main()
{
int num;
printf( "Please enter a number: " ); //Printing output on
Screen
scanf( "%d", &num ); //taking input from
user
printf( "You entered %d", num );

return 0;
}
On Terminal type for Compilation and Execution:
gcc filename.c –o output
./output

Practices Questions in C Language and execute on terminal:

4. Write a program in C language which find the Factorial of positive number.


#include <stdio.h>
 
int main()
{
int c, n, fact = 1;

printf("Enter a number to calculate it's factorial\n");


scanf("%d", &n);
 
for (c = 1; c <= n; c++)
fact = fact * c;
 
printf("Factorial of %d = %d\n", n, fact);
 
return 0;
}

On Terminal type for Output:


gcc filename.c –o result
./result

5 Write a program in C language which perform Addition of two numbers.also


compile and execute on terminal.
#include <stdio.h>
int main()
{ int num1, num2, sum;
printf( "Please enter number1: " );
scanf( "%d", &num1 );
printf( "Please enter number2: " );
scanf( "%d", &num2 );
sum=num1+num2;
printf( "You entered %d", sum );
return 0;}
//On Terminal type for Output:
gcc filename.c –o results
./result

6 Write a program in C language which find the Prime


Number by taking positive number from user.

#include <stdio.h>
int main()
{
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d",&n);

for(i=2; i<=n/2; ++i)


{
// condition for nonprime number
if(n%i==0)
{
flag=1;
break;
}
}
if (flag==0)
printf("%d is a prime number.",n);
else
printf("%d is not a prime number.",n);
return 0; }

-----------------------------------------------------

5. Write a program in C language which perform Recursion of numbers.


#include <stdio.h>
int sum(int);
int main()
{
int number, result;

printf("Enter a positive integer: ");


scanf("%d", &number);

result = sum(number);

printf("sum=%d", result);
}

int sum(int num)


{
if (num!=0)
return num + sum(num-1); // sum() function calls itself
else
return num;
}

-------------------------------------------

Task No. 1:

Write a program in C language which calculate the factorial of positive number. And write
program to find prime number by taking input as numbers.

Task No. 2:

Write a program in C language which find prime number from the numbers taken from user and
compile and execute on terminal in Ubuntu.

Task No. 3:
Write a program in C language which perform the recursion operation on numbers taken from
user also compile and execute on terminal in ubuntu.

You might also like