0% found this document useful (0 votes)
3 views

Cp Programming

Uploaded by

billanandha77
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Cp Programming

Uploaded by

billanandha77
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Meenakshi Sundararajan Engineering College

(An Autonomous Institution, Affiliated to Anna University, Chennai)


U24CS101 - Programming in C - Laboratory Component

REGISTER NUMBER:243115104062

PROGRAM:
#include <stdio.h>

int main() {

int r, b, h, l; // Variables for radius, base, height, and length

float a1, a2, a3; // Variables for areas

printf("Enter the values of r, b, h, l: ");

scanf("%d %d %d %d", &r, &b, &h, &l);

// Calculating areas

a1 = l * b; // Area of rectangle

a2 = 3.14 * r * r; // Area of circle

a3 = 0.5 * b * h; // Area of triangle

// Display the results

printf("\nArea of rectangle: %.2f", a1);

printf("\nArea of circle: %.2f", a2);

printf("\nArea of triangle: %.2f\n", a3);

return 0;

OUTPUT:

Enter the values of r, b, h, l: 5 4 3 6

Area of rectangle: 24.00

Area of circle: 78.50

Area of triangle: 6.00

Department: CSE-B MSEC R2024


PAGE NO

Meenakshi Sundararajan Engineering College

(An Autonomous Institution, Affiliated to Anna University, Chennai) U24CS101 -


Programming in C - Laboratory Component

REGISTER NUMBER:243115104062

PROGRAM:
#include <stdio.h>
int main() {
int num1, num2, num3;
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);
if (num1 >= num2 && num1 >= num3) {
printf("The greatest number is: %d\n", num1);
} else if (num2 >= num1 && num2 >= num3) {
printf("The greatest number is: %d\n", num2);
} else {
printf("The greatest number is: %d\n", num3);
}

return 0;
}

OUTPUT:
Enter three numbers: 12 45 27
The greatest number is:45
Department: CSE-B MSEC R2024
PAGE NO
Meenakshi Sundararajan Engineering College
(An Autonomous Institution, Affiliated to Anna University, Chennai)
U24CS101 - Programming in C - Laboratory Component

REGISTERNUMBER: 243115104062

PROGRAM:
#include <stdio.h>
int main() {
int num, i;
printf("\nEnter the number: ");
scanf("%d", &num);
printf("\nPrint first %d natural numbers:\n", num);
for (i = 1; i <= num; i++) {
printf("%d\n", i);
}
return 0;
}

OUTPUT:
Print first 10 natural numbers
1
2
3
4
5
6
7
8
9
10
Department: CSE-B MSEC R2024
PAGE NO
Meenakshi Sundararajan Engineering College
(An Autonomous Institution, Affiliated to Anna University, Chennai)
U24CS101 - Programming in C - Laboratory Component

REGISTERNUMBER:

{
int pin = 1234; // Predefined PIN
int enteredPin, option;
double balance = 1000.00; // Initial balance
double amount;

// Prompt for PIN


printf("Enter your PIN: ");
scanf("%d", &enteredPin);

if (enteredPin != pin) {
printf("Incorrect PIN. Access denied.\n");
return 1; // Exit the program
}

do {

printf("\nATM Menu:\n");
printf("1. Check Balance\n");
printf("2. Deposit Money\n");
printf("3. Withdraw Money\n");

Department: CSE-B MSEC R2024


PAGE NO
Meenakshi Sundararajan Engineering College
(An Autonomous Institution, Affiliated to Anna University, Chennai)
U24CS101 - Programming in C - Laboratory Component

REGISTERNUMBER: 234115104062
printf("4. Exit\n");
printf("Select an option (1-4): ");
scanf("%d", &option);

switch (option) {
case 1: // Check Balance
printf("Your current balance is: $%.2f\n", balance);
break;

case 2: // Deposit Money


printf("Enter amount to deposit: $");
scanf("%lf", &amount);
if (amount > 0) {
balance += amount;
printf("You have successfully deposited: $%.2f\n", amount);
} else {
printf("Invalid deposit amount.\n");
}
break;

case 3: // Withdraw Money


printf("Enter amount to withdraw: $");
scanf("%lf", &amount);
if (amount > 0 && amount <= balance) {

Department: CSE-B MSEC R2024


PAGE NO
Meenakshi Sundararajan Engineering College
(An Autonomous Institution, Affiliated to Anna University, Chennai)
U24CS101 - Programming in C - Laboratory Component

REGISTERNUMBER: 234115104062
balance -= amount;
printf("You have successfully withdrawn: $%.2f\n", amount);
}
else if (amount > balance) {
printf("Insufficient balance.\n");
}
else {
printf("Invalid withdrawal amount.\n");
}
break;

case 4: // Exit
printf("Thank you for using the ATM. Goodbye!\n");
break;

default:
printf("Invalid option. Please select a valid option (1-4).\n");
}
} while (option != 4); // Continue until the user chooses to exit

return 0;
}

