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

C Lab Part - A Programs

The document outlines several C programming tasks, including calculating distances between points, grading students based on marks, verifying unique identification numbers, determining roots of quadratic equations, approximating sine values, searching keywords in strings, and checking student pass status based on marks. Each task includes a problem description, algorithm, flowchart, and program code. The document is intended for educational purposes in a computer science context.

Uploaded by

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

C Lab Part - A Programs

The document outlines several C programming tasks, including calculating distances between points, grading students based on marks, verifying unique identification numbers, determining roots of quadratic equations, approximating sine values, searching keywords in strings, and checking student pass status based on marks. Each task includes a problem description, algorithm, flowchart, and program code. The document is intended for educational purposes in a computer science context.

Uploaded by

Ankitha Ankitha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

GMIT 1

C LAB PROGRAMS
1. A robot needs to find how far it must travel between two points on a 2D
plane. Develop a C program to calculate the straight-line distance
between the given coordinates.

Problem Description
You have two points on a 2D plane, each defined by coordinates (x1, y1) and (x2,
y2) The task is to calculate the straight-line (Euclidean) distance between these two
points

Algorithm
Step 1: Start
Step 2: Input the coordinates of the first point (x1, y1)
Step 3: Input the coordinates of the second point (x2, y2)
Step 4: Compute the distance
distance = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))
Step 8: Display the distance
Step 9: Stop

MS. Ankitha
Asst Prof, Dept of MCA
GMIT 2

Flowchart:

Program Code
#include <stdio.h>
#include <math.h>
void main()
{
float x1, y1, x2, y2, distance;
printf("Enter x1 and y1 (coordinates of the first point): ");
scanf("%f %f", &x1, &y1);
printf("Enter x2 and y2 (coordinates of the second point): ");
scanf("%f %f", &x2, &y2);
distance = sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1));
printf("The straight-line distance between the two points is: %.2f\n", distance);
}

MS. Ankitha
Asst Prof, Dept of MCA
GMIT 3

2. Develop a C program that takes a student's marks as input and displays


their grade based on the following criteria: 90 and above: Grade A 75 to
89: Grade B 60 to 74: Grade C 50 to 59: Grade D Below 50: Grade F
Choose a suitable control structure to implement this logic efficiently.
Problem Description
Write a C program that takes a student's marks as input and displays the
corresponding grade
based on the following criteria:
• 90 and above: Grade A
• 75 to 89: Grade B
• 60 to 74: Grade C
• 50 to 59: Grade D
• Below 50: Grade F
Flowchart:

Algorithm
MS. Ankitha
Asst Prof, Dept of MCA
GMIT 4

Step 1: Start
Step 2: Input the student's marks
Step 3: Use if-else if statements to determine the grade:
• If marks ≥ 90 → Grade A
• Else if marks ≥ 75 → Grade B
• Else if marks ≥ 60 → Grade C
• Else if marks ≥ 50 → Grade D
• Else → Grade F
Step 4: Display the grade
Step 5: Stop
Program Code
#include <stdio.h>
void main()
{
int marks;
printf("Enter the student's marks (0 to 100): ");
scanf("%d", &marks);
if (marks < 0 || marks > 100)
printf("Invalid marks! Please enter a value between 0 and 100.\n");
else if (marks >= 90)
printf("Grade: A\n");
else if (marks >= 75)
printf("Grade: B\n");
else if (marks >= 60)

MS. Ankitha
Asst Prof, Dept of MCA
GMIT 5

printf("Grade: C\n");
else if (marks >= 50)
printf("Grade: D\n");
else
printf("Grade: F\n");
}

3. Develop a C program that takes a unique identification input like PAN


