Practical Solution Sem 1 C
Practical Solution Sem 1 C
1.1) Write a program to read 5 integer numbers and Calculate and Print Average
of 5 given no’s.
#include <stdio.h>
#include <conio.h>
void main()
{
int a,b,c,d,e,avg;
clrscr();
avg = (a+b+c+d+e)/5;
1|Page
1.2) Write a program To find simple interest. Hint: SI = (P * R * N)/100
#include <stdio.h>
#include <conio.h>
void main()
{
float p, r, n, si;
clrscr();
si = (p * r * n) / 100;
getch();
2|Page
1.3) Write a program To find area of circle (a=∏r 2 where ∏=3.14)
#include <stdio.h>
#include <conio.h>
void main()
{
getch();
3|Page
1.4) Write a program to swap two numbers using third variable.
#include <stdio.h>
#include <conio.h>
void main()
{
int a, b, temp;
clrscr();
clrscr();
printf("Before swapping:\n");
printf("First number = %d\n", a);
printf("Second number = %d\n", b);
temp = a;
a = b;
b = temp;
printf("\nAfter swapping:\n");
printf("First number = %d\n", a);
printf("Second number = %d\n", b);
getch();
4|Page
1.5) To read customer number, customer name, past meter reading, present meter
reading, charge per unit. Calculate bill amount and print all the information
#include <stdio.h>
#include <conio.h>
void main() {
int cnum;
char cname[50];
float pastr, presentr, charge, bill;
clrscr();
getch();
}
5|Page
1.6) To display following output : *********************************************************
* Roll No: _____________ *
* Name: _____________ *
* Class: _____________ * *******************************************************
#include <stdio.h>
#include <conio.h>
void main()
{
char sname[50],clas[20];
int rno;
clrscr();
printf(“*************************************************\n”);
printf("\n*Roll number: %d*\n", rno);
printf("*Student Name: %s*\n", sname);
printf("*Class Name : %s*\n", clas);
printf(“*************************************************\n”);
getch();
}
6|Page
2. Program Based On If Statement..
#include <stdio.h>
#include <conio.h>
void main()
{
int num;
clrscr();
if (num % 2 == 0)
{
printf("%d is even.\n", num);
}
else
{
printf("%d is odd.\n", num);
}
getch();
}
7|Page
2.2) Write a program to check whether given number is positive, negative or zero.
#include <stdio.h>
#include <conio.h>
void main()
{
int num;
clrscr();
if (num > 0)
{
printf("%d is positive.\n", num);
}
else {
printf("You entered zero.\n");
}
getch();
}
8|Page
2.3) Write a program To find maximum number from given three numbers.
#include <stdio.h>
#include <conio.h>
void main()
{
int num1, num2, num3, max;
clrscr();
getch();
}
9|Page
2.4) Write C Program that read a number and checks weather the given no. is
divisible by X or not. (X is any no. entered from user)
#include <stdio.h>
#include <conio.h>
void main()
{
int num, x;
clrscr();
if (x != 0)
{
if (num % x == 0)
{
printf("%d is divisible by %d.\n", num, x);
}
else
{
printf("%d is not divisible by %d.\n", num, x);
}
}
else
{
printf("Divisor cannot be zero.\n");
}
getch();
}
10 | P a g e
2.5) Write a program to read two operands and one arithmetic operator and
perform the operation according to it using switch statement.
#include <stdio.h>
#include <conio.h>
void main()
{
char operator;
double num1, num2, result;
clrscr();
switch (operator) {
case '+':
result = num1 + num2;
printf("%.2lf + %.2lf = %.2lf\n", num1, num2, result);
break;
case '-':
result = num1 - num2;
printf("%.2lf - %.2lf = %.2lf\n", num1, num2, result);
break;
case '*':
result = num1 * num2;
printf("%.2lf * %.2lf = %.2lf\n", num1, num2, result);
break;
case '/':
if (num2 != 0)
{
result = num1 / num2;
printf("%.2lf / %.2lf = %.2lf\n", num1, num2, result);
}
else
{
11 | P a g e
printf("Error! Division by zero.\n");
}
break;
default:
printf("Error! Operator is not correct.\n");
break;
}
getch();
}
12 | P a g e
2.6) Input an integer number. Check & print the message Number is one digit, two
digit …..Five digit.
#include <stdio.h>
#include <conio.h>
void main()
{
int num;
clrscr();
else
{
printf("Enter Correct Number greater than zero");
}
getch(); }
2.7) Input month number and print corresponding month name.
13 | P a g e
#include <stdio.h>
#include <conio.h>
void main()
{
int month;
clrscr();
switch (month) {
case 1:
printf("January\n");
break;
case 2:
printf("February\n");
break;
case 3:
printf("March\n");
break;
case 4:
printf("April\n");
break;
case 5:
printf("May\n");
break;
case 6:
printf("June\n");
break;
case 7:
printf("July\n");
break;
case 8:
printf("August\n");
break;
case 9:
printf("September\n");
14 | P a g e
break;
case 10:
printf("October\n");
break;
case 11:
printf("November\n");
break;
case 12:
printf("December\n");
break;
default:
printf("Invalid month number! Please enter a number between 1 and 12.\n");
break;
}
getch()
15 | P a g e
2.8) An electric power distribution company charges its domestic consumers as
follows:
Write a program that read customer number & power consumed and print the
amount to be paid by the customer. Note that output should be well
formatted.
#include <stdio.h>
#include <conio.h>
void main()
{
int customerNumber, units;
float amount;
clrscr();
else {
amount = 575 + (units - 500) * 1.75;
16 | P a g e
}
getch();
}
17 | P a g e
2.9) Write a program to find net salary of employee. Criteria to calculate net salary
are as follows:
Employee DA MA PF IT
code
1 to 5 67% 12% 10% 15%
6 to 12 62% 10% 9% 10%
13 to 15 55% 8% 8% 8%
#include <stdio.h>
#include <conio.h>
void main()
{
int empCode;
float basicSalary, DA, MA, PF, IT, netSalary;
clrscr();
18 | P a g e
PF = basicSalary * 0.08;
IT = basicSalary * 0.08;
}
else {
printf("Invalid employee code!\n");
}
getch();
}
19 | P a g e
2.10) To read student roll number, name and marks of 3 subjects. Calculate total,
percentage class and result according to criteria. (Use flushall)
If student fails in one or two subjects then declare result as “ATKT” and class
as ****
If student fails in more than two subjects then declare result as “FAIL” and
class as ****
If student passes all subjects then declare result as “PASS” and find class
according to following criteria :-
#include <stdio.h>
#include <string.h>
#include <conio.h>
void main()
{
int rollNumber, marks1, marks2, marks3, total;
float percentage;
char name[50], result[10], clas[20];
clrscr();
20 | P a g e
if (marks3 < 40) failCount++;
if (failCount > 2) {
strcpy(result, "FAIL");
strcpy(clas, "****");
}
else {
strcpy(result, "ATKT");
strcpy(clas, "****");
}
}
else
{
strcpy(result, "PASS");
getch();
}
21 | P a g e
3. Programs based on Loops
3.1) Read 10 integer numbers and find Sum of first 10 integer nos .
#include <stdio.h>
#include <conio.h>
void main()
{
int numbers[10];
int sum = 0;
clrscr();
getch();
}
22 | P a g e
3.2) To find out N! (Factorial of N).
#include <stdio.h>
#include <conio.h>
void main()
{
int n;
unsigned long long factorial = 1;
clrscr();
if (n < 0)
{
printf("Factorial of a negative number doesn't exist.\n");
}
else
{
for (int i = 1; i <= n; i++)
{
factorial *= i;
}
printf("Factorial of %d = %d\n", n, factorial);
}
getch();
}
23 | P a g e
3.3) Read x and y and print value of XY. (Power)
#include <stdio.h>
#include <conio.h>
void main()
{
int x, y;
long long result = 1;
clrscr();
getch();
}
24 | P a g e
3.4) To print N terms of Fibonacci series.
Input: N = 9
Output: Fibonacci series: 1 1 2 3 5 8 13 21
#include <stdio.h>
#include <conio.h>
int fibonacci();
void main()
{
int n;
clrscr();
printf("\n");
return 0;
}
int fibonacci(int n)
{
if (n <= 1)
return n;
else
return fibonacci(n - 1) + fibonacci(n - 2);
}
25 | P a g e
3.5) Read an integer no and print Reverse of the given number
#include <stdio.h>
#include <conio.h>
void main() {
int n, reverse = 0, remainder;
clrscr();
while (n != 0)
{
remainder = n % 10;
reverse = reverse * 10 + remainder;
n /= 10;
}
getch();
}
26 | P a g e
3.6) To check whether inputted number is palindrome number or not.
#include <stdio.h>
#include <conio.h>
void main()
{
int num, reversedNum = 0, remainder, originalNum;
clrscr();
originalNum = num;
while (num != 0)
{
remainder = num % 10;
reversedNum = reversedNum * 10 + remainder;
num /= 10;
}
if (originalNum == reversedNum)
{
printf("%d is a palindrome.\n", originalNum);
}
else
{
printf("%d is not a palindrome.\n", originalNum);
}
getch();
}
27 | P a g e
3.7) To find sum of odd value and even value digits of a given number.
#include <stdio.h>
#include <conio.h>
void main()
{
int n, digit, sumOdd = 0, sumEven = 0;
clrscr();
while (n != 0)
{
digit = n % 10;
if (digit % 2 == 0)
{
sumEven += digit;
}
else
{
sumOdd += digit;
}
n /= 10;
}
getch();
}
28 | P a g e
3.8) Write a program for 1-2+3-4…….N terms
#include <stdio.h>
#include <conio.h>
void main()
{
int n, i, sum = 0;
clrscr();
else
{
sum += i;
}
}
getch();
}
29 | P a g e
3.9) Write a program for x + x2/2! + x3/3! + x4/4!……..xn/n
#include <stdio.h>
#include <conio.h>
int factorial();
void main()
{
float x, sum = 0.0;
int n;
clrscr();
getch();
}
30 | P a g e
3.10) To print the following pattern for n=4.
1
12
123
1234
#include <stdio.h>
#include <conio.h>
void main()
{
int n = 4;
clrscr();
getch();
}
31 | P a g e
3.11) To print the following pattern for n=4.
1
12
123
1234
#include <stdio.h>
#include <conio.h>
void main()
{
int n = 4;
clrscr();
getch();
}
32 | P a g e
3.12) To print the following pattern for n=4.(n in range 1 to 10)
A
A B A
A B C B A
A B C D C B A
#include <stdio.h>
#include <conio.h>
void main()
{
int n = 4;
clrscr();
getch();
}
33 | P a g e
4. Programs Based on Array
4.1) Input n elements in an array and find sum of all elements of array.(1D)
#include <stdio.h>
#include <conio.h>
void main()
{
int n, sum = 0;
clrscr();
int arr[n];
getch();
}
34 | P a g e
4.2) Find out the maximum and minimum element from one dimensional array
#include <stdio.h>
#include <conio.h>
void main()
{
int n;
clrscr();
int arr[n];
getch();
}
35 | P a g e
4.3) Read n elements into one dimensional array and Sort the elements in
ascending order
#include <stdio.h>
#include <conio.h>
void main()
{
int n;
clrscr();
int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++)
{
printf("Element %d: ", i + 1);
scanf("%d", &arr[i]);
}
getch();
}
36 | P a g e
4.4) Read n elements into one dimensional array and find Frequency of each
element.
#include <stdio.h>
#include <conio.h>
void main()
{
int arr[100], freq[100];
int size, i, j, count;
clrscr();
freq[j] = 0;
}
}
if(freq[i] != 0)
{
freq[i] = count;
}
}
37 | P a g e
{
printf("%d occurs %d times\n", arr[i], freq[i]);
}
}
getch();
}
38 | P a g e
4.5) Enter two matrix A and B. create a new matrix C which stores all elements of
matrix A first, then all elements of matrix B.
#include <stdio.h>
#include <conio.h>
void main()
{
int rowsA, colsA, rowsB, colsB;
int i, j;
clrscr();
int k = 0;
for(i = 0; i < rowsA; i++)
{
for(j = 0; j < colsA; j++)
{
C[k++] = A[i][j];
39 | P a g e
}
}
getch();
}
4.6) To read two matrix A and B and perform matrix addition. (A & B both mxn
matrix)
40 | P a g e
#include <stdio.h>
#include <conio.h>
void main()
{
int rows, cols;
int i, j;
clrscr();
41 | P a g e
}
printf("\n");
}
getch();
}
42 | P a g e
4.7) Read a matrix and display the Transpose Matrix
#include <stdio.h>
#include <conio.h>
void main()
{
int rows, cols;
int i, j;
clrscr();
getch();
}
4.8) Read a matrix and check whether it is Identity Matrix or not.
43 | P a g e
#include <stdio.h>
#include <conio.h>
void main()
{
int size, i, j, isIdentity = 1;
clrscr();
int matrix[size][size];
if(isIdentity) {
printf("The matrix is an Identity Matrix.\n");
} else {
printf("The matrix is not an Identity Matrix.\n");
44 | P a g e
}
getch();
}
45 | P a g e
5. Program based on string
5.1) Input string from user and find out Length of string without using strlen
Function
#include <stdio.h>
#include <string.h>
#include <conio.h>
void main()
{
char str[100];
int length = 0;
clrscr();
getch();
}
46 | P a g e
5.2) Input string from user and find out Length of string using strlen Function
#include <stdio.h>
#include <string.h>
#include <conio.h>
void main()
{
char str[100];
int length;
clrscr();
length = strlen(str);
getch();
}
47 | P a g e
5.3) Input two strings from user. Add second string at the end of first string without
using strcat function.
#include <stdio.h>
#include <string.h>
void main()
{
char str1[100], str2[100];
int i, j;
clrscr();
i = 0;
while (str1[i] != '\0')
{
i++;
}
j = 0;
while (str2[j] != '\0')
{
str1[i] = str2[j];
i++;
j++;
}
str1[i] = '\0';
getch();
}
48 | P a g e
5.4) Input two strings from user. Add second string at the end of first string using strcat
function
#include <stdio.h>
#include <string.h>
#include <conio.h>
void main()
{
char str1[100], str2[100];
clrscr();
strcat(str1, str2);
getch();
}
49 | P a g e
5.5) To count and display the total number of Uppercase, Lowercase, digit, blank space
and special character from a given string.
#include <stdio.h>
#include <string.h>
#include <conio.h>
void main()
{
char str[100];
int i;
int uppercase = 0, lowercase = 0, digits = 0, spaces = 0, special = 0;
clrscr();
getch();
}
50 | P a g e
5.6) Convert string into upper case and
#include <stdio.h>
#include <string.h>
#include <conio.h>
void main() {
char str[100];
int i; clrscr();
getch();
}
Lower Case
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main() {
char str[100];
int i; clrscr();
getch();
}
51 | P a g e
5.7) To Reverse a given string with without using strrev
#include <stdio.h>
#include <string.h>
#include <conio.h>
void main()
{
char str[100], temp;
int i, j;
clrscr();
getch();
}
52 | P a g e
5.8) To Reverse a given string with using strrev
#include <stdio.h>
#include <string.h>
#include <conio.h>
void main()
{
char str[100];
clrscr();
strrev(str);
getch();
}
53 | P a g e
5.9) Write a menu based program : Enter a string and perform following operations
1.Encrypt
2.decrypt
3.quit
#include <stdio.h>
#include <string.h>
#include <conio.h>
void main()
{
char str[100];
int choice;
clrscr();
while(1) {
printf("\nMenu:\n");
printf("1. Encrypt\n");
printf("2. Decrypt\n");
printf("3. Quit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
getchar();
switch(choice)
{
case 1:
printf("Enter a string to encrypt: ");
gets(str);
encrypt(str);
printf("Encrypted string: %s\n", str);
break;
54 | P a g e
case 2:
printf("Enter a string to decrypt: ");
gets(str);
decrypt(str);
printf("Decrypted string: %s\n", str);
break;
case 3:
printf("Quitting...\n");
return 0;
default:
printf("Invalid choice! Please try again.\n");
}
}
getch();
}
55 | P a g e