OUTPUT:

Enter your PIN: 1234


ATM Menu:
1. Check Balance
2. Deposit Money
3. Withdraw Money
4. Exit
Select an option (1-4): 1
Your current balance is: $1000.00

Department: CSE-B MSEC R2024


PAGE NO
Meenakshi Sundararajan Engineering College
(An Autonomous Institution, Affiliated to Anna University, Chennai)
U24CS101 - Programming in C - Laboratory Component

REGISTERNUMBER: 234115104062

ATM Menu:
1. Check Balance
2. Deposit Money
3. Withdraw Money
4. Exit
Select an option (1-4): 2
Enter amount to deposit: $500
You have successfully deposited: $500.00

ATM Menu:
1. Check Balance
2. Deposit Money
3. Withdraw Money
4. Exit
Select an option (1-4): 4
Thank you for using the ATM. Goodbye!

RESULT:
The program for ATM stimulation was successfully executed and the output was
verified.

Department: CSE-B MSEC R2024


PAGE NO
Meenakshi Sundararajan Engineering College
(An Autonomous Institution, Affiliated to Anna University, Chennai) U24CS101 -
Programming in C - Laboratory Component

REGISTER NUMBER:243115104062

PROGRAM:
#include <stdio.h>

int main() {

int n, target, found = 0;

printf("Enter the number of elements in the array: ");

scanf("%d", &n);

int arr[n];

// Input the elements of the array

printf("Enter %d elements:\n", n);

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

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

// Input the target element to search

printf("Enter the element to search: ");

scanf("%d", &target);

// Linear search logic

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

if (arr[i] == target) {

printf("Element %d found at index %d\n", target, i);


Department: CSE-B MSEC R2024
PAGE NO
found = 1;

break; // Exit the loop once the element is found

if (!found) {

printf("Element %d not found in the array.\n", target);

return 0;

OUTPUT:
Enter the number of elements in the array: 5

Enter 5 elements:

10 20 30 40 50

Enter the element to search: 30

Element 30 found at index 2

Department: CSE-B MSEC R2024


PAGE NO
Meenakshi Sundararajan Engineering College
(An Autonomous Institution, Affiliated to Anna University, Chennai) U24CS101 -
Programming in C - Laboratory Component

REGISTER NUMBER:243115104062

PROGRAM:
#include <stdio.h>

int main() {
int r1, c1, r2, c2;

// Input dimensions of the first matrix


printf("Enter rows and columns of the first matrix: ");
scanf("%d %d", &r1, &c1);

// Input dimensions of the second matrix


printf("Enter rows and columns of the second matrix: ");
scanf("%d %d", &r2, &c2);

// Check if multiplication is possible


if (c1 != r2) {
printf("Matrix multiplication not possible. Columns of the first matrix must equal rows
of the second.\n");
return 0;
}

int matrix1[r1][c1], matrix2[r2][c2], result[r1][c2];

// Input first matrix


printf("Enter elements of the first matrix (%d x %d):\n", r1, c1);
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c1; j++) {
scanf("%d", &matrix1[i][j]);
}
}

// Input second matrix


printf("Enter elements of the second matrix (%d x %d):\n", r2, c2);
for (int i = 0; i < r2; i++) {
for (int j = 0; j < c2; j++) {
scanf("%d", &matrix2[i][j]);
}
}

Department: CSE-B MSEC R2024


PAGE NO
Meenakshi Sundararajan Engineering College
(An Autonomous Institution, Affiliated to Anna University, Chennai)
U24CS101 - Programming in C - Laboratory Component

REGISTER NUMBER:243115104062

// Initialize result matrix to zero


for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
result[i][j] = 0;
}
}

// Matrix multiplication logic


for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
for (int k = 0; k < c1; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}

// Print the result matrix


printf("Resultant matrix (%d x %d):\n", r1, c2);
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}
return 0;
}
OUTPUT:

