0% found this document useful (0 votes)
17 views10 pages

Practicals Part-2 2023 C Language Updated

The document contains a series of C programming exercises designed for a Computer Science practical class. Each exercise includes a specific task, such as exchanging values, calculating the volume of a sphere, and performing arithmetic operations, along with the corresponding C code implementation. The exercises cover basic programming concepts, including input/output, loops, functions, and conditional statements.

Uploaded by

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

Practicals Part-2 2023 C Language Updated

The document contains a series of C programming exercises designed for a Computer Science practical class. Each exercise includes a specific task, such as exchanging values, calculating the volume of a sphere, and performing arithmetic operations, along with the corresponding C code implementation. The exercises cover basic programming concepts, including input/output, loops, functions, and conditional statements.

Uploaded by

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

PUNJAB GROUP OF COLLEGES

Subject: Computer Science Practicals: C language


Class: ICS-II Teacher: Prof. Adnan Faisal Khan

1. Write a program to input two numbers and exchange their values. The program should display the
values before and after exchanging values.
#include <stdio.h>
#include<conio.h>
void main()
{
int num1, num2, temp;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
printf("Values before exchanging:\n");
printf("Number 1: %d\n", num1);
printf("Number 2: %d\n", num2);
temp = num1;
num1 = num2;
num2 = temp;
printf("\nValues after exchanging:\n");
printf("Number 1: %d\n", num1);
printf("Number 2: %d\n", num2);
getch();
}

2. Write a C program to input the radius of a sphere. Computer its volume and surface
area ( Formula for surface area = 4 π R2and volume = 4/3 π R3).
#include <stdio.h>
#include<conio.h>
void main()
{
float r,sarea,volume;
const float pi = 3.14;

printf("Enter the radius of the sphere: ");


scanf(“%f", &r);

sarea = 4 * pi * r* r;
volume = 4.0 / 3.0 * pi * r*r*r;

printf("Surface Area of the sphere: %.2f\n", sarea);


printf("Volume of the sphere: %.2f\n", volume);

getch();
}
3. Write a program that input name, age and address from the user and displays it on the screen
#include <stdio.h>
#include<conio.h>
void main()
{
char name[50];
int age;
char address[100];

// Input user's information


printf("Enter your name, age and Address: ");
scanf("%s%d%s", name,&age,address);

// Display user's information


printf("\nName: %s\n", name);
printf("Age: %d\n", age);
printf("Address: %s\n", address);

getch();
}

4. Write a program that input four numbers and calculate the sum average and product of all the
numbers.
#include <stdio.h>
#include<conio.h>
void main()
{
float num1, num2, num3, num4;
float sum, average, product;

// Input four numbers


printf("Enter four numbers:");
scanf("%f %f %f %f", &num1, &num2, &num3, &num4);

// Calculate sum, average, and product


sum = num1 + num2 + num3 + num4;
average = sum / 4;
product = num1 * num2 * num3 * num4;

// Display results
printf("Sum: %.2f\n", sum);
printf("Average: %.2f\n", average);
printf("Product: %.2f\n", product);
getch();
}

5. Write a program that takes three numbers as an input, display the smallest number from three
numbers on the computer screen.
#include <stdio.h>
#include<conio.h>
void main()
{
float num1, num2, num3,smallest;
// Input three numbers
printf("Enter three numbers:\n");
scanf("%f %f %f", &num1, &num2, &num3);
if (num1 <num2 && num1<num3 )
smallest = num1;
else if (num2 < num3)
smallest = num2;
else
smallest = num3;

// Display the smallest number


printf("The smallest number is %f\n", smallest);
getch();
}

6. Write a program , on purchase certain item, a discount (dis) of 10% is offered of the quantity (qty)
purchased is more than 1000. Enter quantity (qty) and rate per item through the keyboard to
calculate the total (tot) expenses. (Hints: tot = (qty * rate) – (qty * rate * dis/100) )
Solution:

#include<stdio.h>
#include<conio.h>
void main()
{
int qty,dis=0;
float total,rate ;
clrscr();
printf("Enter Quantity of item : ");
scanf("%d",&qty);
printf("Enter Rate of item : ”);
scanf("%f",&rate);
if(qty>1000)
dis=10;
total=(qty*rate) -(qty*rate*dis/100);
printf("Total expenses=Rs. %f",total);
getch();
}

7. Write a C-program which perform simple arithmetic operation ( +, -, *, /, %) by using switch


statement.
#include <stdio.h>
#include<conio.h>
void main() {
char operator;
float num1, num2, result;

printf("Enter an operator (+, -, *, /, %%): ");


scanf("%c", &operator);

printf("Enter two numbers: ");


scanf("%f %f", &num1, &num2);

switch (operator) {
case '+':
result = num1 + num2;
printf("Result: %.2f\n", result);
break;
case '-':
result = num1 - num2;
printf("Result: %.2f\n", result);
break;
case '*':
result = num1 * num2;
printf("Result: %.2f\n", result);
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
printf("Result: %.2f\n", result);
} else {
printf("Error: Division by zero!\n");
}
break;
case '%':
if (num2 != 0) {
result = (int)num1 % (int)num2;
printf("Result: %.2f\n", result);
} else {
printf("Error: Modulus by zero!\n");
}
break;
default:
printf("Invalid operator!\n");
}

getch();
}
8. Write a program that input a number from the user then find that number is positive, negative and
zero.

#include <stdio.h>
#include<conio.h>
void main()
{
int num=0;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0)
printf("The number %d is positive.\n", num);
else if (num< 0)
printf("The number %d is negative.\n", num);
else
printf("The number is zero.\n");

getch();
}

