0% found this document useful (0 votes)
30 views65 pages

Ilovepdf Merged

Uploaded by

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

Ilovepdf Merged

Uploaded by

greycloud791
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 65

Program Coding:

#include <stdio.h>
void main() {
int i, n, num, a[100], found = 0;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
printf("Enter %d elements in the array:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
printf("Enter the element to be searched: ");
scanf("%d", &num);
for (i = 0; i < n; i++) {
if (a[i] == num) {
printf("%d is found at index %d\n", num, i);
found = 1;
break;
}
}
if (!found) {
printf("%d is not found in the array\n", num);
}
}
Output:
Program Coding:
#include <stdio.h>
void main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0) {
printf("%d is positive.\n", num);
}
else if (num < 0) {
printf("%d is negative.\n", num);
}
else {
printf("The number is zero.\n");
}
}
Output:
Program Coding:
#include <stdio.h>
void main() {
int n, i = 1, sum = 0;
float avg;
printf("Enter the value of n: ");
scanf("%d", &n);
do {
sum += i;
i++;
}
while (i <= n);
avg = (float)sum / n;
printf("The sum of first %d numbers = %d\n", n, sum);
printf("The average of first %d numbers = %.2f\n", n, avg);
}
Output:
Program Coding:
#include <stdio.h>
void main() {
int num1, num2, sum;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
sum = num1 + num2;
printf("The sum of %d and %d is: %d\n", num1, num2, sum);
}
Output:
Program Coding:
#include <stdio.h>
void main() {
float num1, num2, average;
printf("Enter the first number: ");
scanf("%f", &num1);
printf("Enter the second number: ");
scanf("%f", &num2);
average = (num1 + num2) / 2;
printf("The average of %.2f and %.2f is: %.2f\n", num1, num2,
average);
}
Output:
Program Coding:
#include <stdio.h>
#include <math.h>
float PI = 3.14;
void main() {
double radius, area, circumference;
printf("Enter the radius of the circle: ");
scanf("%lf", &radius);
area = PI * radius * radius;
circumference = 2 * PI * radius;
printf("Area of the circle: %.2f\n", area);
printf("Circumference of the circle: %.2f\n", circumference);
}
Output:
Program Coding:
#include <stdio.h>
void main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
if (number % 2 == 0) {
printf("%d is even.\n", number);
}
else {
printf("%d is odd.\n", number);
}
}
Output:
Program Coding:
#include <stdio.h>
void main()
{
int a[10], n, i, search, count = 0;
printf("Enter the number of elements:\t");
scanf("%d", &n);
printf("\nEnter %d numbers:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
printf("\nArray Elements:\n");
for (i = 0; i < n; i++) {
printf("%d\t", a[i]);
}
printf("\n\nEnter the Element to be searched:\t");
scanf("%d", &search);
for (i = 0; i < n; i++){
if (search == a[i]){
count++;
}
}
if (count == 0) {
printf("\nElement %d is not present in the array\n", search);
}
else{
printf("\nElement %d is present %d times in the array\n", search, count);
}
}
Output:
Program Coding:
#include <stdio.h>
void insert(int a[], int n) /* function to sort an aay with insertion sort */
{
int i, j, temp;
for (i = 1; i < n; i++) {
temp = a[i];
j = i - 1;
while(j>=0 && temp <= a[j]) /* Move the elements greater than temp to one
position ahead from their
current position*/
{
a[j+1] = a[j];
j = j-1;
}
a[j+1] = temp;
}
}
void printArr(int a[], int n) /* function to print the array */
{
int i;
for (i = 0; i < n; i++)
printf("%d ", a[i]);
}
int main()
{
int a[] = { 12, 31, 25, 8, 32, 17 };
int n = sizeof(a) / sizeof(a[0]);
printf("Before sorting array elements are - \n");
printArr(a, n);
insert(a, n);
printf("\nAfter sorting array elements are - \n");
printArr(a, n);
return 0;
}

Output:
Program Coding:
#include <stdio.h>

// Function to perform quicksort


void qsort(int arr[], int fst, int last);

// Main function
int main() {
int arr[30]; // Array declaration
int i, size;

// Input: size of the array


printf("Enter total number of elements: ");
scanf("%d", &size);

// Input: array elements


printf("Enter %d elements: \n", size);
for (i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}

// Quick sort
qsort(arr, 0, size - 1);

// Output: sorted array


printf("Quick sorted elements are: \n");
for (i = 0; i < size; i++) {
printf("%d\t", arr[i]);
}
printf("\n");

return 0;
}

// Function to perform quicksort


void qsort(int arr[], int fst, int last) {
int i, j, pivot, tmp;

if (fst < last) {


pivot = fst; // Set pivot as the first element
i = fst;
j = last;

// Partitioning logic
while (i < j) {
while (arr[i] <= arr[pivot] && i < last) {
i++;
}
while (arr[j] > arr[pivot]) {
j--;
}
if (i < j) { // Swap elements
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}

// Swap the pivot element with the j-th element


tmp = arr[pivot];
arr[pivot] = arr[j];
arr[j] = tmp;

// Recursively quicksort the left and right parts


qsort(arr, fst, j - 1);
qsort(arr, j + 1, last);
}
}

Output:
Program Coding:
#include <stdio.h>

// Function prototypes
void merge(int arr[], int min, int mid, int max);
void part(int arr[], int min, int max);

// Main function
int main() {
int arr[30];
int i, size;

// Input: size of the array


printf("\n\t------- Merge sorting method --------------\n\n");
printf("Enter total number of elements: ");
scanf("%d", &size);

// Input: array elements


for (i = 0; i < size; i++) {
printf("Enter element %d: ", i + 1);
scanf("%d", &arr[i]);
}

// Sort the array using merge sort


part(arr, 0, size - 1);

// Output: sorted array


printf("\n\t------- Merge sorted elements --------------\n\n");
for (i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");

return 0;
}

// Function to divide the array into parts


void part(int arr[], int min, int max) {
int mid;

if (min < max) {


mid = (min + max) / 2;

// Recursively divide the array


part(arr, min, mid);
part(arr, mid + 1, max);

// Merge the divided arrays


merge(arr, min, mid, max);
}
}

// Function to merge the divided arrays


void merge(int arr[], int min, int mid, int max) {
int tmp[30]; // Temporary array
int i, j, k, m;

j = min; // Start of the left part


m = mid + 1; // Start of the right part
// Merging the arrays
for (i = min; j <= mid && m <= max; i++) {
if (arr[j] <= arr[m]) {
tmp[i] = arr[j];
j++;
} else {
tmp[i] = arr[m];
m++;
}
}

// Copy the remaining elements of the left half, if any


if (j > mid) {
for (k = m; k <= max; k++) {
tmp[i] = arr[k];
i++;
}
} else {
// Copy the remaining elements of the right half, if any
for (k = j; k <= mid; k++) {
tmp[i] = arr[k];
i++;
}
}

// Copy the merged temporary array back to the original array


for (k = min; k <= max; k++) {
arr[k] = tmp[k];
}
}

Output:
Program Coding:
#include <stdio.h>
#include <stdlib.h>

#define MAX 10

int create(int);
void linearprob(int[], int, int);
void display(int[]);

int main() {
int a[MAX], num, key, i;
char ans;

// Initialize the hash table


for (i = 0; i < MAX; i++)
a[i] = -1;

printf("\nCOLLISION HANDLING BY LINEAR PROBING");

do {
printf("\nEnter the number: ");
scanf("%d", &num);
key = create(num);
linearprob(a, key, num);

printf("\nDo you wish to continue? (y/n): ");


scanf(" %c", &ans); // Note the space before %c to consume any newline
characters
} while (ans == 'y' || ans == 'Y');
display(a);
return 0;
}

int create(int num) {


int key = num % 10; // Create hash key using modulo operation
return key;
}

void linearprob(int a[MAX], int key, int num) {


int flag = 0, i, count = 0;

// Check if the calculated key is empty


if (a[key] == -1) {
a[key] = num; // Place number directly if slot is empty
} else {
// Check if the table is full
for (i = 0; i < MAX; i++) {
if (a[i] != -1) {
count++;
}
}

if (count == MAX) {
printf("\nHash Table is full");
display(a);
exit(1);
}
// Linear probing to find the next available slot
for (i = (key + 1) % MAX; i != key; i = (i + 1) % MAX) {
if (a[i] == -1) {
a[i] = num;
flag = 1;
break;
}
}

// If not found, wrap around and check from the beginning


if (flag == 0) {
for (i = 0; i < key; i++) {
if (a[i] == -1) {
a[i] = num;
flag = 1;
break;
}
}
}
}
}

void display(int a[MAX]) {


int i;
printf("\nHash Table is..\n");
for (i = 0; i < MAX; i++)
printf("Index %d: %d\n", i, a[i]);
}
Output:
Program Coding:
#include <stdio.h>
#include <stdlib.h>

// Define the structure for the binary search tree nodes


typedef struct bst {
int data;
struct bst *left, *right;
} node;

// Function declarations
node *getnode();
void insert(node *, node *);
void inorder(node *);
node *search(node *, int, node **);
void del(node *, int);

int main() {
int choice, key;
char ans = 'N';
node *newnode, *root = NULL, *temp, *parent;

do {
printf("\n\tProgram for Binary Search Tree");
printf("\n\t1. Create");
printf("\n\t2. Search");
printf("\n\t3. Delete");
printf("\n\t4. Display");
printf("\n\t5. Exit");
printf("\n\tEnter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
do {
newnode = getnode();
printf("\n\tEnter the element: ");
scanf("%d", &newnode->data);

if (root == NULL)
root = newnode;
else
insert(root, newnode);

printf("\n\tDo you want to enter more elements? (y/n): ");


ans = getchar(); // Use getchar() to handle input properly
ans = getchar(); // This will capture the next input (enter key)
} while (ans == 'y' || ans == 'Y');
break;

case 2:
printf("\n\tEnter the element to be searched: ");
scanf("%d", &key);
temp = search(root, key, &parent);
if (temp != NULL)
printf("\n\tParent of node %d is %d", temp->data, parent ? parent-
>data : -1);
else
printf("\n\tElement not found.");
break;

case 3:
printf("\n\tEnter the element to be deleted: ");
scanf("%d", &key);
del(root, key);
break;

case 4:
if (root == NULL)
printf("\n\tTree is not created.");
else {
printf("\n\tThe Tree is: ");
inorder(root);
}
break;

case 5:
printf("\n\tExiting...");
break;

default:
printf("\n\tInvalid choice.");
}
} while (choice != 5);

return 0;
}

// Function to create a new node


node *getnode() {
node *temp = (node *)malloc(sizeof(node));
temp->left = NULL;
temp->right = NULL;
return temp;
}

// Function to insert a new node into the binary search tree


void insert(node *root, node *newnode) {
if (newnode->data > root->data) {
if (root->right == NULL)
root->right = newnode;
else
insert(root->right, newnode);
} else if (newnode->data < root->data) {
if (root->left == NULL)
root->left = newnode;
else
insert(root->left, newnode);
}
}

// Inorder traversal to display the tree


void inorder(node *temp) {
if (temp != NULL) {
inorder(temp->left);
printf(" %d", temp->data);
inorder(temp->right);
}
}
// Function to search for a node in the tree
node *search(node *root, int key, node **parent) {
node *temp = root;
*parent = NULL;

while (temp != NULL) {


if (temp->data == key) {
printf("\n\tThe %d element is present", temp->data);
return temp;
}
*parent = temp;
if (key < temp->data)
temp = temp->left;
else
temp = temp->right;
}
return NULL;
}

// Function to delete a node from the tree


void del(node *root, int key) {
node *temp, *parent = NULL, *succ;

temp = search(root, key, &parent);


if (temp == NULL) {
printf("\n\tElement not found.");
return;
}
// Case 1: Node with two children
if (temp->left != NULL && temp->right != NULL) {
succ = temp->right;
parent = temp;

while (succ->left != NULL) {


parent = succ;
succ = succ->left;
}

temp->data = succ->data;
temp = succ;
}

// Case 2: Node with one or zero children


node *child = (temp->left != NULL) ? temp->left : temp->right;

if (parent == NULL) // Deleting the root node


root = child;
else if (parent->left == temp)
parent->left = child;
else
parent->right = child;

free(temp);
printf("\n\tNode deleted successfully.");
}
Output:
Program Coding:
#include<stdio.h>

int main() {
int first, last, middle, size, i, sElement, list[100];

// Input the size of the list


printf("Enter the size of the list: ");
scanf("%d", &size);

// Input the elements in ascending order


printf("Enter %d integer values in ascending order:\n", size);

for (i = 0; i < size; i++) {


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

// Input the element to search


printf("Enter the value to be searched: ");
scanf("%d", &sElement);

// Initialize the first, last, and middle indices


first = 0;
last = size - 1;
middle = (first + last) / 2;

// Perform binary search


while (first <= last) {
if (list[middle] < sElement) {
first = middle + 1;
} else if (list[middle] == sElement) {
printf("Element found at index %d.\n", middle);
break;
} else {
last = middle - 1;
}
middle = (first + last) / 2;
}

// If the element is not found


if (first > last) {
printf("Element not found in the list.\n");
}

return 0;
}

Output:
Program Coding:
#include <stdlib.h>
struct node {
int item;
struct node* left;
struct node* right;
};

void inorderTraversal(struct node* root) {


if (root == NULL) return;
inorderTraversal(root->left);
printf("%d ->", root->item);
inorderTraversal(root->right);
}

void preorderTraversal(struct node* root) {


if (root == NULL) return;
printf("%d ->", root->item);
preorderTraversal(root->left);
preorderTraversal(root->right);
}

void postorderTraversal(struct node* root) {


if (root == NULL) return;
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ->", root->item);
}
struct node* createNode(int value) {
struct node* newNode = malloc(sizeof(struct node));
newNode->item = value;
newNode->left = NULL;
newNode->right = NULL;

return newNode;
}
struct node* insertLeft(struct node* root, int value) {
root->left = createNode(value);
return root->left;
}
struct node* insertRight(struct node* root, int value) {
root->right = createNode(value);
return root->right;
}
int main() {
int rootValue;
printf("Enter the value for the root node: ");
scanf("%d", &rootValue);
struct node* root = createNode(rootValue);
int leftValue;
printf("Enter the value for the left child of the root: ");
scanf("%d", &leftValue);
insertLeft(root, leftValue);
int rightValue;
printf("Enter the value for the right child of the root: ");
scanf("%d", &rightValue);
insertRight(root, rightValue);
int leftChildValue;
printf("Enter the value for the left child of the left child of the root: ");
scanf("%d", &leftChildValue);
insertLeft(root->left, leftChildValue);
printf("\nInorder traversal \n");
inorderTraversal(root);
printf("\nPreorder traversal \n");
preorderTraversal(root);
printf("\nPostorder traversal \n");
postorderTraversal(root);

return 0;
}

Output:
Program Coding:
#include <stdio.h>
#include <stdlib.h>
void push();
void pop();
void display();
struct node
{
int val;
struct node *next;
};
struct node *head;

void main ()
{
int choice=0;
printf("\n*********Stack operations using linked list*********\n");
printf("\n----------------------------------------------\n");
while(choice != 4)
{
printf("\n\Choose one from the below options...\n");
printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");
printf("\n Enter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("Exiting....");
break;
}
default:
{
printf("Please Enter valid choice ");
}
};
}
}
void push ()
{
int val;
struct node *ptr = (struct node*)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("Not able to push the element");
}
else
{
printf("Enter the value: ");
scanf("%d",&val);
if(head==NULL)
{
ptr->val = val;
ptr -> next = NULL;
head=ptr;
}
else
{
ptr->val = val;
ptr->next = head;
head=ptr;

}
printf("Item pushed");

}
}

void pop()
{
int item;
struct node *ptr;
if (head == NULL)
{
printf("Underflow");
}
else
{
item = head->val;
ptr = head;
head = head->next;
free(ptr);
printf("Item popped");

}
}
void display()
{
int i;
struct node *ptr;
ptr=head;
if(ptr == NULL)
{
printf("Stack is empty\n");
}
else
{
printf("Printing Stack elements \n");
while(ptr!=NULL)
{
printf("%d\n",ptr->val);
ptr = ptr->next;
}
}
}

Output:
Program Coding:
#include<stdio.h>
#include<ctype.h>

char stack[100];
int top = -1;

void push(char x)
{
stack[++top] = x;
}

char pop()
{
if(top == -1)
return '\0'; // Return null character to signify an empty stack
else
return stack[top--];
}

int priority(char x)
{
if(x == '(')
return 0;
if(x == '+' || x == '-')
return 1;
if(x == '*' || x == '/')
return 2;
return 0;
}

int main()
{
char exp[100];
char *e, x;

printf("Enter the expression: ");


scanf("%s", exp);
printf("\n");

e = exp;

while(*e != '\0')
{
if(isalnum(*e))
printf("%c ", *e);
else if(*e == '(')
push(*e);
else if(*e == ')')
{
while((x = pop()) != '(')
printf("%c ", x);
}
else
{
while(top != -1 && priority(stack[top]) >= priority(*e)) // Ensure stack is not
empty before accessing
printf("%c ", pop());
push(*e);
}
e++;
}

while(top != -1)
{
printf("%c ", pop());
}

return 0;
}

Output:
Program Coding:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *front;
struct node *rear;
void insert();
void delete();
void display();
void main ()
{
int choice;
while(choice != 4)
{
printf("\n*************************Main Menu*****************************\n");

printf("\n=======================================================
==========\n");
printf("\n1.Insert an element\n2.Delete an element\n3.Display the
queue\n4.Exit\n");
printf("\nEnter your choice: ");
scanf("%d",& choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nEnter valid choice\n");
}
}
}
void insert()
{
struct node *ptr;
int item;

ptr = (struct node *) malloc (sizeof(struct node));


if(ptr == NULL)
{
printf("\nOVERFLOW\n");
return;
}
else
{
printf("Enter value:");
scanf("%d",&item);
ptr -> data = item;
if(front == NULL)
{
front = ptr;
rear = ptr;
front -> next = NULL;
rear -> next = NULL;
}
else
{
rear -> next = ptr;
rear = ptr;
rear->next = NULL;
}
}
}
void delete()
{
struct node *ptr;
if(front == NULL)
{
printf("\nUNDERFLOW\n");
return;
}
else
{
ptr = front;
front = front -> next;
free(ptr);
}
}
void display()
{
struct node *ptr;
ptr = front;
if(front == NULL)
{
printf("\nEmpty queue\n");
}
else
{ printf("\nPrinting values .....\n");
while(ptr != NULL)
{
printf("\n%d\n",ptr -> data);
ptr = ptr -> next;
}
}
}

Output:
Program Coding:
#include<stdio.h>
void main()
{
int queue[5],ch=1,front=0,rear=0,i,j=1,x=5;
printf("Queue using Array");
printf("\n1.Insertion \n2.Deletion \n3.Display \n4.Exit");
while(ch)
{
printf("\nEnter the Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(rear==x)
printf("\n Queue is Full");
else
{
printf("\n Enter no %d:",j++);
scanf("%d",&queue[rear++]);
}
break;
case 2:
if(front==rear)
{
printf("\n Queue is empty");
}
else
{
printf("\n Deleted Element is %d",queue[front++]);
x++;
}
break;
case 3:
printf("\nQueue Elements are:\n ");
if(front==rear)
printf("\n Queue is Empty");
else
{
for(i=front; i<rear; i++)
{
printf("%d",queue[i]);
printf("\n");
}
break;
case 4:
exit(0);
default:
printf("Wrong Choice: please see the options");
}
}
}
}
Output:
Program Coding:
#include<stdio.h>
#include <stdlib.h>
struct Employee {
char name[50];
int employeeId;
float salary;
};
void main() {
struct Employee *employeePtr;
employeePtr = (struct Employee *)malloc(sizeof(struct Employee));

if (employeePtr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
printf("Enter employee name: ");
scanf("%s", employeePtr->name);
printf("Enter employee ID: ");
scanf("%d", &employeePtr->employeeId);
printf("Enter employee salary: ");
scanf("%f", &employeePtr->salary);
printf("\nEmployee Details:\n");
printf("Name: %s\n", employeePtr->name);
printf("Employee ID: %d\n", employeePtr->employeeId);
printf("Salary: %.2f\n", employeePtr->salary);
free(employeePtr);
}
Output:
Program Coding:
#include <stdio.h>
int stack[100], choice, n, top, x, i;
void push(void);
void pop(void);
void display(void);
void peek(void);
int main() {
top = -1;
printf("\n Enter the size of STACK[Max = 100]:");
scanf("%d", &n);
printf("\n\t STACK OPERATIONS USING ARRAY");
printf("\n\t--------------------------------");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.PEEK\n\t 4.DISPLAY\n\t 5.EXIT");
do {
printf("\n Enter the Choice:");
scanf("%d", &choice);

switch (choice) {
case 1:
push();
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
printf("\n\t EXIT POINT ");
break;
default:
printf("\n\t Please Enter a Valid Choice(1/2/3/4/5)");
}
} while (choice != 5);
return 0;
}
void push() {
if (top >= n - 1) {
printf("\n\tSTACK is overflow");
} else {
printf(" Enter a value to be pushed:");
scanf("%d", &x);
top++;
stack[top] = x;
}
}
void pop() {
if (top <= -1) {
printf("\n\t Stack is underflow");
} else {
printf("\n\t The popped element is %d", stack[top]);
top--;
}
}
void peek() {
if (top >= 0) {
printf("\n The top element in STACK is %d", stack[top]);
} else {
printf("\n The STACK is empty");
}
}
void display() {
if (top >= 0) {
printf("\n The elements in STACK \n");
for (i = top; i >= 0; i--)
printf("\n%d", stack[i]);
printf("\n Press Next Choice");
} else {
printf("\n The STACK is empty");
}
}

Output:
Program Coding:
#include<stdio.h>
void main()
{
char op;
int first,second;
printf("Enter an operator(+,-,*,/):");
scanf("%c",&op);
printf("Enter two operands:");
scanf("%d %d",&first,&second);
switch(op){
case'+':
printf("%d+%d=%d",first,second,first+second);
break;
case'-':
printf("%d-%d=%d",first,second,first-second);
break;
case'*':
printf("%d*%d=%d",first,second,first*second);
case'/':
printf("%d/%d=%d",first,second,first/second);
break;
default:
printf("Error! Operator is not correct");
}
}
Output:
Program Coding:
#include<stdio.h>
#include<math.h>
void main()
{
float principal,rate,time,emi;
printf("Enter principal:");
scanf("%f",&principal);
printf("Enter rate:");
scanf("%f",&rate);
printf("Enter time in year:");
scanf("%f",&time);
rate=rate/(12*100);
time=time*12;
emi=(principal*rate*pow(1+rate,time))/(pow(1+rate,time)-1);
printf("Monthly EMI is=%f\n",emi);
}

Output:
Program Coding:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int i;
struct Student {
char name[50];
int marks;
};
void clearInputBuffer() {
int c;
while ((c = getchar()) != '\n' && c != EOF);
}
int main() {
int numStudents;
struct Student *students;
FILE *file;
printf("Enter number of students: ");
if (scanf("%d", &numStudents) != 1 || numStudents <= 0) {
fprintf(stderr, "Invalid input for number of students\n");
return 1;
}
clearInputBuffer();
students = (struct Student *)malloc(numStudents * sizeof(struct Student));
if (students == NULL) {
fprintf(stderr, "Memory allocation failed\n");
return 1;
}
for (i = 0; i < numStudents; i++) {
printf("For student%d\n", i + 1);
printf("Enter name: ");
if (fgets(students[i].name, 50, stdin) != NULL) {
students[i].name[strcspn(students[i].name, "\n")] = 0; // Remove newline
} else {
fprintf(stderr, "Error reading name\n");
free(students);
return 1;
}
printf("Enter marks: ");
if (scanf("%d", &students[i].marks) != 1) {
fprintf(stderr, "Invalid input for marks\n");
free(students);
return 1;
}
clearInputBuffer();
}
file = fopen("D:\\student.txt", "w");
if (file == NULL) {
fprintf(stderr, "Error opening file\n");
free(students);
return 1;
}
for (i = 0; i < numStudents; i++) {
fprintf(file, "Name:%s\n", students[i].name);
fprintf(file, "Marks=%d\n\n", students[i].marks);
}
fclose(file);
free(students);
printf("\nData has been written to D:\\student.txt\n");
}

Output:
Program Coding:
#include <stdio.h>
float celsiusToFahrenheit(float celsius) {
return (celsius * 9 / 5) + 32;
}
void main() {
int celsius, fahrenheit;
printf("Enter temperature in Celsius: ");
scanf("%d", &celsius);
fahrenheit = celsiusToFahrenheit(celsius);
printf("%d Celsius is equal to %d Fahrenheit\n", celsius, fahrenheit);
}
Output:
Program Coding:
#include <stdio.h>
#include <string.h>
int isPalindrome(char *str) {
char *start = str;
char *end = str + strlen(str) - 1;
while (end > start) {
if (*start != *end) {
return 0;
}
start++;
end--;
}
return 1;
}
void main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str);
if (isPalindrome(str)) {
printf("%s is a palindrome.\n", str);
} else {
printf("%s is not a palindrome.\n", str);
}
}

Output:
Program Coding:
#include <stdio.h>
float calculateAverage(int num1,int num2,int num3,int num4,int num5) {
float sum = num1 + num2 + num3 + num4 + num5;
return sum / 5;
}
void main() {
int num1, num2, num3, num4, num5;
printf("Enter five numbers: ");
scanf("%d %d %d %d %d", &num1, &num2, &num3, &num4, &num5);
float average = calculateAverage(num1, num2, num3, num4, num5);
printf("The average of the five numbers is: %.2f\n", average);
}
Output:
Program Coding:
#include <stdio.h>
void main() {
char ch;
printf("Enter an alphabet: ");
scanf("%c", &ch);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
printf("%c is a vowel.\n", ch);
} else {
printf("%c is a consonant.\n", ch);
}
}
Output:
Program Coding:
#include <stdio.h>
void main() {
int num;
for (num = 1; num <= 10; num++) {
printf("%d\n", num);
}
}
Output:

You might also like