Enter rows and columns of the first matrix: 2 2


Enter rows and columns of the second matrix: 2 2
Enter elements of the first matrix (2 x 2):
10
01
Enter elements of the second matrix (2x 2):
12
34
Resultant matrix (2 x 2):
12
34
Department: CSE-B MSEC R2024
PAGE NO
Meenakshi Sundararajan Engineering College
(An Autonomous Institution, Affiliated to Anna University, Chennai)
U24CS101 - Programming in C - Laboratory Component

REGISTERNUMBER: 234115104062

PROGRAM:
#include <stdio.h>
#include <string.h>

int main() {
char str[100];
int len;

// Input the string


printf("Enter a string: ");
scanf("%s", str);
len = strlen(str);

// Calculate and display the length of the string


printf("The length of the string \"%s\" is: %lu\n", str,len);

return 0;
}

OUTPUT:
Enter a string: Hello
The length of the string "Hello" is: 5

Department: CSE-B MSEC R2024


PAGE NO
Meenakshi Sundararajan Engineering College
(An Autonomous Institution, Affiliated to Anna University, Chennai)
U24CS101 - Programming in C - Laboratory Component

REGISTERNUMBER: 234115104062

PROGRAM:
#include <stdio.h>
#include <string.h>

int main() {
char str1[100], str2[100];

printf("Enter the first string: ");


scanf("%s", str1);
printf("Enter the second string: ");
scanf("%s", str2);

// Concatenate str2 to str1


strcat(str1, str2);

// Display the result


printf("The concatenated string is: %s\n", str1);
return 0;
}

OUTPUT:
Enter the first string: Hello
Enter the second string: World
The concatenated string is: HelloWorld

Department: CSE-B MSEC R2024


PAGE NO
Meenakshi Sundararajan Engineering College
(An Autonomous Institution, Affiliated to Anna University, Chennai)
U24CS101 - Programming in C - Laboratory Component

REGISTERNUMBER: 234115104062

PROGRAM:
#include <stdio.h>
#include <string.h>

int main() {
char str1[100], str2[100];

// Input a string to copy


printf("Enter a string: ");
scanf("%s", str1);

// Copy the contents of str1 to str2


strcpy(str2, str1);

// Display the copied string


printf("The copied string is: %s\n", str2);

return 0;
}

OUTPUT:
Enter a string: Hello
The copied string is: Hello

Department: CSE-B MSEC R2024


PAGE NO
Meenakshi Sundararajan Engineering College
(An Autonomous Institution, Affiliated to Anna University, Chennai)
U24CS101 - Programming in C - Laboratory Component

REGISTERNUMBER: 234115104062

PROGRAM:
#include <stdio.h>
#include <string.h>

int main() {
char str1[100], str2[100];

// Input two strings


printf("Enter the first string: ");
scanf("%s", str1);
printf("Enter the second string: ");
scanf("%s", str2);

// Compare the two strings


int result = strcmp(str1, str2);

// Display the result of comparison


if (result == 0) {
printf("The strings are equal.\n");
} else if (result > 0) {
printf("The first string is greater than the second string.\n");
} else {
printf("The first string is smaller than the second string.\n");
}

return 0;
}
OUTPUT:
Enter the first string: hello
Enter the second string: hello
The strings are equal.

