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

CS3271 NEW C Programming Lab Manual

Uploaded by

Susbehe
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)
145 views

CS3271 NEW C Programming Lab Manual

Uploaded by

Susbehe
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/ 40

EX.

NO:01 Programs using I/O Statements and Expressions


DATE:

Aim:
To write C programs using I/O statements and expressions.

Algorithm:
Step 1: Start the program
Step 2: Declare n variables.
Step 3: Read the value of n variables.
Step 4: Print the value of n variables.
Step 5: Stop the program

Programs:

Unformatted I/O functions

/*Program illustrate the use of getchar() and putchar() functions*/


#include<stdio.h>
main()
{
char n;
n = getchar();
putchar(n);
}

/*Program to illustrate the concept of puts() with gets() functions*/


#include <stdio.h>
main()
{
char name[20];
puts("Enter the Name");
gets(name);
puts("Name is :");
puts(name);
}

/*Program to explain the use of getch() function*/


#include <stdio.h>
main()
{
char n;
puts("Enter the Char");
n = getch();
puts("Char is :");
putchar(n);
getch();
}

Formatted I/O functions

/*Program to illustrate the use of formatted code by using the formatted scanf() function */
#include<stdio.h>
main()
{
char n,name[20];
int abc;
float xyz;
printf("Enter the single character, name, integer data and real value");
scanf("\n%c%s%d%f", &n,name,&abc,&xyz);
printf("\nChar : %c \tName: %s \tInteger data: %d \tReal value %f",n,name,abc,xyz);
getch();
}
Expressions

Example of evaluation statements are

x = a–b/3+c *2–1
y = a–b/(3+c)*(2–1)
z = a–(b/(3+c)*2) –1;

The following program illustrates the effect of presence of parenthesis in expressions.

#inlcude<stdio.h>
main ()
{
float a, b, c x, y, z;
a = 9;
b = 12;
c = 3;
x = a – b / 3 + c * 2 – 1;
y = a – b / (3 + c) * (2 – 1);
z = a – ( b / (3 + c) * 2) – 1;
printf ("x = %f",x);
printf ("y = %f",y);
printf ("z = %f",z);
}
EX.NO:02 Programs using Decision-Making Constructs
DATE:

Aim:
To write C programs using decision-making constructs.

Algorithm:
Step 1: Start the program
Step 2: Declare n variables.
Step 3: Read the value of n variables.
Step 4: Check whether the condition is true or false.
Step 5: If the condition is true it executes the statement, else it goes to the next condition or terminates.
Step 6: Stop the program

Program:
//Simple if statement
#include <stdio.h>
void main( )
{
int x, y;
x = 15;
y = 13;
if (x > y)
{
printf("X is greater than Y");
}
}

Program:
//if...else statement
#include <stdio.h>
void main( )
{
int x, y;
x = 15;
y = 18;
if (x > y )
{
printf("x is greater than y");
}
else
{
printf("y is greater than x");
}
}
Program:
//Nested if... else statement
#include <stdio.h>
void main( )
{
int a, b, c;
printf("Enter 3 numbers...");
scanf("%d%d%d",&a, &b, &c);
if(a > b)
{
if(a > c)
{
printf("a is the greatest");
}
else
{
printf("c is the greatest");
}
}
else
{
if(b > c)
{
printf("b is the greatest");
}
else
{
printf("c is the greatest");
}
}
}

Program:
//else if ladder
#include <stdio.h>
void main( )
{
int a;
printf("Enter a number...");
scanf("%d", &a);
if(a%5 == 0 && a%8 == 0)
{
printf("Divisible by both 5 and 8");
}
else if(a%8 == 0)
{
printf("Divisible by 8");
}
else if(a%5 == 0)
{
printf("Divisible by 5");
}
else
{
printf("Divisible by none");
}
}
EX.NO:03a Write a program to find whether the given year is leap year or not
DATE:

Aim:
To write a C program takes a year as input and finds whether a year is leap year or not.

Algorithm:
Step 1: Start the program.
Step 2: Take a year as input.
Step 3: Check whether a given year is divisible by 400.
Step 4: Check whether a given year is divisible by 100.
Step 5: Check whether a given year is divisible by 4.
Step 6: If the condition at step 2 and 4 becomes true, then the year is a leap year.
Step 7: If the condition at step 3 becomes true, then the year is not a leap year.
Step 8: Stop the program.

Program:
void main()
{
int year;
printf("Enter a year \n");
scanf("%d", &year);
if ((year % 400) == 0)
printf("%d is a leap year \n", year);
else if ((year % 100) == 0)
printf("%d is a not leap year \n", year);
else if ((year % 4) == 0)
printf("%d is a leap year \n", year);
else
printf("%d is not a leap year \n", year);
}
EX.NO:03b Design a calculator to perform the operations, namely, addition, subtraction,
DATE: multiplication, division and square of a number.