9. Write a program to input a number then find number is Even or Odd.


#include <stdio.h>
#include<conio.h>
void main()
{
int n;
printf("Enter a number: ");
scanf("%d", &n);
if (n % 2 == 0)
printf("%d is an even number.\n", n);
else
printf("%d is an odd number.\n", n);
getch();
}
10. Write a program to display the following series using for loop:
1 3 5 7 9 11 13 15 17 19 21 23

#include <stdio.h>
#include<conio.h>
void main()
{
int i;
printf("Series:\n ");
// Using a for loop to print the series
for ( i = 1; i <= 23; i += 2)
printf("%d\t", i);
getch();
}

11. Write a program to calculate and display sum of given series:


x + x2 + x3 +…….+ xn
#include <stdio.h>
#include<conio.h>
#include <math.h>

void main() {
int x, n;
double sum = 0.0;
printf("Enter the value of x: ");
scanf("%d", &x);
printf("Enter the value of n: ");
scanf("%d", &n);
// Calculating the sum of the series
for (int i = 1; i <= n; i++) {
sum += pow(x, i);
}

printf("Sum of the series: %.2lf\n", sum);

getch();
}

12. Write a program to display the following series using any single loop:
10 1
9 2
8 4
7 8
6 16
5 32
#include <stdio.h>
void main()
{
int col2 = 1;

for (int col1 = 10; col1 >= 5; col1--)


{
printf("%4d%8d\n", col1, col2);
col1--;
col2 *= 2;
}

getch();
}

13. Write a C program using while loop to print the output as shown below:
* 14.* 15.
* 16.
* 17.
* *18.
* 19.* 20.
* *
21. * *23.
22.
* * * * * *
* 24.* 25.
* 26.
* 27.
* *28.

#include <stdio.h>
#include<conio.h>
void main()
{
int rows = 4;
int cols = 6;
int i = 0;
while (i < rows)
{
int j = 0;
while (j < cols)
{
printf("*\t");
j++;
}
printf("\n");
i++;
}
getch();
}

14. Write a program to display following output on screen:

*
**
***
****
*****
#include <stdio.h>
#include<conio.h>
int main() {
int i,j,k;
for (i = 1; i <= 5; i++) {
// Print spaces
for ( j = 1; j <=5- i; j++)
printf(" ");

// Print asterisks
for ( k = 1; k <= i; k++) {
printf("* ");
}
printf("\n");
}
getch();
}

15. Write a program to show iteration number of inner and outer loop using nested loop.
#include <stdio.h>
#include<conio.h>
void main()
{
int i;
int j;
for (i=1:i<=5;i++)
for (j=1;j<=5;j++)
printf("Outer loop: %d, Inner loop: %d\n", i,j);

getch();
}

16. Write a program to display following output on screen:


*****
****
***
**
*
#include <stdio.h>
#include<conio.h>
void main()
{
int i,j;
for ( i = 5; i >= 1; i--)
{
for ( j = 1; j <= i; j++)
printf("*");
printf("\n");
}
getch();
}

17. Write a program to input a number from the user in main function pass number to a function and
display the product of that number in function.

#include <stdio.h>
#include<conio.h>

void Product(int num) {


int product = num * 10;
printf("Product of %d and 10 is: %d\n", num,
product);
}
return
void main()
{
int n;
printf("Enter a number: ");
scanf("%d", &n);

// Call the function with the entered number


Product(n);

getch();
}

18. Write a function that receives a number and return factorial of that number to the calling function.
#include <stdio.h>
#include<conio.h>
// Function to calculate factorial
int factorial(int num)
{
int result = 1,i;
for (i = 1; i <= num; i++)
result *= i;
return result;
}

void main()
{
int number, result;
printf("Enter a number: ");
scanf("%d", &number);

if (number < 0)
printf("Factorial is not defined for
negative numbers.\n");
else
result = factorial(number);
printf("Factorial of %d is %d\n",
number, result);
}
getch();
}

19. Write a program to input a number in main function. Passes the number to a function. The
function display the table of received number on the screen.

#include <stdio.h>
#include<conio.h>
void displayTable(int num) {
printf("Multiplication Table of %d:\n", num);

for (int i = 1; i <= 10; i++) {


printf("%d x %d = %d\n", num, i, num * i);
}
}
void main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
displayTable(number);

getch();
}

You might also like