Assignment 00
Assignment 00
Q.1 Hands-on the Linux Commands (cd, cmp, cat, Is, man, mkdir, rmdir, mv,
passwd, pwd, rm).
Ans.
Commands Use
cd Change Directory
cmp File Comparision
cat File data elaboration
ls Listing
man Manual (discription of any folder)
mkdir Make directory
mv Move file
passwd Password change
pwd Present working directory
rm Remove file
rmdir Remove directory
cp Copy file
Taha Zulfiquar
Roll no.- 23F029
Enrollment No.- 0801EE231103
You can use it to write simple notes and documents, or you can enable more
advanced features that are useful for software development.
Code:
#include <stdio.h>
int main()
{
printf("Hello World\n");
return 0;
}
Output:
Taha Zulfiquar
Roll no.- 23F029
Enrollment No.- 0801EE231103
Code:
#include <stdio.h>
int main()
{
float a, b, c;
printf("Enter value of a:\n");
scanf("%f", &a);
printf("Enter value of b:\n");
scanf("%f", &b);
Output:
Q.5 Declare three variables, "num1," "num2," and "num3." Calculate the
average of these three numbers and display the result.
Code:
#include <stdio.h>
int main()
{
float num1, num2, num3, avg;
printf("Enter 3 numbers: ");
scanf("%f %f %f", &num1, &num2, &num3);
avg = (num1 + num2 + num3) / 3;
printf("The average of the given numbers is %0.01f.\n", avg);
return 0;
}
Output:
Taha Zulfiquar
Roll no.- 23F029
Enrollment No.- 0801EE231103
Q.6 Define a constant named "PI" with a value of 3.14 and calculate the area
of a circle using a radius stored in a variable named "radius."
Code:
#include <stdio.h>
float PI = 3.14;
int main()
{
float radius, area;
printf("Enter the radius: ");
scanf("%f", &radius);
area = PI * radius * radius;
printf("The are of the circle with radius %0.1f is %0.1f.",
radius, area);
}
Output:
Taha Zulfiquar
Roll no.- 23F029
Enrollment No.- 0801EE231103
Q.7 Write a C program to swap the values of two variables with and without
using a dummy variable.
Code:
#include <stdio.h>
void withdummyvariable(int a, int b, int swap)
{
printf("Before swapping: a = %d, b = %d\n", a, b);
swap = a;
a = b;
b = swap;
printf("After swapping: a = %d, b = %d\n", a, b);
}
void withoutdummyvariable(int a, int b)
{
printf("Before swapping: a = %d, b = %d\n", a, b);
a = a + b;
b = a - b;
a = a - b;
printf("After swapping: a = %d, b = %d\n", a, b);
}
int main()
{
int a, b, swap;
printf("Enter the value of a: ");
scanf("%d", &a);
printf("Enter the value of b: ");
scanf("%d", &b);
printf("\n");
Output:
Q.8 Declare a global variable "globalVar" with an initial value. Then, declare
a local variable with the same name within a function and print both values to
observe the scope.
Code:
#include <stdio.h>
int globalVar = 29;
void func(int globalVar)
{
/* this stores a temporary value for the variable named globalVar
which can only be accessed when this function is called */
globalVar = 47;
printf("This is a local variable: %d\n", globalVar);
}
int main()
{
printf("This is a global variable: %d\n", globalVar);
int c;
func(c);
return 0;
}
Taha Zulfiquar
Roll no.- 23F029
Enrollment No.- 0801EE231103
Output:
#include <stdio.h>
int main()
{
float a, b, c;
int num;
printf("Enter 1 to convert Farenheit to Celsius and 2 to convert
Celsius to Fahrenheit: \n");
scanf("%d", &num);
switch (num)
{
case 1:
printf("Enter temperature in fahrenheit: \n");
scanf("%f", &a);
c = (a - 32) * 5 / 9;
printf("The temperature in celsius= %0.01f°\n", c);
break;
case 2:
printf("Enter temperature in celsius: \n");
scanf("%f", &a);
c = (a * 9 / 5) + 32;
printf("The temperature in fahrenheit= %0.01f°\n", c);
break;
default:
break;
}
return 0;
}
Taha Zulfiquar
Roll no.- 23F029
Enrollment No.- 0801EE231103
Output: