0% found this document useful (0 votes)
46 views32 pages

C Practicals

c programming praticals

Uploaded by

jsaurav2468
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)
46 views32 pages

C Practicals

c programming praticals

Uploaded by

jsaurav2468
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/ 32

Module 1:- Programming with C

1. Practical 1:-

a. To calculate simple interest taking principal, rate of interest and number of years as input

from user. Write algorithm & draw flowchart for the same.

Algorithm:

Name of Algorithm: To calculate the simple interest.

Step 1: Start

Step 2: Read principal amount as ‘p’, time as ‘t’ and rate of interest as ‘r’

Step 3: SI = p*t*r/100

Step 4: Display SI

Step 5: Stop

#include <stdio.h>

int main() {

float principal, rate, time, interest;

// Take user input for principal, rate, and time

printf("Enter principal amount: ");

scanf("%f", &principal);

printf("Enter interest rate: ");

scanf("%f", &rate);

printf("Enter time period (in years): ");

scanf("%f", &time);

// Calculate simple interest

interest = (principal * rate * time) / 100;

// Display the result

printf("Simple Interest is: %.2f\n", interest);

return 0;

}
Output:

Enter principal amount: 1000

Enter interest rate: 6

Enter time period (in years): 2

Simple Interest is: 120.00

b. Write a program to find greatest of three numbers using conditional operator. Write

algorithm & draw flowchart for the same.

Algorithm

Step1:

// using nested if-else

#include <stdio.h>

int main()

int c = 10, b = 22, a = 9;

// Finding largest by comparing using relational operators

if (a >= b) {

if (a >= c)

printf("%d is the largest number.", a);

else

printf("%d is the largest number.", c);

else {

if (b >= c)

printf("%d is the largest number.", b);

else

printf("%d is the largest number.", c);

return 0;
}

Output:

The numbers A, B and C are: 10, 22, 9

22 is the largest number.

c. Write a program to check if the year entered is leap year or not. Write algorithm & draw

flowchart for the same.

Algorithm

START

Step 1 → Take integer variable year

Step 2 → Enter the year

Step 3 → Check if year is divisible by 4 but not 100, DISPLAY "leap year"

Step 4 → Check if year is divisible by 400, DISPLAY "leap year"

Step 5 → Otherwise, DISPLAY "not leap year"

Flowchart

#include <stdio.h>

int main() {

int year;

printf("Enter a year: ");

scanf("%d", &year);

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))

printf("%d is a leap year.\n", year);

Else

printf("%d is not a leap year.\n", year);

return 0;
}

2. Practical 2:-

a. Write a program to calculate roots of a quadratic equation.

Algorithm

Start

Read a, b, c values

Compute d = b2 4ac

if d > 0 then

r1 = b+ sqrt (d)/(2*a)

r2 = b sqrt(d)/(2*a)

Otherwise if d = 0 then

compute r1 = -b/2a, r2=-b/2a

print r1,r2 values

Otherwise if d < 0 then print roots are imaginary

Stop

#include <stdio.h>

#include <math.h>

int main()

float a, b, c, r1, r2, d;

printf("Enter the values of a b c: ");

scanf(" %f %f %f", & a, & b, & c);

d = b * b - 4 * a * c;