Number, AADHAR_Number, APAAR_Id, Driving License, Passport
and checks it against a set of stored KYC records. Based on the input,
display whether the individual is verified or not. Use an appropriate
control structure to handle multiple possible ID matches. Assume all
Unique identification are of integer type.
Problem Description
You have a set of stored KYC records identified by different unique IDs such as
PAN Number, AADHAR Number, APAAR ID, Driving License, Passport. The
program should take an integer input representing one of these IDs and check if it
matches any stored record. If yes, print "Verified", else "Not Verified".
Flowchart:

MS. Ankitha
Asst Prof, Dept of MCA
GMIT 6

Algorithm
Step 1: Start
Step 2: Display the menu:
1→ PAN Number
2 → AADHAR Number
3 → APAAR Id
4 → Driving License
5 → Passport
Step 3: Read the user’s choice.
Step 4: Use a switch(choice) to process the selected ID type:

MS. Ankitha
Asst Prof, Dept of MCA
GMIT 7

• Case 1 (PAN):
Print "PAN Verified".
• Case 2 (AADHAR):
Print "AADHAR Verified".
• Case 3 (APAAR):
Print "APAAR Verified".
• Case 4 (Driving License):
Print "Driving License Verified".
• Case 5 (Passport):
Print "Passport Verified".
• Default: Print "Not verified".
Step 5: Stop

Program Code
#include <stdio.h>
int main()
{
int choice, id;
printf("------ KYC Verification System ------\n");
printf("1. PAN Number\n");
printf("2. AADHAR Number\n");
printf("3. APAAR Id\n");
printf("4. Driving License\n");
printf("5. Passport\n");

MS. Ankitha
Asst Prof, Dept of MCA
GMIT 8

printf("Enter your choice (1-5): ");


scanf("%d", &choice);
switch(choice)
{
case 1: // PAN
printf("PAN Verified!\n");
break;
case 2: // AADHAAR
printf("AADHAR Verified!\n");
break;
case 3: // APAAR
printf("APAAR Verified!\n");
break;
case 4: // Driving License
printf("Driving License Verified!\n");
break;
case 5: // Passport
printf("Passport Verified!\n");
break;
default:
printf("Not Verified!\n");
}
return 0;
}

MS. Ankitha
Asst Prof, Dept of MCA
GMIT 9

4. A math app needs to determine the type of roots for a quadratic


equation based on user input. Develop a C program to calculate and
display the roots based on the given coefficients.
Problem Description
A math learning app needs to determine the nature of the roots of a quadratic
equation of the
form:
The program takes coefficients a, b, and c of a quadratic equation:
ax2+bx+c=0
It calculates the roots and determines the type of roots based on the discriminant:
• If a=0 and b=0: Invalid co-efficients
• If a=0(a alone zero): Equation is linear, roots are equal
Δ=b2−4ac
• If Δ>0: Two distinct real roots.
• If Δ=0: One real root (repeated).
• If Δ<0: Two complex roots

Flowchart:

MS. Ankitha
Asst Prof, Dept of MCA
GMIT 10

MS. Ankitha
Asst Prof, Dept of MCA
GMIT 11

Algorithm:
Step1: Start
Step2: Input the coefficients a, b, and c of the quadratic equation.
Step3: Check if a == 0 and b == 0:
If true, print "Invalid Coefficients" and terminate the program.
Step4: Else if a == 0:
o The equation is linear (bx + c = 0).
o Calculate root = -c / b.
o Print the linear root.
Step5: Else (a ≠ 0):
o The equation is quadratic.
o Calculate the discriminant: disc = b² - 4ac.
o Print the discriminant
o If disc > 0:
▪ Calculate two real and distinct roots:
▪ root1 = (-b + sqrt(disc)) / (2a)
▪ root2 = (-b - sqrt(disc)) / (2a)
▪ Print both roots.
o Else if disc == 0:
▪ Calculate one real and repeated root:
▪ root = -b / (2a)
▪ Print the root.
o Else (disc < 0):
▪ Calculate complex roots:

MS. Ankitha
Asst Prof, Dept of MCA
GMIT 12

