C Lab Part - A Programs
C Lab Part - A Programs
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
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");
}
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
MS. Ankitha
Asst Prof, Dept of MCA
GMIT 9
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
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", °);
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);
}
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
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
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
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