Aim:
To write a C program to design a calculator to perform the operations, namely, addition, subtraction,
multiplication, division and square of a number.

Algorithm:
Step 1: Start the program
Step 2: Declare the variables.
Step 3: Read the values of two variables.
Step 4: Display the menu
Step 5: Read the user choice
Step 6: If the choice is +, then adds two numbers, and prints the result.
Step 7: If the choice is -, then subtracts two numbers, and prints the result.
Step 8: If the choice is *, then multiply two numbers, and prints the result.
Step 9: If the choice is /, then divides two numbers, and prints the result.
Step 10: If the choice is s, then square of given number, and print the result.
Step 11: If you want to continue the choice press y else press n.
Step 12: Stop the program

Program:
// Design a Calculator to perform Addition, Subtraction, Multiplication, Division and Square of a number
#include <stdio.h>
#include <conio.h>
void main()
{
int fn,sn;
char c,c1;
float r=0;
clrscr();
printf("Enter the First Number : ");
scanf("%d",&fn);
printf("\nEnter the Second Number : ");
scanf("%d",&sn);
do
{
printf("\nWhich operations you want to perform (+,-,*,/,s)\n");
scanf("%s",&c);
switch(c)
{
case '+':
r = fn+sn;
break;
case '-':
r = fn-sn;
break;
case '*':
r = fn*sn;
break;
case '/':
r = fn/(float)sn;
break;
case 's':
r = fn*fn;
break;
} // switch
printf("\nResult = %f\n",r);
printf("\ndo you want continue (y/n) :");
scanf("%s",&c1);
} while (c1=='y' || c1=='Y');
} // main
EX.NO:03c Check whether a given number is Armstrong number or not
DATE:

Aim:
To write a C program to check whether a given number is Armstrong number or not.

Algorithm:
Step 1: Start the program
Step 2: Declare the variables.
Step 3: Read n value of variables.
Step 4: Store n into another temporary variable tn
Step 5: If tn!=0 then calculate
r=n%10
sum=sum+r*r*r
tn=tn/10
Step 6: Check whether sum equal to n or not. If both numbers are equal, then print number is Armstrong
number.
Step 7: Otherwise print number is not Armstrong number.
Step 8: Stop the program

Program:
/*Armstrong Number - An Armstrong Number is a Number which is equal to it’s sum of digit’s cube. For
example - 153 is an Armstrong number: here 153 = (1*1*1) + (5*5*5) + (3*3*3).*/
#include <stdio.h>
int main()
{
int number, sum=0, rem=0,tempNumber;
printf("Enter an integer number: ");
scanf("%d", &number);
tempNumber=number;
while(tempNumber!=0)
{
rem=tempNumber%10;
sum=sum + (rem*rem*rem);
tempNumber= tempNumber /10;
}
/* checking number is armstrong or not */
if(sum==number)
printf("%d is an Armstrong number.",number);
else
printf("%d is not an Armstrong number.",number);
return 0;
}

Output:
First run:
Enter an integer number: 153
153 is an Armstrong number.
Second run:
Enter an integer number: 167 167
is not an Armstrong number.
EX.NO:04 Given a set of numbers like <10, 36, 54, 89, 12, 27>, find sum of weights based on
DATE: the following conditions
 5 if it is a perfect cube
 4 if it is a multiple of 4 and divisible by 6
 3 if it is a prime number 27

Aim:
To write a C program to find sum of weights based on the following conditions
• 5 if it is a perfect cube
• 4 if it is a multiple of 4 and divisible by 6
• 3 if it is a prime number 27
Algorithm:
Step 1: Start the program.
Step 2: Declare n array variables.
Step 3: Read n number of array elements.
Step 4: Sorting the elements.
Step 5: Check whether the number is perfect cube or not. If it is perfect cube then add sum=sum+5
Step 6: Check whether the number is multiple of 4 and divisible by 6 or not. If the conditions is true then add
sum=sum+4
Step 7: Check whether the number is prime or not. If it is prime number then add sum=sum+3
Step 8: Sort and print the array.
Step 9: Stop the program