▪ real = -b / (2a)
▪ imag = sqrt(-disc) / (2a)
▪ Print the complex roots in the form real ± imag i.
Step6: Stop
Program Code
#include <stdio.h>
#include <math.h>
int main()
{
float a, b, c, root1, root2, disc, real, imag;
printf("Enter coefficients of quadratic equation (a, b, c): ");
scanf("%f %f %f", &a, &b, &c);
if (a == 0 && b == 0)
{
printf("Invalid Coefficients!\n");
}
else if (a == 0)
{
// Linear equation bx + c = 0
root1 = -c / b;
printf("Linear Equation with root: %.2f\n", root1);
}
Else
{

MS. Ankitha
Asst Prof, Dept of MCA
GMIT 13

// Quadratic equation
disc = b * b - 4 * a * c;
printf("Discriminant = %.2f\n", disc);
if (disc > 0)
{
root1 = (-b + sqrt(disc)) / (2 * a);
root2 = (-b - sqrt(disc)) / (2 * a);
printf("Two distinct real roots: %.2f and %.2f\n", root1, root2);
}
else if (disc == 0)
{
root1 = -b / (2 * a);
printf("One real root: %.2f\n", root1);
}
else
{
real = -b / (2 * a);
imag = sqrt(-disc) / (2 * a);
printf("Complex roots: %.2f + %.2fi and %.2f - %.2fi\n", real, imag, real,
imag);
}
}
return 0;
}

MS. Ankitha
Asst Prof, Dept of MCA
GMIT 14

5. A sensor in a robotic arm needs to calculate the angle of rotation in real-


time, but the hardware doesn't support built-in trigonometric functions.
Develop a C program to approximate the value of sin(x) using a series
expansion method for improved performance.
Flowchart:

Algorithm
Step 1: Start
Step 2: Input angle x in degrees
Step 3: Convert degrees to radians:
x_rad = x * π / 180
Step 4: Initialize variables:

MS. Ankitha
Asst Prof, Dept of MCA
GMIT 15

- term = x_rad
- sum = 0
- sign = 1
Step 5: Loop through n terms for the desired accuracy
- Add current term * sign to sum
- Compute next term using formula:
term = term * x_rad * x_rad / ((2*i) * (2*i+1))
- Flip sign for next term (sign = -sign)
Step 6: Print final sum as approximate sin(x)
Step 7: Stop

Program Code
#include <stdio.h>
#include<math.h>
#define PI 3.14
void main()
{
float sum,term,x,nume;
int deg;
i=2,fact=1;
printf("Enter angle in degrees: ");
scanf("%d", &deg);
x=(deg*PI)/180;
sum=x;

MS. Ankitha
Asst Prof, Dept of MCA
GMIT 16

nume=x;
do
{
fact=fact*i*(i+1);
nume = -nume * x * x ;
term = nume/fact;
sum += term;
i+=2;
} while (fabs(term)>=0.0001);
printf("Approximated sin(%d) = %.2f\n", deg, sum);
}

6. Develop a C program that accepts a course description string and a


keyword from the user. Search whether the keyword exists within the
course description using appropriate string functions. If found, display:
"Keyword '' found in the course description." Otherwise,
display:"Keyword '' not found in the course description.

Problem Description
Develop a C program that accepts a course description (string) and a keyword
(string) from the user. The program should search the keyword inside the course
description. If the keyword exists, print a confirmation message; otherwise, print
that it is not found.

MS. Ankitha
Asst Prof, Dept of MCA
GMIT 17

Flowchart

Algorithm
Step 1: Start
Step 2: Input the course description (string)
Step 3: Input the keyword to search
Step 4: Use the strstr() function to check if the keyword exists in the course
description
- If strstr() returns a non-NULL pointer → Keyword found

MS. Ankitha
Asst Prof, Dept of MCA
GMIT 18

- Else → Keyword not found