if (d > 0) { r1 = -b + sqrt(d) / (2 * a);

r2 = -b - sqrt(d) / (2 * a);

printf("The real roots = %f %f", r1, r2);

else if (d == 0) { r1 = -b / (2 * a); r2 = -b / (2 * a);


printf("Roots are equal =%f %f", r1, r2);

Else

printf("Roots are imaginary");

return 0;

Output:

Case 1:

Enter the values of a b c: 1 4 3

The real roots = -3.000000 -5.000000

Case 2:

Enter the values of a b c: 1 2 1

Roots are equal =-1.000000 -1.000000

Case 3:

Enter the values of a b c: 1 1 4

Roots are imaginary

b. Write a menu driven program using switch case to perform add / subtract / multiply /

divide based on the users choice.

#include<stdio.h>

#include<conio.h>

int main()

int a,b;

int op;

printf(" 1.Addition\n 2.Subtraction\n 3.Multiplication\n 4.Division\n");

printf("Enter the values of a & b: ");

scanf("%d %d",&a,&b);

printf("Enter your Choice : ");


scanf("%d",&op);

switch(op)

case 1 :

printf("Sum of %d and %d is : %d",a,b,a+b);

break;

case 2 :

printf("Difference of %d and %d is : %d",a,b,a-b);

break;

case 3 :

printf("Multiplication of %d and %d is : %d",a,b,a*b);

break;

case 4 :

printf("Division of Two Numbers is %d : ",a/b);

break;

default :

printf(" Enter Your Correct Choice.");

break;

return 0;

Output:

1.Addition

2.Subtraction

3.Multiplication

4.Division

Enter the values of a & b: 20 15

Enter your Choice : 1


Sum of 20 and 15 is : 35

c. Write a program to print the pattern of asterisks

#include <stdio.h>

int main()

int rows = 5;

// This loop to print all rows

for (int i = 0; i < rows; i++) {

// Inner loop 1 to print

// white spaces for each row

for (int j = 0; j < 2 * (rows - i) - 1; j++) {

printf(" ");

// Inner loop 2 to print star

// (*) character for each row

for (int k = 0; k < 2 * i + 1; k++) {

printf("* ");

printf("\n");

return 0;

Output:

***

*****

*******

*********
3. Practical 3

a. Write a program using while loop to reverse the digits of a number

#include <stdio.h>

int main() {

int num, reversedNum = 0, remainder;

// Prompt the user to enter a number

printf("Enter a number: ");

scanf("%d", &num);

// Reverse the number using a while loop

while (num != 0) {

remainder = num % 10;

reversedNum = reversedNum * 10 + remainder;

num /= 10; }

// Print the reversed number

printf("Reversed number: %d\n", reversedNum);

return 0;

Output:

Enter a number: 2345

Reversed number = 5432

. b. Write a program to calculate the factorial of a given number.

#include <stdio.h>

int main() {

int n, i;

unsigned long long fact = 1;

printf("Enter an integer: ");

scanf("%d", &n);

// shows error if the user enters a negative integer


if (n < 0)

printf("Error! Factorial of a negative number doesn't exist.");

else {

for (i = 1; i <= n; ++i) {

fact *= i;

printf("Factorial of %d = %llu", n, fact);

return 0;

Output:

Enter an integer: 10

Factorial of 10 = 3628800

c. Write a program to print the Fibonacci series.

#include <stdio.h>

int main() {

int i, n;

// initialize first and second terms

int t1 = 0, t2 = 1;

// initialize the next term (3rd term)

int nextTerm = t1 + t2;

// get no. of terms from user

printf("Enter the number of terms: ");

scanf("%d", &n);

// print the first two terms t1 and t2

printf("Fibonacci Series: %d, %d, ", t1, t2);

// print 3rd to nth terms

for (i = 3; i <= n; ++i) {


printf("%d, ", nextTerm);

t1 = t2;

t2 = nextTerm;

nextTerm = t1 + t2;

return 0;

Output:

Enter the number of terms: 10

Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

4. Practical 4

a. Write a program to print area of square using function.

#include <stdio.h>

// Function to calculate the area of a square

// It takes one argument: 'side' (the length of a side of the square)

// It returns the calculated area as a float

float calculateAreaOfSquare(float side) {

float area = side * side;

return area;

int main()

float sideLength;

float areaResult;

printf("Enter the length of the side of the square: ");

scanf("%f", &sideLength);

areaResult = calculateAreaOfSquare(sideLength);

printf("The area of the square with side length %.2f is: %.2f\n", sideLength,
areaResult);

return 0;

Output:

b. Write a program using recursive function

#include <stdio.h>

// Recursive function to calculate factorial

int factorial(int n) {

// Base case: if n is 0 or 1, factorial is 1

if (n == 0 || n == 1) {

return 1;

// Recursive step: n * factorial of (n-1)

else {

return n * factorial(n - 1);

int main() {

int number = 5;

int result;

result = factorial(number); // Call the recursive function

printf("The factorial of %d is %d\n", number, result);

return 0;

Output:

c. Write a program to square root, abs() value using function.

#include <stdio.h> // Required for input/output operations like printf and scanf

#include <stdlib.h> // Required for the abs() function


#include <math.h> // Required for the sqrt() function

// Function to calculate the absolute value of an integer

int getAbsoluteValue(int num) {

return abs(num);

// Function to calculate the square root of a double

double getSquareRoot(double num) {

// It's good practice to handle negative numbers for square root

if (num < 0) {

printf("Error: Cannot calculate square root of a negative number.\n");

return -1.0; // Return a sentinel value to indicate an error

return sqrt(num);

int main() {

int int_value;

double double_value;

// Get input for absolute value

printf("Enter an integer to find its absolute value: ");

scanf("%d", &int_value);

// Calculate and print absolute value

int abs_result = getAbsoluteValue(int_value);

printf("Absolute value of %d is: %d\n", int_value, abs_result);

// Get input for square root

printf("Enter a non-negative number to find its square root: ");

scanf("%lf", &double_value);

// Calculate and print square root

double sqrt_result = getSquareRoot(double_value);


if (sqrt_result != -1.0) { // Check if no error occurred

printf("Square root of %.2lf is: %.2lf\n", double_value, sqrt_result);

return 0;

Output:

d. Write a program using goto statement .

#include <stdio.h>

int main ()

int i = 11;

if (i % 2 == 0)

EVEN:

printf("The number is even \n");

goto END;

Else

ODD:

printf("The number is odd \n");

END:

printf("End of program");

return 0;

Output:
The number is odd

End of program

5. Practical 5

a. Write a program to print rollno and names of 10 students using array.

#include <stdio.h>

struct student {

char firstName[50];

int roll;

} s[10];

int main() {

int i;

printf("Enter information of students:\n");

// storing information

for (i = 0; i < 10; ++i) {

s[i].roll = i + 1;

printf("\nFor roll number%d,\n", s[i].roll);

printf("Enter first name: ");

scanf("%s", s[i].firstName);

printf("Displaying Information:\n\n");

// displaying information

for (i = 0; i < 10; ++i) {

printf("\nRoll number: %d\n", i + 1);

printf("First name: ");

puts(s[i].firstName);
}

return 0;

Output:

Enter information of students:

For roll number1,

Enter first name:

For roll number2,

Enter first name:

For roll number3,

Enter first name:

b. Write a program to sort the elements of array in ascending or descending order

#include <stdio.h>

void main (){

int num[20];

int i, j, a, n;

printf("Enter number of elements in an array");

scanf("%d", &n);

printf("Enter the elements");

for (i = 0; i < n; ++i)

scanf("%d", &num[i]);

for (i = 0; i < n; ++i){

for (j = i + 1; j < n; ++j){

if (num[i] > num[j]){

a = num[i];
num[i] = num[j];

num[j] = a;

printf("The numbers in ascending order is:");

for (i = 0; i < n; ++i){

printf("%d", num[i]);

Enter number of elements in an array

Enter the elements

12

23

89

11

22

The numbers in ascending order is:

11

12

22

23

89

6. Practical 6

a. Write a program to extract the portion of a character string and print the extracted part.

/ C Program to demonstrate character

// extraction from a string

#include <stdlib.h>
#include <stdio.h>

int main()

char str[20];

printf("Enter the string: ");

gets(str);

// character extraction

printf("Printing the characters:: \n");

for (int i = 0; str[i] != '\0'; i++) {

if (str[i] != ' ') { // not a white space

printf("%c\n", str[i]); // printing each characters in a new line

return 0;

Output:

Enter the string: St Andrews

Printing the characters::

b. Write a program to find the given string is palindrome or not.


#include <stdio.h>

#include <string.h>

int main()

char string1[20];

int i, length;

int flag = 0;

// Prompt the user for input

printf("Enter a string: ");

scanf("%s", string1);

// Calculate the string length

length = strlen(string1);

// Compare characters from the start and end of the string

// and stop if a mismatch is found or the middle of the string is reached.

for (i = 0; i < length / 2; i++) {

if (string1[i] != string1[length - i - 1]) {

flag = 1;

break;

// Output the result

if (flag) {

printf("%s is not a palindrome\n", string1);

} else {

printf("%s is a palindrome\n", string1);

return 0;
}

c. Write a program to using strlen(), strcmp() function .

#include <stdio.h>

#include <string.h> // Required for strlen() and strcmp()

int main() {

char str1[] = "Hello";

char str2[] = "World";

char str3[] = "Hello";

// Using strlen()

printf("Length of str1: %zu\n", strlen(str1)); // Output: 5

printf("Length of str2: %zu\n", strlen(str2)); // Output: 5

// Using strcmp()

int comparison_result1 = strcmp(str1, str2);

if (comparison_result1 == 0) {

printf("str1 and str2 are equal.\n");

} else if (comparison_result1 < 0) {

printf("str1 is lexicographically smaller than str2.\n");

} else {

printf("str1 is lexicographically greater than str2.\n");

// Output for "Hello" vs "World": str1 is lexicographically smaller than str2.

int comparison_result2 = strcmp(str1, str3);

if (comparison_result2 == 0) {
printf("str1 and str3 are equal.\n"); // Output: str1 and str3 are equal.

} else if (comparison_result2 < 0) {

printf("str1 is lexicographically smaller than str3.\n");

} else {

printf("str1 is lexicographically greater than str3.\n");

return 0;

Output:

7. Practical 7

Write a program to swap two numbers using a function. Pass the values to be swapped to

this function using call-by-value method and call-byreference method.

#include <stdio.h>

swap (int *, int *);

int main()

int a, b;

printf("\nEnter value of a & b: ");

scanf("%d %d", &a, &b);

printf("\nBefore Swapping:\n");

printf("\na = %d\n\nb = %d\n", a, b);

swap(&a, &b);

printf("\nAfter Swapping:\n");

printf("\na = %d\n\nb = %d", a, b);

return 0;

}
swap (int *x, int *y)

int temp;

temp = *x;

*x = *y;

*y = temp;

Output:

8. Practical

8 a. Write a program to read a matrix of size m*n.

#include <stdio.h>

int main() {

int a[10][10], r, c;

printf("Enter rows and columns: ");

scanf("%d %d", &r, &c);

// asssigning elements to the matrix

printf("\nEnter matrix elements:\n");

for (int i = 0; i < r; ++i)

for (int j = 0; j < c; ++j) {

printf("Enter element a%d%d: ", i + 1, j + 1);

scanf("%d", &a[i][j]);

// printing the matrix a[][]

printf("\nEntered matrix: \n");

for (int i = 0; i < r; ++i)


for (int j = 0; j < c; ++j) {

printf("%d ", a[i][j]);

if (j == c - 1)

printf("\n");

return 0;

Output:

b. Write a program to multiply two matrices using a function.

void Matrix_mul (int mat1[][c1], int mat2[][c2])

int mul[r1][c2];

printf ("Multiplication of given two matrices is:\n");

for (int i = 0; i < r1; i++)

for (int j = 0; j < c2; j++)

mul[i][j] = 0;

for (int k = 0; k < r2; k++)

mul[i][j] += mat1[i][k] * mat2[k][j];

printf ("%d\t", mul[i][j]);

printf ("\n");

}
Output:

9. Practical

9 Write a program to print the structure using Title Author Subject Book ID Print

the details of two students.

#include <stdio.h>

#include <string.h>

// Define the structure for a Book

struct Student {

char title[100];

char author[50];

char subject[100];

int student_id;

};

int main() {

// Declare two structure variables, Book1 and Book2

Struct Student Student1;

struct Student Student2;

// --- Student 1 specifications ---

// Use strcpy to assign values to the string members

strcpy(Student1.title, "The C Programming Language");

strcpy(Student1.author, "Brian W. Kernighan");

strcpy(Student1.subject, "Programming");

Student1 student_id = 1001;


// --- Book 2 specifications ---

strcpy(Student2.title, "The Hitchhiker's Guide to the Galaxy");

strcpy(Student2.author, "Douglas Adams");

strcpy(Student2.subject, "Science Fiction");

Student2 student_id = 1002;

// --- Print Book 1 details ---

printf("Book 1 Details:\n");

printf("--------------------------\n");

printf("Title : %s\n", Student1.title);

printf("Author : %s\n", Student1.author);

printf("Subject : %s\n", Student1.subject);

printf("Book ID : %d\n\n", Student1.student_id);

// --- Print Book 2 details ---

printf("Book 2 Details:\n");

printf("--------------------------\n");

printf("Title : %s\n", Student2.title);

printf("Author : %s\n", Student2.author);

printf("Subject : %s\n", Student1.subject);

printf("Book ID : %d\n", Student2.student _id);

return 0;

Output:
10. Practical

10

Create a mini project on “Bank management system”. The program should be menu driven.

#include<stdio.h>

#include<conio.h>

#include<string.h>

void menu();

void new_acc();

void exit();

void de_money();

void withdraw();

void acc_det();

void main()

menu();

getch();

struct {

char acc_no[20];

int amt,exit0,bal;

char name[100],addr[20],acc_type;

}add,check,upd,de,with,det;

void menu()

int choice;

system("cls");

printf("\n%58s","WELCOME TO THE BANK MANAGEMENT SYSTEM");

printf("\n\n%50s","WELCOME TO MAIN MENU");


printf("\n\n\t\t1.Create New Account");

printf("\n\t\t2.Deposit Money");

printf("\n\t\t3.Withdraw Money");

printf("\n\t\t4.Check Existing Account Details");

printf("\n\t\t5.Exit");

schoice:

printf("\n\n\n\t\tEnter Your Choice : ");

scanf("%d",&choice);

system("cls");

switch(choice)

case 1:new_acc();

break;

case 2:de_money();

break;

choice;

account_no:

fptr=fopen("C:\\record.txt","a+");

printf("\n\n%50s","Open New Account");

printf("\n\n\tEnter Your Full Name : ");

gets(add.name);

printf("\n\tEnter yor address : ");

gets(add.addr);

printf("\n\tWhat type of account you want to open \n\tSaving(S)

or Current(C): ");

scanf(" %c",&add.acc_type);

printf("\n\tEnter account number (Use Your Mobile No.): ");

scanf("%s",&add.acc_no);
if(add.acc_no==check.acc_no)

printf("\n\tAccount no. is already in use!");

goto account_no;

else

printf("\n\tAccount Created Successfully.!!");

money:

printf("\n\tPlease deposit A money (Minimum 500 ): ");

scanf("%d",&add.amt);

if(add.amt>=500)

printf("\n\tMoney Deposit Successfully!!");

add.bal=add.amt;

printf("\n\tBalance : %d",add.bal);

else

printf("\tInvalid Amount!!");

goto money;

m("cls");

if(choice==1)

menu();

else if(choice==0)
{

exit();

else

printf("\n\tInvalid !");

goto invalid;

fclose(fptr);

void de_money()

int choice;

FILE *old;

system("cls");

old=fopen("C:\\record.txt","a+");

printf("\n\n%48s","Deposit Money");

transaction:

printf("\n\n\tEnter your account Number : ");

scanf("%d",&de.acc_no);

printf("\n\tAccount Holder Name : %s",add.name);

printf("\n\tYour Main Balance is : %d",add.bal);

printf("\n\tEnter Your amount you want to Deposit : ");

scanf("%d",&de.amt);

add.bal=de.amt+add.amt;

printf("\n\tMoney Deposit Successfully!");

printf("\n\tYour avialable balance is: %d",add.bal);

printf("\n\n\t\t\t\tThank you!!");
invalid:

printf("\n\n\tEnter 1 to go main menu and 0 to Exit : ");

scanf("%d",&choice);

system("cls");

if(choice==1)

menu();

else if(choice==0)

exit();

else

printf("\n\tInvalid !");

goto invalid;

fclose(old);

void withdraw()

int choice;

FILE *old,*newrec;

system("cls");

old=fopen("C:\\record.txt","a+");

printf("\n\n%48s","Withdraw Money");

transaction:

printf("\n\n\tEnter your account Number : ");


scanf("%s",&with.acc_no);

printf("\n\tAccount Holder Name : %s",add.name);

printf("\n\tYour Main Balance is : %d",add.bal);

printf("\n\tEnter Your amount you want to Withdraw : ");

scanf("%d",&with.amt);

add.bal=with.amt-add.bal;

printf("\n\tMoney Withdraw Successfully!");

printf("\n\tYour available balance is: %d",add.bal);

printf("\n\n\t\t\t\tThank you!!");

invalid:

printf("\n\n\tEnter 1 to go main menu and 0 to Exit : ");

scanf("%d",&choice);

system("cls");

if(choice==1)

menu();

else if(choice==0)

exit();

else

printf("\n\tInvalid !");

goto invalid;

fclose(old);

}
void acc_det()

FILE *fptr;

int choice;

system("cls");

fptr=fopen("C:\\record.txt","r");

printf("\n\n%48s","Account Details");

detail:

printf("\n\n\tEnter your account Number : ");

scanf("%s",&det.acc_no);

printf("\n\tAccount Number : %s",add.acc_no);

printf("\n\tAccount Holder Name : %s",add.name);

printf("\n\tAccount Type : %c",add.acc_type);

printf("\n\tYour Balence : %d",add.bal);

invalid:

printf("\n\n\n\tEnter 1 to go main menu and 0 to Exit : ");

scanf("%d",&choice);

system("cls");

if(choice==1)

menu();

else if(choice==0)

exit();

else

{
printf("\n\tInvalid !");

goto invalid;

fclose(fptr);

void exit()

system("cls");

printf("\n\n\n\t\t\t\t Thank You!");

printf("\n\n\t *** The Project Created By Bhavesh Shinde [FY-IT]

***");

OUTPUT:

You might also like