Program:
#include <stdio.h>
#include <math.h>
void main()
{
int nArray[50],wArray[50],nelem,sq,i,j,t;
clrscr();
printf("\nEnter the number of elements in an array : ");
scanf("%d",&nelem);
printf("\nEnter %d elements\n",nelem);
for(i=0;i<nelem;i++)
scanf("%d",&nArray[i]);
// Sorting an array
for(i=0;i<nelem;i++)
for(j=i+1;j<nelem;j++)
if(nArray[i] > nArray[j])
{
t = nArray[i];
nArray[i] = nArray[j];
nArray[j] = t;
}
//Calculate the weight
for(i=0; i<nelem; i++)
{
wArray[i] = 0;
// sq =(int) sqrt(nArray[i]);
if(percube(nArray[i]))
wArray[i] = wArray[i] + 5;
if(nArray[i]%4==0 && nArray[i]%6==0)
wArray[i] = wArray[i] + 4;

if(prime(nArray[i]))
wArray[i] = wArray[i] + 3;

}
for(i=0; i<nelem; i++)
printf("<%d,%d>", nArray[i],wArray[i]);
getch();

int prime(int num)


{
int flag=1,i;
for(i=2;i<=num/2;i++)
if(num%i==0)
{
flag=0;
break;
}
return flag;
}
int percube(int num)
{
int i,flag=0;
for(i=2;i<=num/2;i++)
if((i*i*i)==num)
{
flag=1;
break;
}
return flag;
}
EX.NO:05a Populate an array with height of persons and find how
DATE: many persons are above the average height.

Aim:
To write a C Program to accept the height of persons and find how many persons are above the average
Height using array.

Algorithm:
Step 1: Start the program.
Step 1: Take the height of a person as input and store it in the variable height.
Step 2: If the variable height is greater than or equal to 100 cm and lesser than or equal to 150 cm, then print
the output as “Dwarf”.
Step 3: If the variable height is greater than or equal to 151 cm and lesser than or equal to 165 cm, then print
the output as “Average Height”.
Step 4: If the variable height is greater than or equal to 166 cm and lesser than or equal to 195 cm, then print
the output as “Taller”.
Step 5: If the variable height is greater than or equal to 196 cm and lesser than or equal to 220 cm, then print
the output as “Above average height”.
Step 6: Else print the output as “Height is abnormal” and break the condition.
Step 7: Stop the program.

Program:
/* C program to accept the height of a person in centimeter and categorize the person based on height as taller,
dwarf , average and above average height person */

#include <stdio.h>
void main()
{
float height[10];
int i,n,dwarf=0,average=0,taller=0,aboveaverage=0;
printf("Enter the number of persons:\n");
scanf("%d",&n);
printf("Enter the height of the persons(in centimetres):\n");
for(i=1;i<=n;i++)
{
printf("Person %d:",i);
scanf("%f",&height[i]);
if ((height[i]>=100) && (height[i]<=150))
dwarf++;
else if ((height[i]>=151) && (height[i]<=165))
average++;
else if ((height[i]>=166) && (height[i]<=195))
taller++;
else if(height[i]>=196 && (height[i]<=220))
aboveaverage++;
else
{
printf("Height is Abnormal\n");
break;
}
}
printf("Dwarf:%d\nAverage Height:%d\nTaller:%d\nAbove average Height:%d", dwarf, average, taller,
aboveaverage);
}
EX.NO:05b Populate a two dimensional array with height and weight of persons and
DATE: compute the Body Mass Index of the individuals.

Aim:
To write a C program to compute the Body Mass Index of the individuals using two dimensional array
with height and weight of persons.

Algorithm:
Step 1: Start the program
Step 2: Read n.
Step 3: Read weight and height of n persons.
Step 4: Calculate bmi=weight/((height/100)*(height/100))
Step 5: Print the result of bmi
Step 6: Stop the program

Program:
#include<stdio.h>
void main()
{

int i,j,n;
float height,weight,bmi[5][5];
printf("Enter the number of persons:\n");
scanf("%d",&n);
printf("\nEnter the height in meters and weight in kilograms of the persons");
for(i=1;i<=n;i++)
{
bmi[i][0]=i;
for(j=1;j<2;j++)
{
printf("\nPerson %d:",i);
printf("\nHeight: ");
scanf("%f",&height);
printf("Weight: ");
scanf("%f",&weight);
bmi[i][j]=weight/(height*height);
}
}

printf("\nPersons and BMI\n");


for(i=1;i<=n;i++)
{
printf("\nPerson : %d",i);
for(j=1;j<2;j++)
{
printf("\nThe BMI is %f", bmi[i][j]);
//# Conditions to find out BMI category
if (bmi[i][j] < 18.5)
printf("\nunderweight");

else if (( bmi[i][j] >= 18.5) && (bmi[i][j] < 24.9))


printf("\nHealthy");

else if (( bmi[i][j] >= 24.9) && (bmi[i][j] < 30))


printf("\noverweight");

else if ( bmi[i][j] >=30)


printf("\nSuffering from Obesity");
}
}
}
EX.NO:06 Given a string ―a$bcd./fg‖ find its reverse without changing the position of
DATE: special characters

Aim:
To write a C program to find its a$bcd./fg‖ reverse without changing the position of special characters.

Algorithm:
Step 1: Start the program
Step 2: Read the string
Step 3: Calculate the string length using strlen.
Step 4: Traverse the string from both ends and check it is special character or not.
Step 5: If it is special character just move to next character.
Step 6: If it is not special character swap both characters.
Step 7: Repeat steps 4 to 6. Until traverse all the characters.
Step 8: Print reversed string.
Step 9: Stop the program

Program:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void swap(char *a, char *b)
{
char temp = *a;
*a = *b;
*b = temp;
}
void reverse(char *str)
{
char *p1 = str;
char *p2 = str + strlen(str) - 1;
while (p1 < p2) {
while (!isalpha(*p1)) p1++;
while (!isalpha(*p2)) p2--;
if (p1 < p2) {
swap(p1, p2);
p1++;
p2--;
} } }
int main(void)
{
char str[6];
strcpy(str, "a,b$c");
reverse2(str);
printf("%s\n", str);
return 0;
}
EX.NO:7 Convert the given decimal number into binary, octal and
DATE: hexadecimal numbers using user defined functions

Aim:
To write a C program to convert the given decimal number into binary, octal and hexadecimal numbers
using user defined functions.

Algorithm:
Step 1: Start the program
Step 2: Read n.
Step 3: To convert n to any other base divide n to the base to be converted.
Step 4: Get the remainder and store it in the array.
Step 5: Use the quotient as new n and repeat steps 3 and 4. Until n becomes 0.
Step 6: Print the remainder array in reverse order
Step 7: Stop the program

Program:
#include<stdio.h>
FILE *FP=NULL;
void main()
{
int CH,NUM;
void octal(int);
void binary(int);
void hexa(int);
clrscr();
printf("ENTER DECIMAL NUMBER TO BE CONVERTED : \n");
scanf("%d",&NUM);
printf("\nSELECT CONVERSION");
printf("\n 1. DECIMAL TO BINARY\n");
printf("\n 2. DECIMAL TO OCTAL\n");
printf("\n 3. DECIMAL TO HEXADECIMAL\n");
printf("\nENTER CHOICE HERE :");
scanf("%d",&CH);
switch(CH)
{
case 1 : binary(NUM);
printf("\nOUPUT WRITTEN TO OUTPUT.TXT");
break;
case 2 : octal(NUM);
printf("\nOUPUT WRITTEN TO OUTPUT.TXT");
break;
case 3 : hexa(NUM);
printf("\nOUPUT WRITTEN TO OUTPUT.TXT");
break;
default : printf("\nYOU HAVE ENTERED WRONG CHOICE !!!");
}

getch();
}
void hexa(int Y)
{
char HEXC[5];
int NUM,I,LEN,HEXD[5];
NUM=Y;
LEN=0;
while(Y>0)
{
HEXD[LEN]=Y%16;
Y=Y/16;
LEN++;
};
for(I=LEN-1;I>-1;I--)
{
if(HEXD[I]<10)
HEXC[I]=HEXD[I]+48;
else
HEXC[I]=HEXD[I]+55;
}
if((FP=fopen("output.txt","a+t"))==NULL)
{
printf("FILE CAN'T BE OPENED OR CREATED\n");
exit(0);
}
fprintf(FP,"\nCONVERTED BINARY EQUIVALENT VALUE OF %d IS \t",NUM);
for(I=LEN-1;I>-1;I--)
{
fprintf(FP,"%c",HEXC[I]);
}

}
void binary(int Y)
{
int NUM,I,LEN,BIN[20];
NUM=Y;
LEN=0;
while(Y>0)
{
BIN[LEN]=Y%2;
Y=Y/2;
LEN++;
};
if((FP=fopen("output.txt","a+t"))==NULL)
{
printf("FILE CAN'T BE OPENED OR CREATED\n");
exit(0);
}
fprintf(FP,"\nCONVERTED BINARY EQUIVALENT VALUE OF %d IS \t",NUM);
for(I=LEN-1;I>-1;I--)
{
fprintf(FP,"%d",BIN[I]);
}
}
void octal(int Y)
{
int NUM,I,LEN,OCT[5];
NUM=Y;
LEN=0;
while(Y>0)
{
OCT[LEN]=Y%8;
Y=Y/8;
LEN++;
};
if((FP=fopen("output.txt","a+t"))==NULL)
{
printf("FILE CAN'T BE OPENED OR CREATED\n");
exit(0);
}
fprintf(FP,"\nCONVERTED OCTAL EQUIVALENT VALUE OF %d IS \t",NUM);
for(I=LEN-1;I>-1;I--)
{
fprintf(FP,"%d",OCT[I]);
}
}

Output:
\
EX.NO:8a From a given paragraph perform the following using built-in functions:
DATE: a. Find the total number of words.

Aim:
To write a C program to find the total number of words using built-in functions.

Algorithm:
Step 1: Start the program
Step 2: Read the paragraph.
Step 3: Read one character at a time if any blank space then increment the word count variable by 1.
Step 4: Print count
Step 5: Stop the program

Program:
/* C/C++ program to count no of words from given input string. */
#include <stdio.h>
#define OUT 0
#define IN 1
// returns number of words in str
unsigned countWords(char *str)
{
int state = OUT;
unsigned wc = 0; // word count
// Scan all characters one by one
while (*str)
{
// If next character is a separator, set the
// state as OUT
if (*str == ' ' || *str == '\n' || *str == '\t')
state = OUT;

// If next character is not a word separator and


// state is OUT, then set the state as IN and
// increment word count
else if (state == OUT)
{
state = IN;
++wc;
}
// Move to next character
++str;
} return wc; }
// Driver program to tes above functions
int main(void)
{
char str[] = "One two three\n four\tfive ";
printf("No of words : %u", countWords(str));
return 0; }

Output:
EX.NO:8b From a given paragraph perform the following using built-in functions:
DATE: b. Capitalize the first word of each sentence.

Aim:
To write a C program to capitalize the first word of each sentence using built-in functions.

Algorithm:
Step 1: Start the program
Step 2: Read the paragraph.
Step 3: Capitalize the first character of the paragraph using toupper buitin function.
Step 4: Read one character at a time,and check if it is a full stop(.) then capitalize next character.
Step 5: If any upper case in between the sentence change it to lowercase letter.
Step 6: Print the paragraph.
Step 7: Stop the program

Program:
#include <stdio.h>
#define MAX 100

int main()
{
char str[MAX]={0};
int i;

//input string
printf("Enter a string: ");
scanf("%[^\n]s",str); //read string with spaces

//capitalize first character of words


for(i=0; str[i]!='\0'; i++)
{
//check first character is lowercase alphabet
if(i==0)
{
if((str[i]>='a' && str[i]<='z'))
str[i]=str[i]-32; //subtract 32 to make it capital
continue; //continue to the loop
}
if(str[i]==' ')//check space
{
//if space is found, check next character
++i;
//check next character is lowercase alphabet
if(str[i]>='a' && str[i]<='z')
{
str[i]=str[i]-32; //subtract 32 to make it capital
continue; //continue to the loop
}
}
else
{
//all other uppercase characters should be in lowercase
if(str[i]>='A' && str[i]<='Z')
str[i]=str[i]+32; //subtract 32 to make it small/lowercase
}
}

printf("Capitalize string is: %s\n",str);

return 0;
}

Output:
EX.NO:8c From a given paragraph perform the following using built-in functions:
DATE: c. Replace a given word with another word.

Aim:
To write a C program to replace a given word with another word.

Algorithm:
Step 1: Start the program
Step 2: Read a sentence.
Step 3: Read search and replace strings.
Step 4: Write the user defined function to replace the search with the replace string.
Step 5: Recursively call using the function until there is no occurrence of the search string.
Step 6: Stop the program

Program:
// C program to search and replace
// all occurrences of a word with
// other word.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

// Function to replace a string with another


// string
char *replaceWord(const char *s, const char *oldW,
const char *newW)
{
char *result;
int i, cnt = 0;
int newWlen = strlen(newW);
int oldWlen = strlen(oldW);

// Counting the number of times old word


// occur in the string
for (i = 0; s[i] != '\0'; i++)
{
if (strstr(&s[i], oldW) == &s[i])
{
cnt++;

// Jumping to index after the old word.


i += oldWlen - 1;
}
}

// Making new string of enough length


result = (char *)malloc(i + cnt * (newWlen - oldWlen) + 1);

i = 0;
while (*s)
{
// compare the substring with the result
if (strstr(s, oldW) == s)
{
strcpy(&result[i], newW);
i += newWlen;
s += oldWlen;
}
else
result[i++] = *s++;
}

result[i] = '\0';
return result;
}

// Driver Program
int main()
{
char str[] = "xxforxx";
char c[] = "xx";
char d[] = "Geeks";

char *result = NULL;

// oldW string
printf("Old string: %sn", str);

result = replaceWord(str, c, d);


printf("New String: %sn", result);

free(result);
return 0;
}

Output:
EX.NO:9 Solve towers of Hanoi using recursion
DATE:

Aim:
To write a C program to solve towers of Hanoi problem using recursion.

Algorithm:
Step 1: Start the program
Step 2: Read number of disks as num.
Step 3: if num==1 move the disk from frompeg to topeg..
Step 4: Else repeatedly movedisk(num-1,frompeg,auxpeg,topeg)
Step 5: Print the sequence of moves.
Step 6: Stop the program

Program:
/* C program for Tower of Hanoi using Recursion */
#include <stdio.h>
void towers(int, char, char, char);
int main()
{
int num;
printf("Enter the number of disks : ");
scanf("%d", &num);
printf("The sequence of moves involved in the Tower of Hanoi are :\n");
towers(num, 'A', 'C', 'B');
return 0;
}

void towers(int num, char frompage, char topage, char auxpage)


{
if (num == 1)
{
printf("\n Move disk 1 from page %c to page %c", frompage, topage);
return;
}

towers(num - 1, frompage, auxpage, topage);


printf("\n Move disk %d from page %c to page %c", num, frompage, topage);
towers(num - 1, auxpage, topage, frompage);
}

Output:
EX.NO:10 Sort the list of numbers using pass by reference
DATE:

Aim:
To write a C program to sort the list of numbers using pass by reference.

Algorithm:
Step 1: Start the program
Step 2: Read n.
Step 3: Read n numbers
Step 4: Call the function sort by passing the address of two numbers to swap.
Step 5: Repeat the step 4 untill all numbers are sorted.
Step 6: Print the sorted array
Step 7: Stop the program

Program:
#include<stdio.h>
void swap(int array1[], int array2[])
{
int temp, ai, aj;
ai = *array1;
aj = *array2;
if (ai > aj) /* For decreasing order use < */
{
temp = aj;
aj = ai;
ai = temp;
}
}

int main()
{
int array[100], n, c, d, swap;

printf("Enter number of elements\n");


scanf("%d", &n);

printf("Enter %d integers\n", n);

for (c = 0; c < n; c++)


scanf("%d", &array[c]);

for (c = 0; c < (n - 1); c++)


{
for (d = 0; d < n - c - 1; d++)
{
swap(array[c], array[d]);
}
}
printf("Sorted list in ascending order:\n");

for (c = 0; c < n; c++)


printf("%d\n", array[c]);

return 0;
}
Output:
EX.NO:11 Generate salary slip of employees using structures and pointers
DATE:

Aim:
To write a C program for generating salary slip of employees using structures and pointers.

Algorithm:
Step 1: Start the program
Step 2: Read the number of employees.
Step 3: Read employee details and their basic pay ,allowances and detections.
Step 4: Calculate the net pay.
Step 5: Print all details
Step 6: Stop the program

Program:
#include<stdio.h>
#include<dos.h>
struct employee
{
int NO;
char NAME[10];
int DESIGN_CODE;
int DAYS_WORKED;
}EMP[12]={
{1,"GANESH",1,25},
{2,"MAHESH",1,30},
{3,"SURESH",2,28},
{4,"KALPESH",2,26},
{5,"RAHUL",2,24},
{6,"SUBBU",2,25},
{7,"RAKESH",2,23},
{8,"ATUL",2,22},
{9,"DHARMESH",3,26},
{10,"AJAY",3,26},
{11,"ABDUL",3,27},
{12,"RASHMI",4,29}
};
void main()
{

int EMPNO;
void gen_payslip(int);
clrscr();

printf("ENTER THE EMPLOYEE NO TO GENERATE PAYSLIP : ");


scanf("%d",&EMPNO);
if(EMPNO>0 && EMPNO<13)
gen_payslip(EMPNO);
else
printf("\nYOU HAVE ENTERED WRONG EMP NO. !!");
getch();
}
void gen_payslip(int EMPNO)
{
struct date D;
char DESG[10];
float NETPAY,BASIC,PF,PAYRATE,PTAX=200;
getdate(&D);
printf("\n\n\t\t\tSHREE KRISHNA CHEMISTS AND DRUGGIST");
printf("\n\t\t\t\tSALARY MONTH %d %d\n",D.da_mon,D.da_year);
printf("\n\n\tEMP.NO.: %d\t\tEMP.NAME: %s",EMPNO,EMP[EMPNO-1].NAME);
switch(EMP[EMPNO-1].DESIGN_CODE)
{
case 1: PAYRATE=400;
printf("\tDESIGNATION: CLERK");
break;
case 2: PAYRATE=300;
printf("\tDESIGNATION: SALESMEN");
break;
case 3: PAYRATE=250;
printf("\tDESIGNATION: HELPER");
break;
case 4: PAYRATE=350;
printf("\tDESIGNATION: COMP.OPTR");
break;
}
BASIC=PAYRATE*EMP[EMPNO-1].DAYS_WORKED;
PF=BASIC/10;
printf("\n\n\tDAYS WORKED: %d",EMP[EMPNO-1].DAYS_WORKED);
printf("\t\tPAY RATE: %.0f\t\tGEN.DATE:%d/%d/%d ",PAYRATE,D.da_day,D.da_mon,D.da_year);
printf("\n\t ");
printf("\n\n\tEARNINGS\tAMOUNT(RS.)\t\tDEDUCTIONS\tAMOUNT(RS.)");
printf("\n\t ");
printf("\n\n\tBASIC PAY\t%.0f\t\t\tP.F.\t\t%.0f",BASIC,PF);
printf("\n\n\t\t\t\t\t\tPROF.TAX\t%.0f",PTAX);
printf("\n\n\t ");
printf("\n\n\tGROSS EARN.\t%.0f\t\t\tTOTAL DEDUCT.\t%.0f",BASIC,PF+PTAX);
NETPAY=BASIC-(PF+PTAX);
printf("\n\t\t\t\t\t\tNET PAY\t\t%.0f",NETPAY);
printf("\n\t "); }

Output:
EX.NO:12 Compute internal marks of students for five different subjects
DATE: using structures and functions

Aim:
To write a C program to compute internal marks of students for five different subjects using structures
and functions.

Algorithm:
Step 1: Start the program
Step 2: Read the number of student.
Step 3: For every student get attendance percentage.
Step 4: Calculate attendance mark based on the conditions.
Step 5: Read 3 internal assessment marks for every subject.
Step 6: Calculate the internal marks out of 15
Step 7: Add the attendance mark and total mark to find internal mark.
Step 8: Print the internal mark.
Step 9: Stop the program

Program:
#include<stdio.h>
#include<conio.h>
struct stud
{
int rollno, s1, s2, s3, s4, s5, tot ;
char name[10] ;
float avg ;
} s[10] ;
void main()
{
int i, n ;
clrscr() ;
printf("Enter the number of students : ") ;
scanf("%d", &n) ;
for(i = 0 ; i < n ; i++)
{
printf("\nEnter the roll number : ") ;
scanf("%d", &s[i].rollno) ;
printf("\nEnter the name : ") ;
scanf("%s", s[i].name) ;
printf("\nEnter the marks in 5 subjects : ") ;
scanf("%d %d %d %d %d", &s[i].s1, &s[i].s2, &s[i].s3, &s[i].s4, &s[i].s5) ;
s[i].tot = s[i].s1 + s[i].s2 + s[i].s3 + s[i].s4 + s[i].s5 ;
s[i].avg = s[i].tot / 2.0 ;
}
printf("\nRoll No. Name \t\tSub1\t Sub2\t Sub3\t Sub4 \t Sub5\t Total\t Average\n\n") ;
for(i = 0 ; i < n ; i++)
{
printf("%d \t %s \t\t %d \t %d \t %d \t %d \t %d \t %d \t %.2f \n", s[i].rollno,s[i].name,s[i].s1,s[i].s2,
s[i].s3, s[i].s4, s[i].s5,s[i].tot,s[i].avg);
}
getch() ; }
Output:
EX.NO:13 Insert, update, delete and append telephone details of an individual or a company
DATE: into a telephone directory using random access file.

Aim:
To write a C program to insert, update, delete and append telephone details of an individual or a
company into a telephone directory using random access file.

Algorithm:
Step 1: Start the program
Step 2: Create a structure for telephone directory.
Step 3: To insert a record in a random access file create a file pointer and get necessary details from user.
Step 4: To update the record in a random access file create a file pointer and get necessary details from the user.
Step 5: To delete a record get the record number from user.
Step 6: Stop the program

Program:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define MAX 50
struct Phone_record
// declare all variables
{
char fname[MAX];
char lname[MAX];
char category[MAX];
char phonenumber[MAX];
int count;
};

struct Phone_list
{
int count; // keeps rack of number of ppl in list
struct Phone_record pr[MAX];
};

void displaymenu(void);
void choices(void);
void add_record(struct Phone_record *);
void del_record(struct Phone_list *);
void save_record(struct Phone_record *);
void load_record(struct Phone_record *);
void print_record(const struct Phone_record *);

void displaymenu()
{

printf( "A-Add a Phone Number\n"


"D-Delete a Phone Number\n"
"S-Save the phone book\n"
"L-Load the phone book\n"
"P-Print the Phone List\n"
"Q-Quit the program\n"
"H-Help Menu\n");
}

void choices(void)
{

struct Phone_record pst;


char pick='x';
pst.count=0;
// scanf("%c",&pick);
printf("\n\nPlease make your selection: \n\n");
while (pick!='Q')
{
scanf("%c",&pick);
switch (pick)
{
case 'A':
add_record(&pst);
break;
case 'D':
//del_record();
break;
case'S':
save_record(&pst);
break;
case'L':
load_record(&pst);
break;
case 'P':
print_record(&pst);
break;
case 'H':
displaymenu();
break;
case 'Q':
break;
default:
printf("your choice was invalid\n\n");
break;
}
}
}

void add_record(struct Phone_record *pst)


{
printf("pleaes enter the last name\n");
scanf("%s",pst->lname);
printf("please enter the first name\n");
scanf("%s",pst->fname);
printf("plese enter a category\n");
scanf("%s",pst->category);
printf("plese enter your phone number\n");
scanf("%s",pst->phonenumber);
}

void del_record(struct Phone_list *book)


{

printf("deleting the record");

void save_record(struct Phone_record *pst)


{
// pfile=fopen("c:\\myfile.txt","w");
/*fprintf(file,"%d\n",(*book.count));
for i=0; i<(*book).count; i++);
{
fprintf("file,%s\n",(*book.Phone_list[i].name);
fprintf("file,%s\n",(*book.Phone_list[i].category);
fprintf("file,%s\n",(*book.Phone_list[i].phonenumber);

}
*/
}
void load_record(struct Phone_record *pst)
{
pfile=fopen("c:\\myfile.txt","r");
fscanf(pfile,"%d",&(pst.count);
for (i=0; i<pst.count; i++;)
{
fscanf(pfile,"%s %s",pst[i]->fname, pst[i]->lname);
fscanf(pfile,"%s",pst[i]->category);
fscanf(pfile,"%s",pst[i]->phonenumber);
}
}
void print_record(const struct Phone_record *pst)
{
printf(" your name is %s %s\n",pst->fname, pst->lname);
printf("your category is %s\n",pst->category);
printf(" your phone number is %s\n",pst->phonenumber);
choices();
}

int main (int argc, char*argv[1])


{
FILE *pfile=NULL; // File Pointer
struct Phone_record;
int i;
pfile=fopen("C:\\myfile.txt","w");
printf("Welcome to the Phone Book");
printf("\n\n");
displaymenu();
choices();
fclose(pfile);
return 0;
}

Output:
EX.NO:14 Count the number of account holders whose balance is less than the minimum balance
DATE: using sequential access file.

Aim:
To write a C program to count the number of account holders whose balance is less than the minimum
balance using sequential access file.

Algorithm:
Step 1: Start the program
Step 2: Read the number of records.
Step 3: Read information for account holders..
Step 4: Store it in the file.
Step 5: Display all the details one by one.
Step 6: Read the minimum balance value.
Step 7: If balance<mini balance then increment the count value.
Step 8: Print the count value.
Step 9: Stop the program.

Program:
#include<stdio.h>
#include<conio.h>
void creation();
void deposit();
void withdraw();
void bal();
int a=0,i = 101;
struct bank
{
int no;
char name[20];
float bal;
float dep;
}s[20];
void main()
{
int ch;
while(1)
{
clrscr();
printf("\n*********************");
printf("\n BANKING ");
printf("\n*********************");
printf("\n1-Creation");
printf("\n2-Deposit");
printf("\n3-Withdraw");
printf("\n4-Balance Enquiry");
printf("\n5-Exit");
printf("\nEnter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1: creation();
break;
case 2: deposit();
break;
case 3: withdraw();
break;
case 4: bal();
break;
case 5: exit(0);
defalut: printf("Enter 1-5 only");
getch();
}
}
}
void creation()
{

printf("\n*************************************");
printf("\n ACCOUNT CREATION ");
printf("\n*************************************");
printf("\nYour Account Number is :%d",i);
s[a].no = i;
printf("\nEnter your Name:");
scanf("%s",s[a].name);
printf("\nYour Deposit is Minimum Rs.500");
s[a].dep=500;
a++;
i++;
getch();
}
void deposit()
{
int no,b=0,m=0;
float aa;
printf("\n*************************************");
printf("\n DEPOSIT ");
printf("\n*************************************");
printf("\nEnter your Account Number");
scanf("%d",&no);
for(b=0;b<i;b++)
{
if(s[b].no == no)
m = b;
}
if(s[m].no == no)
{
printf("\n Account Number : %d",s[m].no);
printf("\n Name : %s",s[m].name);
printf("\n Deposit : %f",s[m].dep);
printf("\n How Much Deposited Now:");
scanf("%f",&aa);
s[m].dep+=aa;
printf("\nDeposit Amount is :%f",s[m].dep);
getch();
}
else
{
printf("\nACCOUNT NUMBER IS INVALID");
getch();
}
}
void withdraw()
{

int no,b=0,m=0;
float aa;
printf("\n*************************************");
printf("\n WITHDRAW ");
printf("\n*************************************");
printf("\nEnter your Account Number");
scanf("%d",&no);
for(b=0;b<i;b++)
{
if(s[b].no == no)
m = b;
}
if(s[m].no == no)
{
printf("\n Account Number : %d",s[m].no);
printf("\n Name : %s",s[m].name);
printf("\n Deposit : %f",s[m].dep);
printf("\n How Much Withdraw Now:");
scanf("%f",&aa);
if(s[m].dep<aa+500)
{
printf("\nCANNOT WITHDRAW YOUR ACCOUNT HAS MINIMUM
BALANCE");
getch();
}
else
{
s[m].dep-=aa;
printf("\nThe Balance Amount is:%f",s[m].dep);
}
}
else
{

printf("INVALID");
getch();
}
getch();
}
void bal()
{
int no,b=0,m=0;
float aa;
printf("\n*************************************");
printf("\n BALANCE ENQUIRY ");
printf("\n*************************************");
printf("\nEnter your Account Number");
scanf("%d",&no);
for(b=0;b<i;b++)
{
if(s[b].no == no)
m = b;
}
if(s[m].no==no)
{
printf("\n Account Number : %d",s[m].no);
printf("\n Name : %s",s[m].name);
printf("\n Deposit : %f",s[m].dep);
getch();
}
else
{
printf("INVALID");
getch();
}
}

You might also like