Department: CSE-B MSEC R2024


PAGE NO
Meenakshi Sundararajan Engineering College
(An Autonomous Institution, Affiliated to Anna University, Chennai)
U24CS101 - Programming in C - Laboratory Component

REGISTERNUMBER: 234115104062

PROGRAM:
#include <stdio.h>
#include <string.h>

#define MAX_USERS 5

// Arrays to store account numbers, PINs, and balances


int accountNumbers[MAX_USERS] = {101, 102, 103, 104, 105};
char pins[MAX_USERS][5] = {"1234", "2345", "3456", "4567", "5678"};
double balances[MAX_USERS] = {1000.0, 2000.0, 1500.0, 500.0, 750.0};

// Function declarations
int authenticateUser();
void displayMenu();
void checkBalance(int userIndex);
void deposit(int userIndex);
void withdraw(int userIndex);

int main() {
int userIndex = authenticateUser();

if (userIndex == -1) {
printf("Authentication failed. Exiting...\n");
return 0; // Exit the program if authentication fails
Department: CSE-B MSEC R2024
PAGE NO
Meenakshi Sundararajan Engineering College
(An Autonomous Institution, Affiliated to Anna University, Chennai)
U24CS101 - Programming in C - Laboratory Component

REGISTERNUMBER: 234115104062
}

int choice;
while (1) {
displayMenu();
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
checkBalance(userIndex);
break;
case 2:
deposit(userIndex);
break;
case 3:
withdraw(userIndex);
break;
case 4:
printf("Thank you for using the ATM. Goodbye!\n");
return 0;
default:
printf("Invalid choice. Please try again.\n");
}
}
Department: CSE-B MSEC R2024
PAGE NO
Meenakshi Sundararajan Engineering College
(An Autonomous Institution, Affiliated to Anna University, Chennai)
U24CS101 - Programming in C - Laboratory Component

REGISTERNUMBER: 234115104062
return 0;
}

// Function to authenticate the user with account number and PIN


int authenticateUser() {
int accountNumber;
char pin[5];

// Ask the user for their account number and PIN


printf("Enter account number: ");
scanf("%d", &accountNumber);

printf("Enter PIN: ");


scanf("%s", pin);

// Check for the correct account number and PIN


for (int i = 0; i < MAX_USERS; i++) {
if (accountNumbers[i] == accountNumber && strcmp(pins[i], pin) == 0) {
printf("Authentication successful.\n");
return i; // Return the index of the user in the arrays
}
}
return -1; // Invalid account number or PIN
}
Department: CSE-B MSEC R2024
PAGE NO
Meenakshi Sundararajan Engineering College
(An Autonomous Institution, Affiliated to Anna University, Chennai)
U24CS101 - Programming in C - Laboratory Component

REGISTERNUMBER: 234115104062
// Function to display the ATM menu
void displayMenu() {
printf("\nATM Menu:\n");
printf("1. Check Balance\n");
printf("2. Deposit\n");
printf("3. Withdraw\n");
printf("4. Exit\n");
}

// Function to check the balance of the user's account


void checkBalance(int userIndex) {
printf("Your current balance is: $%.2f\n", balances[userIndex]);
}

// Function to deposit money into the user's account


void deposit(int userIndex) {
double amount;
printf("Enter deposit amount: $");
scanf("%lf", &amount);

if (amount > 0) {
balances[userIndex] += amount;
printf("You have successfully deposited $%.2f. New balance: $%.2f\n", amount,
balances[userIndex]);
}
Department: CSE-B MSEC R2024
PAGE NO
Meenakshi Sundararajan Engineering College
(An Autonomous Institution, Affiliated to Anna University, Chennai)
U24CS101 - Programming in C - Laboratory Component

REGISTERNUMBER: 234115104062

else {
printf("Invalid deposit amount.\n");
}
}