Step 5: Display appropriate message
Step 6: Stop
Program Code
#include <stdio.h>
#include <string.h>
#define MAX 100
void main()
{
char description[MAX];
char keyword[100];
int len;
printf("Enter the course description:\n");
scanf("%s",description);
len = strlen(description);
printf("Enter the keyword to search: ");
scanf("%s", keyword);
if (strstr(description, keyword) )
printf("Keyword '%s' found in the %s course description.\n",
keyword,description);
else
printf("Keyword '%s' not found in the %s course description.\n",
keyword,description);
}

MS. Ankitha
Asst Prof, Dept of MCA
GMIT 19

7. Develop a C program that takes marks for three subjects as input. Use a
function to checkif the student has passed (minimum 40 marks in each
subject). Display the average andwhether the student passed or failed.
Problem Description
Develop a C program that takes marks for three subjects as input, checks if the
student has passed (minimum 40 marks in each subject), and then displays the
average marks and the pass/fail status
Flowchart

Algorithm
Step 1: Start
Step 2: Input marks for three subjects: mark1, mark2, mark3
MS. Ankitha
Asst Prof, Dept of MCA
GMIT 20

Step 3: Calculate the average:


average = (mark1 + mark2 + mark3) / 3.0
Step 4: Call a function isPassed(mark1, mark2, mark3) that returns:
• 1 if all marks ≥ 40
• 0 otherwise
Step 5: Display the average
Step 6: If return value is 1 → Print “Passed”
Else → Print “Failed”
Step 7: Stop

Program Code
#include <stdio.h>
int isPassed(int m1, int m2, int m3)
{
if (m1 >= 40 && m2 >= 40 && m3 >= 40)
return 1;
else return 0;
}
void main()
{
int subject1, subject2, subject3;
float average;
printf("Enter marks for Subject 1: ");
scanf("%d", &subject1);

MS. Ankitha
Asst Prof, Dept of MCA
GMIT 21

printf("Enter marks for Subject 2: ");


scanf("%d", &subject2);
printf("Enter marks for Subject 3: ");
scanf("%d", &subject3);
average = (subject1 + subject2 + subject3) / 3.0;
printf("Average Marks: %.2f\n", average);
if (isPassed(subject1, subject2, subject3))
printf("Result: PASS\n");
else
printf("Result: FAIL\n");
}
7. In an ATM system, two account balances need to be swapped
temporarily for validation. Develop a C program that accepts two
balances and uses a function with pointers to swap them. Display the
balances before and after swapping.

Problem Description
In an ATM system, you need to temporarily swap two account balances for
validation. The program will accept two balances, swap them using a function
with pointers, and display the balances before and after swapping.
Flowchart

MS. Ankitha
Asst Prof, Dept of MCA
GMIT 22

MS. Ankitha
Asst Prof, Dept of MCA
GMIT 23

Algorithm
Step 1: Start
Step 2: Input two account balances: balance1 and balance2
Step 3: Display balances before swapping
Step 4: Call a function swap(&balance1, &balance2) that:
• Uses a temporary variable and pointer dereferencing to swap the values
Step 5: Display balances after swapping
Step 6: Stop

Program Code
#include <stdio.h>
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void main()
{
int balance1, balance2;
printf("Enter balance for Account 1: ");
scanf("%d", &balance1);
printf("Enter balance for Account 2: ");
scanf("%d", &balance2);

MS. Ankitha
Asst Prof, Dept of MCA
GMIT 24

printf("\nBefore Swapping:\n");
printf("Account 1 Balance: %d\n", balance1);
printf("Account 2 Balance: %d\n", balance2);
swap(&balance1, &balance2);
printf("\nAfter Swapping:\n");
printf("Account 1 Balance: %d\n", balance1);
printf("Account 2 Balance: %d\n", balance2);
}

MS. Ankitha
Asst Prof, Dept of MCA

You might also like