C Program - Basic

These are practice programs

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

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:

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

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

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