// Function to withdraw money from the user's account


void withdraw(int userIndex) {
double amount;
printf("Enter withdrawal amount: $");
scanf("%lf", &amount);

if (amount > 0 && amount <= balances[userIndex]) {


balances[userIndex] -= amount;
printf("You have successfully withdrawn $%.2f. New balance: $%.2f\n", amount,
balances[userIndex]);
} else {
printf("Insufficient balance or invalid amount.\n");
}
}

Department: CSE-B MSEC R2024


PAGE NO
Meenakshi Sundararajan Engineering College
(An Autonomous Institution, Affiliated to Anna University, Chennai)
U24CS101 - Programming in C - Laboratory Component

REGISTERNUMBER: 234115104062

OUTPUT:

Enter account number: 101


Enter PIN: 1234
Authentication successful.

ATM Menu:
1. Check Balance
2. Deposit
3. Withdraw
4. Exit
Enter your choice: 1
Your current balance is: $1000.00

ATM Menu:
1. Check Balance
2. Deposit
3. Withdraw
4. Exit
Enter your choice: 2
Enter deposit amount: $500
You have successfully deposited $500.00. New balance: $1500.00

ATM Menu:
1. Check Balance
2. Deposit
3. Withdraw
4. Exit
Enter your choice: 3
Enter withdrawal amount: $200
You have successfully withdrawn $200.00. New balance: $1300.00

Department: CSE-B MSEC R2024


PAGE NO
Meenakshi Sundararajan Engineering College
(An Autonomous Institution, Affiliated to Anna University, Chennai)
U24CS101 - Programming in C - Laboratory Component

REGISTERNUMBER: 234115104062
ATM Menu:
1. Check Balance
2. Deposit
3. Withdraw
4. Exit
Enter your choice: 4
Thank you for using the ATM. Goodbye!

RESULT:
The program for ATM stimulation was successfully executed and the output
was verified.

Department: CSE-B MSEC R2024


PAGE NO

Meenakshi Sundararajan Engineering College


(An Autonomous Institution, Affiliated to Anna University, Chennai)
U24CS101 - Programming in C - Laboratory Component

REGISTER NUMBER:234115104062
PROGRAM:
#include<stdio.h>
Float cel(float t1);
Float far(float t2)

Int main(){

Float t1,t2,conv1,conv2;

Printf(“Enter the temperature in celcius:”);


Scanf(“%d”,&t1);
Printf(“Enter the temperature in farenheit:”);
Scanf(“%d”,&t2);

Conv1=cel(t1);
Printf(“\n temperature in farenhiet %f”,conv1);
Conv2=far(t2);
Printf(“\n temperature in celcius %f”,conv2);

return 0;
}

Float cel(float t1){


return(t1*(9.0/5)+32);
}
Float far(float t2){
return((t2-32)*(5/9));
}

OUTPUT:
Enter temperature in celcius:25
Temperature in farenheit:77
Enter temperature in farenheit:98.6
Temperature in celcius:37

Department: CSE-B MSEC R2024


PAGE NO
Meenakshi Sundararajan Engineering College
(An Autonomous Institution, Affiliated to Anna University, Chennai)
U24CS101 - Programming in C - Laboratory Component

REGISTERNUMBER:234115104062
PROGRAM:
#include<stdio.h>
int main(){

Int a=10,b=7;
Printf(“\n Before swapping: a=%d,b=%d”,a,b);
Swap(&a,&b);
Printf(“\n After swapping: a=%d,b=%d”,a,b);
Return 0;
}

Int swap(int *p,int*q){


Int temp;
Temp=*p;
*p=*q;
*q=temp;
return p,q;
}
OUTPUT:
Before swapping: a=10,b=7

After swapping: a=10,b=7

Department: CSE-B MSEC R2024


PAGE NO

You might also like