Ilovepdf Merged
Ilovepdf Merged
#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>
// Main function
int main() {
int arr[30]; // Array declaration
int i, size;
// Quick sort
qsort(arr, 0, size - 1);
return 0;
}
// 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;
}
}
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;
return 0;
}
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;
do {
printf("\nEnter the number: ");
scanf("%d", &num);
key = create(num);
linearprob(a, key, num);
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;
}
}
// 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);
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;
}
temp->data = succ->data;
temp = succ;
}
free(temp);
printf("\n\tNode deleted successfully.");
}
Output:
Program Coding:
#include<stdio.h>
int main() {
int first, last, middle, size, i, sElement, list[100];
return 0;
}
Output:
Program Coding:
#include <stdlib.h>
struct node {
int item;
struct node* left;
struct node* right;
};
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;
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;
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: