1.
Evaluation of expression
#include<stdio.h>
#include<conio.h>
Void main()
int x,y,z,w;
float s;
clrscr();
printf("enter the values of x,y,z,w...");
scanf("%d%d%d",&x,&y,&z,&w);
s=((x+y)^2*(x+z))/w;
printf("the value of s=%f",s);
getch();
}
2. Temperature conversion problem
#include<stdio.h>
#include<conio.h>
int main()
float fahrenheit, celsius;
printf("Enter Fahrenheit: ");
scanf("%f",&fahrenheit);
celsius = (fahrenheit - 32)*5/9;
printf("Celsius: %f ", celsius);
return 0;
}
3. Program to convert days to months and days
#include<stdio.h>
main ()
int months, days ;
printf("Enter days") ;
scanf("%d", &days) ;
months = days / 30 ;
days = days % 30 ;
printf("Months = %d Days = %d", months, days) ;
}
4. maximum of three numbers
#include <stdio.h>
int main()
{
int a, b, c;
printf("Enter three numbers: \na: ");
scanf("%d", &a);
printf("b: ");
scanf("%d", &b);
printf("c: ");
scanf("%d", &c);
if (a > b && a > c)
printf("Biggest number is %d", a);
if (b > a && b > c)
printf("Biggest number is %d", b);
if (c > a && c > b)
printf("Biggest number is %d", c);
return 0;
}
5. calculate square root of five numbers (using goto statement)
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
float ans;
int i=2,count=0;
clrscr();
L:
ans=sqrt(i);
printf("The square root of %d is %f",i,ans);
i++;
count++;
if(count<=4)
goto L;
getch();
}
6. program to calculate the salary of an employee
#include<stdio.h>
#include<conio.h>
void main()
clrscr();
int bp1;
float bp,ta,da,tax,tot;
printf("Enter the basic pay");
scanf("%f",&bp);
bp1=bp/10000;
switch(bp1)
case 1:
ta=bp*5/100;
da=bp*3/100;
tax=bp*2/100;
break;
case 2:
ta=bp*7/100;
da=bp*5/100;
tax=bp*3/100;
break;
case 3:
ta=bp*9/100;
da=bp*7/100;
tax=bp*4/100;
break;
default:ta=bp*10/100;
da=bp*9/100;
tax=bp*5/100;
break;
tot=bp+(ta+da)-tax;
printf("ta=%f \nda=%f \ntax=%f",ta,da,tax);
printf("\n\nTotal is %f",tot);
getch();
}
7. Fibonacci Sequence Up to a Certain Number
#include <stdio.h>
int main() {
int t1 = 0, t2 = 1, nextTerm = 0, n;
printf("Enter a positive number: ");
scanf("%d", &n);
// displays the first two terms which is always 0 and 1
printf("Fibonacci Series: %d, %d, ", t1, t2);
nextTerm = t1 + t2;
while (nextTerm <= n) {
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
return 0;
}
8.pascal’s triangle
#include <stdio.h>
int main()
int n,c=1,k,i,j;
printf("Enter the number of rows: ");
scanf("%d",&n);
for(i=0;i<n;i++)
for(k=1;k<=n-i;k++)
printf(" ");
for(j=0;j<=i;j++)
{
if (j==0||i==0)
c=1;
else
c=c*(i-j+1)/j;
printf("%d ",c);
printf("\n");
return 0;
}
Sorting array in ascending and descending order
#include <stdio.h>
#include<conio.h>
int main()
{
int a[100],n,i,j, temp;
printf("Array size: ");
scanf("%d",&n);
printf("Elements: ");
for(i=0;i<n;i++)
{
scanf("%d", &a[i]);
}
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if (a[j] > a[i])
{
tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
}
printf("\n\nAscending : ");
for (i = 0; i < n; i++)
{
printf(" %d ", a[i]);
}
for (int i = 0; i < n; i++) {
for (j = 0; j < n; j++)
{
if (a[j] < a[i])
{
tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
}
printf("\n\nDescending : ");
for (i = 0; i < n; i++)
{
printf(" %d ", a[i]);
}
return 0;
getch();
}
Matrix multiplication
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
system("cls");
printf("enter the number of row=");
scanf("%d",&r);
printf("enter the number of column=");
scanf("%d",&c);
printf("enter the first matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the second matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("multiply of the matrix=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mul[i][j]=0;
for(k=0;k<c;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
}
}
//for printing result
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
return 0;
}
LowerCase to UpperCase
#include <stdio.h>
#include <string.h>
int main()
char s[100];
int i;
printf("\nEnter a string : ");
gets(s);
for (i = 0; s[i]!='\0'; i++) {
if(s[i] >= 'a' && s[i] <= 'z') {
s[i] = s[i] - 32;
printf("\nString in Upper Case = %s", s);
return 0;
}
Factorial of a Number Using Recursion
#include<stdio.h>
long int fact(int n);
int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, fact(n));
return 0;
}
long int fact(int n)
{
if (n>=1)
return n*fact(n-1);
else
return 1;
}
string operations using switch case in c program
#include <stdio.h>
#include <string.h>
void concatenate(char s1[], char s2[])
strcat(s1, s2);
printf("Concatenated string: %s\n", s1);
void compare(char s1[], char s2[]) {
int result = strcmp(s1, s2);
if (result == 0)
printf("Strings are equal\n");
else if (result < 0)
printf("String 1 is less than String 2\n");
else
printf("String 1 is greater than String 2\n");
void length(char s1[]) {
printf("Length of the string: %lu\n", strlen(s1));
}
int main() {
char str1[100], str2[100];
int choice;
printf("Enter the first string: ");
gets(str1);
printf("Enter the second string: ");
gets(str2);
printf("Choose an operation to perform:\n");
printf("1. Concatenate strings\n");
printf("2. Compare strings\n");
printf("3. Find length of first string\n");
scanf("%d", &choice);
switch (choice) {
case 1:
concatenate(str1, str2);
break;
case 2:
compare(str1, str2);
break;
case 3:
length(str1);
break;
default:
printf("Invalid choice\n");
break;
return 0;
structure Program on Hotel
#include< stdio.h>
#include< string.h>
struct s
char name[40],add[40],grade;
float charge;
int num;
};
void hot1(struct s *ob,char g,int n)
/* this function will display the hotel names of specified grade in order of
charge.*/
int i,j,x=0;
char final[10][40],dummy[40];
float r[10],t;
for(i=0;i<n;i++)
{
if(g==ob[i].grade)
strcpy(final[x],ob[i].name);
r[x]=ob[i].charge;
x++;
for(i=0;i<x;i++)
for(j=i+1;j<x;j++)
if(r[i]>r[j])
t=r[i];
r[i]=r[j];
r[j]=t;
strcpy(dummy,final[i]);
strcpy(final[i],final[j]);
strcpy(final[j],dummy);
}
puts(“Hotel names with specified GRADE in order of charge.”);
for(i=0;i<x;i++)
printf(“\n%s%10.2f”,final[i],r[i]);
void hot2(struct s *ob,float r1,int n)
/* this function will display hotel names below the specified rate*/
int i,x=0;
char final[10][40];
float r[10];
for(i=0;i<n;i++)
if(r1>ob[i].charge)
r[x]=ob[i].charge;
strcpy(final[x],ob[i].name);
x++;
puts(“Hotel names below the specified RATE.”);
for(i=0;i<x;i++)
{
printf(“\n%s%10.2f”,final[i],r[i]);
void main()
struct s hotel[10];
int n,i;
char g;
float rate;
clrscr();
puts(“Enter the number of hotels(within 10):-“);
scanf(“%d”,&n);
for(i=0;i<n;i++)
fflush(stdin);
puts(“Enter the hotel name:-“);
gets(hotel[i].name);
puts(“Enter the hotel address:-“);
gets(hotel[i].add);
puts(“Enter the hotel grade:-(‘a’,’b’,’c’)”);
scanf(“%c”,&hotel[i].grade);
puts(“Enter the room charge:-“);
scanf(“%f”,&hotel[i].charge);
puts(“Enter the room numbers:-“);
scanf(“%d”,&hotel[i].num);
clrscr();
fflush(stdin);
puts(“Now we will search for specific hotels(Press any key)….”);
getch();
clrscr();
printf(“\nEnter the grade for hotel(‘a’,’b’,’c’):-“);
scanf(“%c”,&g);
hot1(hotel,g,n);
printf(“\nPress any key to continue….”);
getch();
printf(“\nEnter the charge:-“);
scanf(“%f”,&rate);
puts(“\n**************\n”);
hot2(hotel,rate,n);
getch();
}
Pointers in structures
#include <stdio.h>
#include <string.h>
struct book{
char title[10];
double price;
int pages;
};
int main(){
struct book b1 = {"Learn C", 675.50, 325};
struct book *strptr;
strptr = &b1;
printf("Title: %s\n", strptr -> title);
printf("Price: %lf\n", strptr -> price);
printf("No of Pages: %d\n", strptr -> pages);
return 0;
}
Cricket team details using union:
#include <stdio.h>
#include <string.h>
// Define a union to store player name or score
union PlayerInfo {
char name[30];
int score;
};
// Define a structure to represent a cricket player
struct Player {
int id;
union PlayerInfo info;
int isName; // Flag to indicate whether 'info' stores a name (1) or a score (0)
};
// Function to set player name
void setPlayerName(struct Player *player, int id, const char *name) {
player->id = id;
strcpy(player->info.name, name);
player->isName = 1;
}
// Function to set player score
void setPlayerScore(struct Player *player, int id, int score) {
player->id = id;
player->info.score = score;
player->isName = 0;
}
// Function to display player info
void displayPlayerInfo(struct Player *player) {
printf("Player ID: %d\n", player->id);
if (player->isName) {
printf("Player Name: %s\n", player->info.name);
} else {
printf("Player Score: %d\n", player->info.score);
}
}
// Main function
int main() {
struct Player team[11]; // Array to store 11 players
// Set and display information for the players
setPlayerName(&team[0], 1, "Sachin Tendulkar");
setPlayerScore(&team[1], 2, 85);
setPlayerName(&team[2], 3, "Virat Kohli");
setPlayerScore(&team[3], 4, 75);
// Display player info
for (int i = 0; i < 4; i++) {
displayPlayerInfo(&team[i]);
printf("--------------------\n");
}
return 0;
}
macros to calculate the maximum and minimum of two numbers in C:
#include <stdio.h>
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
int main() {
int num1 = 10, num2 = 20;
printf("Maximum of %d and %d is %d\n", num1, num2, MAX(num1, num2));
printf("Minimum of %d and %d is %d\n", num1, num2, MIN(num1, num2));
return 0;
}
Write a C program to swap two numbers using pointers
#include <stdio.h>
void swap_numbers(int *ptr1, int *ptr2) {
int temp;
temp = *ptr1;
*ptr1 = *ptr2;
*ptr2 = temp;
}
void main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
swap_numbers(&num1, &num2);
printf("The swapped numbers are: %d %d", num1, num2);
}
LINKED LIST
#include<stdio.h>
#include<stdlib.h>
struct linked_list
{
int number;
struct linked_list *next;
};
typedef struct linked_list node;
node *head=NULL, *last=NULL;
void create_linked_list();
void print_linked_list();
void insert_at_last(int value);
void insert_at_first(int value);
void insert_after(int key, int value);
void delete_item(int value);
void search_item(int value);
int main()
{
int key, value;
//Create a linked list
printf("Create Linked List\n");
create_linked_list();
print_linked_list();
//Insert value at last position to existing Linked List
printf("\nInsert new item at last\n");
scanf("%d", &value);
insert_at_last(value);
print_linked_list();
//Insert value at first position to existing Linked List
printf("\nInsert new item at first\n");
scanf("%d", &value);
insert_at_first(value);
print_linked_list();
//Insert value after a defined value to existing Linked List
printf("\nEnter a KEY (existing item of List), after that you want to insert a
value\n");
scanf("%d", &key);
printf("\nInsert new item after %d KEY\n", key);
scanf("%d", &value);
insert_after(key, value);
print_linked_list();
//Search an item from Linked List
printf("\nEnter an item to search it from List\n");
scanf("%d", &value);
search_item(value);
//Delete value from List
printf("\nEnter a value, which you want to delete from list\n");
scanf("%d", &value);
delete_item(value);
print_linked_list();
return 0;
}
/*
User Defined Functions
*/
void create_linked_list()
{
int val;
while(1)
{
printf("Input a number. (Enter -1 to exit)\n");
scanf("%d", &val);
if(val==-1)
break;
insert_at_last(val);
}
void insert_at_last(int value)
{
node *temp_node;
temp_node = (node *) malloc(sizeof(node));
temp_node->number=value;
temp_node->next=NULL;
//For the 1st element
if(head==NULL)
{
head=temp_node;
last=temp_node;
}
else
{
last->next=temp_node;
last=temp_node;
}
void insert_at_first(int value)
{
node *temp_node = (node *) malloc(sizeof(node));
temp_node->number=value;
temp_node->next = head;
head = temp_node;
}
void insert_after(int key, int value)
{
node *myNode = head;
int flag = 0;
while(myNode!=NULL)
{
if(myNode->number==key)
{
node *newNode = (node *) malloc(sizeof(node));
newNode->number = value;
newNode->next = myNode->next;
myNode->next = newNode;
printf("%d is inserted after %d\n", value, key);
flag = 1;
break;
}
else
myNode = myNode->next;
}
if(flag==0)
printf("Key not found!\n");
void delete_item(int value)
{
node *myNode = head, *previous=NULL;
int flag = 0;
while(myNode!=NULL)
{
if(myNode->number==value)
{
if(previous==NULL)
head = myNode->next;
else
previous->next = myNode->next;
printf("%d is deleted from list\n", value);
flag = 1;
free(myNode); //need to free up the memory to prevent memory leak
break;
}
previous = myNode;
myNode = myNode->next;
}
if(flag==0)
printf("Key not found!\n");
}
void search_item(int value)
{
node *searchNode = head;
int flag = 0;
while(searchNode!=NULL)
{
if(searchNode->number==value)
{
printf("%d is present in this list. Memory address is %d\n", value,
searchNode);
flag = 1;
break;
}
else
searchNode = searchNode->next;
}
if(flag==0)
printf("Item not found\n");
void print_linked_list()
{
printf("\nYour full linked list is\n");
node *myList;
myList = head;
while(myList!=NULL)
{
printf("%d ", myList->number);
myList = myList->next;
}
puts("");
}
// C program to implement READ A FILE AND PRINT THE DATA
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Driver code
int main()
{
FILE* ptr;
char ch;
// Opening file in reading mode
ptr = fopen("test.txt", "r");
if (NULL == ptr) {
printf("file can't be opened \n");
}
printf("content of this file are \n");
// Printing what is written in file
// character by character using loop.
do {
ch = fgetc(ptr);
printf("%c", ch);
// Checking if character is not EOF.
// If it is EOF stop reading.
} while (ch != EOF);
// Closing the file
fclose(ptr);
return 0;
}
a simple C program to copy the content of one file to another file.
Aim:
To write a C program to copy the contents of one file to another file.
Algorithm:
1. Start
2. Declare file pointers and variables:
o FILE *sourceFile, *destFile
o char sourcePath[100], destPath[100], ch
3. Get the source file path from the user:
o Print: "Enter source file path:"
o Read the source file path into sourcePath
4. Get the destination file path from the user:
o Print: "Enter destination file path:"
o Read the destination file path into destPath
5. Open the source file in read mode:
o sourceFile = fopen(sourcePath, "r")
o If sourceFile is NULL, print an error message and exit
6. Open the destination file in write mode:
o destFile = fopen(destPath, "w")
o If destFile is NULL, print an error message, close the sourceFile, and exit
7. Copy contents from the source file to the destination file:
o Use a while loop to read characters from sourceFile until EOF:
ch = fgetc(sourceFile)
fputc(ch, destFile)
8. Print a success message:
o Print: "File copied successfully."
9. Close both files:
o fclose(sourceFile)
o fclose(destFile)
10. End
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *sourceFile, *destFile;
char sourcePath[100], destPath[100];
char ch;
// Get the source file path
printf("Enter source file path: ");
scanf("%s", sourcePath);
// Get the destination file path
printf("Enter destination file path: ");
scanf("%s", destPath);
// Open the source file in read mode
sourceFile = fopen(sourcePath, "r");
if (sourceFile == NULL) {
printf("Unable to open source file.\n");
exit(EXIT_FAILURE);
}
// Open the destination file in write mode
destFile = fopen(destPath, "w");
if (destFile == NULL) {
printf("Unable to open destination file.\n");
fclose(sourceFile);
exit(EXIT_FAILURE);
}
// Copy contents from source file to destination file
while ((ch = fgetc(sourceFile)) != EOF) {
fputc(ch, destFile);
}
printf("File copied successfully.\n");
// Close the files
fclose(sourceFile);
fclose(destFile);
return 0;
}