Program 1: Calculate Straight-Line Distance Between Two Points
A robot needs to find how far it must travel between two points on a 2D plane. The straight-line distance (Euclidean distance) can be calculated using the formula:
distance = √((x₂ - x₁)² + (y₂ - y₁)²)
C Program
#include <stdio.h>
#include <math.h>
int main() {
double x1, y1, x2, y2, distance;
// Input coordinates of point 1
printf("Enter coordinates of point 1 (x1 y1): ");
scanf("%lf %lf", &x1, &y1);
// Input coordinates of point 2
printf("Enter coordinates of point 2 (x2 y2): ");
scanf("%lf %lf", &x2, &y2);
// Calculate distance
distance = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2
- y1));
// Display result
printf("The straight-line distance between the points
is: %.4lf\\n", distance);
return 0;
}
there were some issues with sqrt, use the below to compile
cc filename.c -o filename -lm./filename
Enter coordinates of point 1 (x1 y1): 2 3
Enter coordinates of point 2 (x2 y2): 7 8
The straight-line distance between the points is: 7.0711
Explanation
- scanf("%lf %lf", &x1, &y1) - reads the coordinates of the first point.
- scanf("%lf %lf", &x2, &y2) - reads the coordinates of the second point.
- sqrt((x2 - x1)² + (y2 - y1)²) - applies the Euclidean distance formula.
-
The result is printed with 4 decimal places using
%.4lf
.
Program 2: Calculate Grade Based on Marks
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
A suitable if-else ladder is used to implement this grading logic efficiently.
C Program
#include <stdio.h>
int main() {
int marks;
char grade;
printf("Enter the student's marks (0-100): ");
scanf("%d", &marks);
if (marks < 0 || marks > 100) {
printf("Invalid marks entered.\\n");
return 1; // Exit for invalid input
}
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';
}
printf("Grade: %c\\n", grade);
return 0;
}
Enter the student's marks (0-100): 82
Grade: B
Explanation
- scanf("%d", &marks) - reads the student's marks.
- A validation check ensures marks are within the range 0-100.
- An if-else ladder assigns the grade based on the given ranges.
- The grade is displayed using
printf()
.
Program 3: KYC Verification System
Develop a C program that takes a unique identification input like PAN Number, AADHAR Number, APAAR ID, Driving License, or Passport and checks it against a set of stored KYC records. Based on the input, the program displays whether the individual is verified or not.
Program Requirements
- Stores a few KYC records with different ID types (all integers).
- User selects the ID type, then enters the ID number.
- The program searches the corresponding stored records.
- A switch statement handles multiple ID types.
- Displays whether the individual is verified or not.
C Program
#include <stdio.h>
#define MAX_RECORDS 5
// Function to check if an ID exists in the given array
int isVerified(int idList[], int size, int id) {
int int i;
for (i = 0; i < size; i++) {
if (idList[i] == id)
return 1; // Found
}
return 0; // Not found
}
int main() {
// Stored KYC IDs for each type (sample data)
int panNumbers[MAX_RECORDS] = {123456789, 987654321,
112233445, 556677889, 998877665};
int aadharNumbers[MAX_RECORDS] = {111122223333,
222233334444, 333344445555, 444455556666, 555566667777};
int apaarIds[MAX_RECORDS] = {10101, 20202, 30303,
40404, 50505};
int drivingLicenses[MAX_RECORDS] = {789456123,
456123789, 123789456, 987123654, 654987321};
int passports[MAX_RECORDS] = {555555, 666666, 777777,
888888, 999999};
int choice;
long long inputId; // To handle large IDs like
Aadhar
printf("Select ID type to verify:\\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");
printf("Enter your choice (1-5): ");
scanf("%d", &choice);
printf("Enter the ID number: ");
scanf("%lld", &inputId);
int verified = 0;
switch (choice) {
case 1:
verified = isVerified(panNumbers,
MAX_RECORDS, (int)inputId);
break;
case 2: { // Aadhar uses long long
long long aadharNumbersLL[MAX_RECORDS] =
{111122223333, 222233334444, 333344445555, 444455556666,
555566667777};
verified = 0;
int i;
for (i = 0; i < MAX_RECORDS; i++) {
if (aadharNumbersLL[i] ==
inputId) {
verified = 1;
break;
}
}
}
break;
case 3:
verified = isVerified(apaarIds,
MAX_RECORDS, (int)inputId);
break;
case 4:
verified = isVerified(drivingLicenses,
MAX_RECORDS, (int)inputId);
break;
case 5:
verified = isVerified(passports,
MAX_RECORDS, (int)inputId);
break;
default:
printf("Invalid choice.\\n");
return 1;
}
if (verified)
printf("Individual Verified.\\n");
else
printf("Individual NOT Verified.\\n");
return 0;
}
Select ID type to verify:
1. PAN Number
2. AADHAR Number
3. APAAR ID
4. Driving License
5. Passport
Enter your choice (1-5): 2
Enter the ID number: 333344445555
Individual Verified.
Explanation
- User chooses the ID type (1-5).
- The corresponding stored records array is searched for a match.
- switch statement routes the logic to the correct ID type.
-
isVerified()
function checks arrays for a match, returning 1 (found) or 0 (not found). - Program displays whether the individual is Verified or NOT Verified.