Practicals Part-2 2023 C Language Updated
Practicals Part-2 2023 C Language Updated
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;
sarea = 4 * pi * r* r;
volume = 4.0 / 3.0 * pi * r*r*r;
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];
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;
// 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;
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();
}
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();
}
#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();
}
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);
}
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;
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();
}
*
**
***
****
*****
#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();
}
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>
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);
getch();
}