50
CS8261 C PROGRAMMING LABORATORY L T P C 0 0 4 2 OBJECTIVES: To develop programs in C using basic constructs. To develop applications in C using strings, pointers, functions, structures. To develop applications in C using file processing. LIST OF EXPERIMENTS: 1..Programs using I/O statements and expressions. 2. Programs using decision-making constructs. 3. Write a program to find whether the given year is leap year or Not? (Hint: not every centurion year is a leap. For example 1700, 1800 and 1900 is not a leap year) 4. Design a calculator to perform the operations, namely, addition, subtraction, multiplication, division and square of a number. 5. Check whether a given number is Armstrong number or not? 6. Given a set of numbers like <10, 36, 54, 89, 12, 27>, 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. Sort the numbers based on the weight in the increasing order as shown below <10,its weight>,<36,its weight><89,its weight> 7. Populate an array with height of persons and find how many persons are above the average height. 8. Populate a two dimensional array with height and weight of persons and compute the Body Mass Index of the individuals. 9. Given a string ―a$bcd./fg‖ find its reverse without changing the position of special characters. (Example input:a@gh%;j and output:j@hg%;a) 10. Convert the given decimal number into binary, octal and hexadecimal numbers using user defined functions. 11. From a given paragraph perform the following using built-in functions:

tscse.files.wordpress.com€¦  · Web viewCS8261 C PROGRAMMING LABORATORY L T P C 0 0 4 2 . OBJECTIVES: To develop programs in C using basic constructs. To develop applications

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: tscse.files.wordpress.com€¦  · Web viewCS8261 C PROGRAMMING LABORATORY L T P C 0 0 4 2 . OBJECTIVES: To develop programs in C using basic constructs. To develop applications

CS8261 C PROGRAMMING LABORATORY L T P C 0 0 4 2 OBJECTIVES:

To develop programs in C using basic constructs.To develop applications in C using strings, pointers, functions, structures. To develop applications in C using file processing.

LIST OF EXPERIMENTS: 1..Programs using I/O statements and expressions.2. Programs using decision-making constructs.3. Write a program to find whether the given year is leap year or Not? (Hint: not every centurion year is a leap. For example 1700, 1800 and 1900 is not a leap year)4. Design a calculator to perform the operations, namely, addition, subtraction, multiplication, division and square of a number.5. Check whether a given number is Armstrong number or not? 6. Given a set of numbers like <10, 36, 54, 89, 12, 27>, 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.

Sort the numbers based on the weight in the increasing order as shown below <10,its weight>,<36,its weight><89,its weight>7. Populate an array with height of persons and find how many persons are above the average height. 8. Populate a two dimensional array with height and weight of persons and compute the Body Mass Index of the individuals. 9. Given a string ―a$bcd./fg‖ find its reverse without changing the position of special characters. (Example input:a@gh%;j and output:j@hg%;a) 10. Convert the given decimal number into binary, octal and hexadecimal numbers using user defined functions. 11. From a given paragraph perform the following using built-in functions:

a. Find the total number of words.b. Capitalize the first word of each sentence.c. Replace a given word with another word.

12. Solve towers of Hanoi using recursion.13. Sort the list of numbers using pass by reference.14. Generate salary slip of employees using structures and pointers.15. Compute internal marks of students for five different subjects using structures and functions.16. Insert, update, delete and append telephone details of an individual or a company into a telephone directory using random access file.17. Count the number of account holders whose balance is less than the minimum balance using sequential access file.

Mini project

Page 2: tscse.files.wordpress.com€¦  · Web viewCS8261 C PROGRAMMING LABORATORY L T P C 0 0 4 2 . OBJECTIVES: To develop programs in C using basic constructs. To develop applications

18. Create a ―Railway reservation system‖ with the following modules BookingAvailability checkingCancellationPrepare chart

TOTAL: 60 PERIODSOUTCOMES:Upon completion of the course, the students will be able to: Develop C programs for simple applications making use of basic constructs, arrays and strings. Develop C programs involving functions, recursion, pointers, and structures.Design applications using sequential and random access file processing.

Page 3: tscse.files.wordpress.com€¦  · Web viewCS8261 C PROGRAMMING LABORATORY L T P C 0 0 4 2 . OBJECTIVES: To develop programs in C using basic constructs. To develop applications

List of Experiments

Sl.No Ex.No. Date Title of the ExperimentsPage No.

Staff’s sign

1 1a Programs using I/O statements

2 1b Programs using Expressions

3 2a Find Odd or Even

4 2b Find the largest of three Numbers

5 3 Finding Leap Year or Not

6 4 Creating a Calculator

7 5 Finding Armstrong Number

8 6 Finding Sum Of Weights & Sorting The Elements Based On Weights

9 7 Finding The Persons Having Above Average Height

10 8 Finding Body Mass Index Using Array

11 9 Reverse String Without Changing The Position Of Special Character

12 10 Sorting The Values Using Pass By Reference

13 11 Number System Conversion Using User Defined Function

14 12 Finding Number Of Words And Capitalize the First Word

15 13 Employee Salary Slip

16 14 Students Internal Mark Sheet

17 15 Sequential Access File

18 16 Towers of Hanoi

19 17 Random Access File

Page 4: tscse.files.wordpress.com€¦  · Web viewCS8261 C PROGRAMMING LABORATORY L T P C 0 0 4 2 . OBJECTIVES: To develop programs in C using basic constructs. To develop applications

EX.NO.:1a PROGRAMS USING I/O STATEMENTS

AIM:To Write a C program to use Input and Output Statements

PROCEDURE:1. Start the program2. Use Unformulated input & output statements to get char or string

getchar(), getche() , getch() & gets()3. Print the char or string using unformulated output statements

putch(), putchar() & puts()4. Use formatted Input statements to read different types of data

scanf()5. Print different types of data using formatted output statements

printf()6. Stop the program

SYNTAX:Unformatted I/O◦ charvariable=getchar();◦ charvariable=getche();◦ charvariable=getch();◦ gets(chararray_variable);◦ putch(charvariable);◦ puts(chararray_variable);Formatted I/O◦ scanf(“format specifiers”,address of variables);◦ printf(“Any text”);◦ printf(“Any text,format spcifiers”,variables);◦ printf(“format spcifiers”,variables);

SAMPLE PROGRAMS:P1: Get & Display a single character using getch()

#include<stdio.h>#include<conio.h>main()

{char c1;c1=getch();

Page 5: tscse.files.wordpress.com€¦  · Web viewCS8261 C PROGRAMMING LABORATORY L T P C 0 0 4 2 . OBJECTIVES: To develop programs in C using basic constructs. To develop applications

putch(c1);}

P2: Get & Display a single character using getche()#include<stdio.h>#include<conio.h>main()

{char c1;}

P3: Get & Display a single character using getchar()#include<stdio.h>#include<conio.h>main()

{char c1;}

P4: Get & Display a More characters using gets()#include<stdio.h>#include<conio.h>main()

{char name[20];}

P5: Get & Display a student’s regno,Name and GPA using scanf & printf#include<stdio.h>#include<conio.h>main()

{int regno;char name[25];float GPA;printf(“\nEnter a Student Regno,Name & GPA\n”);scanf();prinntf(“\n------------------------------------------------------\n”);prinntf(“\n\t\t REG.NO \t\t NAME \t\t GPA \t\t\n”);prinntf(“\n------------------------------------------------------\n”);printf();}

OUTPUT:

Page 6: tscse.files.wordpress.com€¦  · Web viewCS8261 C PROGRAMMING LABORATORY L T P C 0 0 4 2 . OBJECTIVES: To develop programs in C using basic constructs. To develop applications

RESULT:Thus the program for I/O statements has been written & executed successfully.

EX.NO. :1b PROGRAMS USING EXPRESSIONS

AIM:To Write C programs using Expressions

PROCEDURE:1. Start the program2. Use Arithmetic operators (+,-,*,/,%)3. Use Increment & Decrement Operators (++,--)4. Use comparison operators (>,<,>=,<=,==,!=)5. Use Bit wise operators (~,^,|,&)6. Use logical operators (||,&&,!)7. Use Ternary Operator (?:)8. Stop the program

SYNTAX:Expression = Operand operator OperandOperator : SymbolOperand : variable or Constantvar1=Var2+Var3 (or) Var1=Value1+Value2; (or) Var1=Var2+valueLogical Operators : returns True(1) or false(0) values-Condition1&& Condition2Ternary Operator: if condition is true True part will be executed. Otherwise, False part

will be executed - Var1= ( condition)? True part: False Part

SAMPLE PROGRAMS:P1: Arithmetic Operators

#include<stdio.h>#include<conio.h>main()

{ int a,b;clrscr(); //to clear the output screena=20;b=5;printf(“Addition of %d + %d is = %d”,a+b);printf(“Subtraction of);printf(“Multiplication of);printf(“Division of);printf(“Modulo Division of);getch();

Page 7: tscse.files.wordpress.com€¦  · Web viewCS8261 C PROGRAMMING LABORATORY L T P C 0 0 4 2 . OBJECTIVES: To develop programs in C using basic constructs. To develop applications

}P2: Bit wise Operators

#include<stdio.h>#include<conio.h>main()

{ int a,b;clrscr(); //to clear the output screena=20;b=5;printf(“Bit wise OR of %d + %d is = %d”,a|b);printf(“Bit wise AND of %d + %d is = %d”, );printf(“One’s complement of %d is = %d”, );getch();}

P3: Relational Operators#include<stdio.h>

#include<conio.h>main()

{ int a,b;clrscr(); //to clear the output screena=20;b=5;printf(“a>b : %d”,a>b);getch();}

P4: Logical, Ternary Operators, Increment & decrement Operators#include<stdio.h>#include<conio.h>main()

{ int a,b,c;clrscr(); //to clear the output screena=20;b=5;c=(a>b)?(a+b):(a-b);printf(“The value of c is = ”, );d=(a>b)&&(a>c);printf(“The value of d is = ”, );d=(b<c)&&(a<c);printf(“The value of d is = ”, );printf(“a++ & ++b is = ”,++a,b++);

Page 8: tscse.files.wordpress.com€¦  · Web viewCS8261 C PROGRAMMING LABORATORY L T P C 0 0 4 2 . OBJECTIVES: To develop programs in C using basic constructs. To develop applications

getch();}

RESULT:Thus the C program for expressions has been written & executed successfully.

EX.NO.:2 FINDING ODD OR EVEN

AIM:To Write a C program to find whether a given number is odd or even

PROCEDURE:1. Start the program2. Read an input Value3. Divide it by 2.4. Take the remainder, if the remainder is greater than zero , then5. Print the given Number is EVEN.6. Otherwise, print the given Number is ODD.7. Stop the Programs

SYNTAX:if(condition)

statement;else

statement;

SAMPLE PROGRAM:#include<stdio.h>#include<conio.h>main()

{int Num,remainder;printf(“Enter a Number”);scanf( );remainder= ;if ( )

printf(“%d is EVEN”, );else

printf(“%d is ODD’, );getch();}

OUTPUT:

Page 9: tscse.files.wordpress.com€¦  · Web viewCS8261 C PROGRAMMING LABORATORY L T P C 0 0 4 2 . OBJECTIVES: To develop programs in C using basic constructs. To develop applications

RESULT:Thus the program is written & executed Successfully.

EX.NO.:3 FINDING LEAP YEAR OR NOT

AIM:To Write a C program to find whether the given the given year is leap year or Not.

PROCEDURE:1. Start the program2. Read an input Value (Year)3. If the Year is divisible by both 4 and 100 then,4. Print the given year is LEAP YEAR5. Otherwise, Print the given year is NOT A LEAP YEAR

PROGRAM:#include<stdio.h>#include<conio.h>main()

{int year;printf(“Enter a year”);scanf( );if( )

printf(“%d is a LEAP YEAR”, );else

printf(“%d is NOT a LEAP YEAR”, );}

Page 10: tscse.files.wordpress.com€¦  · Web viewCS8261 C PROGRAMMING LABORATORY L T P C 0 0 4 2 . OBJECTIVES: To develop programs in C using basic constructs. To develop applications

EX.NO.:4 CREATING A CALCULATOR

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:1. Start the program2. Read an option to select the operation: 1.Add 2.Sub 3.Mul 4.Div 5.Square3. Enter the input values according to the operation.4. Use switch case to perform the operation.5. Print the Result.6. Stop the program.

PROGRAM:#include<stdio.h>#include<conio.h>main()

{int option,a,b,c;clrscr();printf(“Enter the option”);printf(“\n 1.ADD \t 2.SUB \t 3.MUL \t 4.DIV \t 5.SQUARE\n”);scanf(“%d”,&option);switch()

{case 1:

printf(“Enter two values”);scanf(“%d%d”,&a,&b);c=a+b;break;

case 2:case 3:case 4:case 5:default:

printf(“Choose the correct option”);}

printf(“ Result is =%d”,c);

Page 11: tscse.files.wordpress.com€¦  · Web viewCS8261 C PROGRAMMING LABORATORY L T P C 0 0 4 2 . OBJECTIVES: To develop programs in C using basic constructs. To develop applications

getch();}

OUTPUT:

RESULT:Thus the program is written & executed Successfully.

Page 12: tscse.files.wordpress.com€¦  · Web viewCS8261 C PROGRAMMING LABORATORY L T P C 0 0 4 2 . OBJECTIVES: To develop programs in C using basic constructs. To develop applications

EX.NO.: 5 FINDING ARMSTRONG NUMBER

AIM:To Write a C program to find whether a given number is Armstrong number or not.

ALGORITHM:1. Start the program.2. Read an input value.3. Split the digits using modulo division.4. Cube the individual digits5. Sum the cubes.6. If the given number == sum of cubes,then

Print “ ARMSTRONG NUMBER”7. Otherwise,

Print “ NOT AN ARMSTRONG NUMBER”8. Stop the program.

PROGRAM:#include<stdio.h>#include<conio.h>main()

{int N,A,digit,cube,sum=0;printf(“Enter a Number”);scanf(“%d”,&N);A=N;while( )

{digit=N%10;cube=(digit*digit*digit);sum=sum+cube;N=N/10;}

if ( sum==A)printf(“%d is Armstrong Number”, );

elseprintf(“%d is not an Armstrong Number”, );

getch();}

Page 13: tscse.files.wordpress.com€¦  · Web viewCS8261 C PROGRAMMING LABORATORY L T P C 0 0 4 2 . OBJECTIVES: To develop programs in C using basic constructs. To develop applications

OUTPUT:

RESULT:Thus the program is written & executed Successfully.

Page 14: tscse.files.wordpress.com€¦  · Web viewCS8261 C PROGRAMMING LABORATORY L T P C 0 0 4 2 . OBJECTIVES: To develop programs in C using basic constructs. To develop applications

Ex:6 FINDING SUM OF WEIGHTS & SORTING THE ELEMENTS BASED ON WEIGHTS

AIM:Write a C program to find sum of weights for a given set of numbers like <10, 36, 54, 89, 12,

27>, Find the sum of weights based on the following conditions. 1. 5 if it is a perfect cube.2. 4 if it is a multiple of 4 and divisible by 6.3. 3 if it is a prime number.

ALGORITHM: Declare & initialize an integer array : a[] Declare an array called w[] to store the weight values of the respective elements. Assign the weight values to each element of that array by checking the conditions

◦ If a[i] is a perfect cube , assign weight 5 to w[i]◦ If a[i] is multiples of 4 & divisible by 6 then assign 4 to w[i]◦ If a[i] is a prime number then assign 3 to w[i]

Add all weight values Print the sum of weights Sort the array elements in increasing order based on their weight values.

PROGRAM:#include<stdio.h>#include<conio.h>#include<math.h>void main(){double cubroot,cube;int a[10]={ },w[10],i,j,N=6,count=0,A,sum=0;clrscr();for(i=0;i<N;i++)

{A=a[i];w[i]=0;

cubroot=ceil(pow(A,(1/3)));cube=cubroot*cubroot*cubroot;if(A==cube)

w[i]=w[i]+5;if(A%4==0 && A%6==0)

w[i]=w[i]+4;

for(j=2;j<=A;j++){if(A%j == 0)

Page 15: tscse.files.wordpress.com€¦  · Web viewCS8261 C PROGRAMMING LABORATORY L T P C 0 0 4 2 . OBJECTIVES: To develop programs in C using basic constructs. To develop applications

count=count+j;}

if(A==1 || A==count)w[i]=w[i]+3;

}printf("\n\nELEMENT\t\t\t\tWEIGHT \n\n");for(i=0;i<N;i++)

{printf("%d \t\t\t\t%d\n\n",a[i],w[i]);sum=sum+w[i];}

printf("The sum of Weights is = sum);printf("Sorting of the Elements based on weight values\n");for(i=0;i<N;i++)

for(j=0;j<N;j++)if(w[j]>w[j+1])

{t=w[j];w[j]=w[j+1];w[j+1]=t;

k=a[j];a[j]=a[j+1];a[j+1]=k;}

for(i=0;i<N;i++){printf("<%d,%d> \t",a[i],w[i]);}

getch();}

OUTPUT :

Page 16: tscse.files.wordpress.com€¦  · Web viewCS8261 C PROGRAMMING LABORATORY L T P C 0 0 4 2 . OBJECTIVES: To develop programs in C using basic constructs. To develop applications

RESULT:Thus the program is written & executed Successfully.

Ex.No:7 FINDING THE PERSONS HAVING ABOVE AVERAGE HEIGHT

AIM:Write a C program to populate an array with height of persons and find how many persons are

above the average height.

ALGORITHM:1. Declare an array to store the height values of persons2. Initialize the count value to 0.3. Read the number of persons4. Read the individual person’s height5. Find the sum of heights6. Find the average of heights7. Check whether a person’s height is greater than the average height or not.

1. If Yes, then increase the count value8. Print the count value.

PROGRAM:#include<stdio.h>#include<conio.h>void main(){float height[20],avg;int N,i,sum=0,count=0;clrscr();printf("Enter the no. of persons");scanf("%d",&N);printf("Enter the persons height on eby one\n");for(i=0;i<N;i++)

{scanf("%f",&height[i]);sum=sum+height[i];}

avg=sum/N;for(i=0;i<N;i++)

{if(height[i]>avg)

{ count=count+1; }}

printf("Totally %d Persons are having above average height",count);getch();}

Page 17: tscse.files.wordpress.com€¦  · Web viewCS8261 C PROGRAMMING LABORATORY L T P C 0 0 4 2 . OBJECTIVES: To develop programs in C using basic constructs. To develop applications

OUTPUT:

RESULT:Thus the program is written & executed Successfully.

Page 18: tscse.files.wordpress.com€¦  · Web viewCS8261 C PROGRAMMING LABORATORY L T P C 0 0 4 2 . OBJECTIVES: To develop programs in C using basic constructs. To develop applications

EX.NO.: 8 FINDING BODY MASS INDEX USING ARRAY

AIM:To Write a C program to perform the following,

▪ Populate a two dimensional array with height and weight of persons▪ Compute the Body Mass Index of the individuals.

ALGORITHM:1. Start the program.2. Read the Number of Persons.3. Enter the height,weight values.4. Read height in centi meters & weight in Kilograms of a person.5. Height in Meter= height in cm/100;6. Compute the body mass index value using the following formula

BMI=(weight in Kg)/(Height in Meters)2

7. Store the BMI values in a resultant array.8. Print the results.9. Stop the program.

PROGRAM:#include<stdio.h>#include<conio.h>void main(){float height[20],weight[20],BMI[20],HIM[20];int i,j,N;clrscr();printf("\nEnter the No.of the elements\n");scanf("%d",&N);printf("\n Enter the Height & Weight values\n");for(i=0;i<N;i++)

{scanf("%f%f",&height[i],&weight[i]);HIM[i]=height[i]/100;}

printf("\nPerson\tHeight\tWeight\tBMS\n");for(i=0;i<N;i++)

{

Page 19: tscse.files.wordpress.com€¦  · Web viewCS8261 C PROGRAMMING LABORATORY L T P C 0 0 4 2 . OBJECTIVES: To develop programs in C using basic constructs. To develop applications

BMI[i]=weight[i]/(HIM[i]*HIM[i]);printf("\n%d\t%.2f\t%.2f\t%.2f\n",(i+1),HIM[i],weight[i],BMI[i]);}

getch();}

OUTPUT:Enter No. of the elements3

Enter the Height & Weight values152 58122 28135 40

Person Height Weight BMS

1 1.52 58.00 25.10

2 1.22 28.00 18.81

3 1.35 40.00 21.95

Page 20: tscse.files.wordpress.com€¦  · Web viewCS8261 C PROGRAMMING LABORATORY L T P C 0 0 4 2 . OBJECTIVES: To develop programs in C using basic constructs. To develop applications

RESULT:Thus the program is written & executed Successfully.

EX.NO.: 9 REVERSE STRING WITHOUT CHANGING THE POSITION OF SPECIAL CHARACTER

AIM:To Write a C program for reversing a string without changing the position of special

characters. (Example input:a@gh%;j and output:j@hg%;a)

ALGORITHM:1. Start the program.2. Read a String.3. Call a function to reverse a string.4. Reverse function

Find strlen Set forward & reverse pointers ( f=0,r=strlen-1) Check the following

o If both str[f] and str[l] are alphanumeric then swap these two chars in that string and do f++,r--

o If str[f] is a special char then f++o Id str[r] is a special char then r--;o If both are special chars then f++ &r--

5. Print the reversed string.6. Stop the program.

PROGRAM:#include<stdio.h>#include<conio.h>void reverse(char *);void main(){ char str[50]; clrscr(); printf("Enter a string\n"); scanf("%s",str); printf( "Input string: %s\n",str); reverse(str); printf("Output string:%s\n",str); getch();}void reverse(char *str){

Page 21: tscse.files.wordpress.com€¦  · Web viewCS8261 C PROGRAMMING LABORATORY L T P C 0 0 4 2 . OBJECTIVES: To develop programs in C using basic constructs. To develop applications

int r = strlen(str) - 1, f= 0; char t; while (f < r) {

if(isalnum(str[f])!=0 && isalnum(str[r])!=0){t=str[r];str[r]=str[f];str[f]=t;f++;r--;}

else if(isalnum(str[f])!=0 && isalnum(str[r])==0){ r--; }

else if(isalnum(str[f])==0 && isalnum(str[r])!=0){ f++; }

else{f++;r--;}

}}

OUTPUT:Enter a string:a@gh%;jInput string: a@gh%;jOutput string:j@hg%;a

RESULT:Thus the program is written & executed Successfully.

Page 22: tscse.files.wordpress.com€¦  · Web viewCS8261 C PROGRAMMING LABORATORY L T P C 0 0 4 2 . OBJECTIVES: To develop programs in C using basic constructs. To develop applications

Ex.No.: 10 SORT THE LIST OF NUMBERS USING PASS BY REFERENCE

AIM:Write a C program to sort numbers using pass by Reference

ALGORITHM:1. Start the Program2. Read the size of array3. Read the elements one by one4. Call the function sort()5. Pass the stating address of the array to the sort function as input.6. Store the address in the formal argument of that function (Pointer variable)

Access the array values by (increasing the addresses) the pointer variable Sort the values using the pointer variable

7. Print the Result8. Stop the program

PROGRAM : #include<stdio.h>void sort(int *,int);void main(){int a[20],N,i;printf("Enter the no. of elements\n");scanf("%d",&N);printf("Enter the Elements one by one\n");for(i=0;i<N;i++)

scanf("%d",&a[i]);sort(a,N);

printf("\n Sorted Order\n");for(i=0;i<N;i++)

printf("%d\t",a[i]);getch();}

void sort(int *x,int n){int t,i,j;for(i=0;i<n-1;i++)

{for(j=0;j<n-1;j++)

{if(*(x+j) > *(x+(j+1)))

Page 23: tscse.files.wordpress.com€¦  · Web viewCS8261 C PROGRAMMING LABORATORY L T P C 0 0 4 2 . OBJECTIVES: To develop programs in C using basic constructs. To develop applications

{t=*(x+j);*(x+j) =*(x+(j+1));*(x+(j+1))=t;}

}}

}

OUTPUT:Enter the no. of elements3Enter the Elements one by one100345Sorted Order3 45 100

Page 24: tscse.files.wordpress.com€¦  · Web viewCS8261 C PROGRAMMING LABORATORY L T P C 0 0 4 2 . OBJECTIVES: To develop programs in C using basic constructs. To develop applications

RESULT:Thus the program is written & executed successfully

Ex.No:11NUMBER SYSTEM CONVERSION USING USER DEFINED FUNCTION

AIMWrite a C program to convert decimal number into octal , binary & hexa decimal using user

defined functions

ALGORITHM1. Start the program2. Read a decimal value3. Call oct() function

Store the remainder when the number is divided by 8 in an array. Divide the number by 8 now Repeat the above two steps until the number is not equal to 0. Print the array in reverse order now.

4. Call hex() function Take a decimal number as input Divide the input number by 16. Store the remainder in the array Do step 2 with the quotient obtained until quotient becomes zero Print the array in the reversed fashion to get hexadecimal number.

5. Call bin() function Store the remainder when the number is divided by 2 in an array. Divide the number by 2 Repeat the above two steps until the number is greater than zero. Print the array in reverse order now.

PROGRAM:#include<stdio.h>void oct(int);void hex(int);void bin(int);void main(){int dec;clrscr();printf("enter a decimal value\n");scanf("%d",&dec);oct(dec);

bin(dec);hex(dec);getch();}

Page 25: tscse.files.wordpress.com€¦  · Web viewCS8261 C PROGRAMMING LABORATORY L T P C 0 0 4 2 . OBJECTIVES: To develop programs in C using basic constructs. To develop applications

void oct(int x){

int octalNum[100],n,i,j;n=x;i = 0;

while (n != 0){octalNum[i] = n % 8;n = n / 8;i++;

}

for (j = i - 1; j >= 0; j--) printf("%d",octalNum[j]); printf("\n\n");}

void bin(int x){

int binaryNum[1000]; // counter for binary array int i = 0,n=x,j; while (n > 0)

{ // storing remainder in binary array binaryNum[i] = n % 2; n = n / 2; i++; } // printing binary array in reverse order for (j = i - 1; j >= 0; j--)

printf("%d",binaryNum[j]); printf("\n\n");}

void hex(int x){

int n=x,j=0,i,remainder,hexadecimal[20];

while (n != 0) {

remainder = n % 16;if (remainder < 10)

Page 26: tscse.files.wordpress.com€¦  · Web viewCS8261 C PROGRAMMING LABORATORY L T P C 0 0 4 2 . OBJECTIVES: To develop programs in C using basic constructs. To develop applications

hexadecimal[j++] = 48 + remainder;else hexadecimal[j++] = 55 + remainder;n =n / 16;

}

// display integer into character for (i = j-1; i >=0; i--)

printf("%c", hexadecimal[i]);}

OUTPUT:enter a decimal value1517

00000000000001111

F

RESULT:Thus the program is written & executed successfully

Page 27: tscse.files.wordpress.com€¦  · Web viewCS8261 C PROGRAMMING LABORATORY L T P C 0 0 4 2 . OBJECTIVES: To develop programs in C using basic constructs. To develop applications

Ex. No. : 12 FINDING NUMBER OF WORDS AND CAPITALIZE THE FIRST WORDAIM:

To write a C program to find the total number of words and capitalize the first word of each sentence.ALGORITHM:

1. Start the program.2. Read a paragraph.3. Count the no. of words4. Capitalize the first word of each sentence.5. Print the capitalized text and no. of words.

PROGRAM:#include<stdio.h>void main(){char text[200],t[100],ch;int n,i,words=0;clrscr();printf("Enter a text\n ");gets(text);i=0;text[i]=toupper(text[i]);while((ch=text[i])!='\0'){

if(ch==' '||ch=='.')words++;

if(ch=='.')text[i+1]=toupper(text[i+1]);

i++;

}printf("\n\tSentence =%s",text);printf("\n\tNo. of Words=%d",words+1);getch();}OUTPUT:Enter a text hai welcome to all.good morning.how are you? Sentence =Hai welcome to all.Good morning.How are you? No. of Words=9

Page 28: tscse.files.wordpress.com€¦  · Web viewCS8261 C PROGRAMMING LABORATORY L T P C 0 0 4 2 . OBJECTIVES: To develop programs in C using basic constructs. To develop applications

RESULT:Thus the program is written & executed successfully.

EX.NO.: 13 EMPLOYEE SALARY SLIP

AIM:To Write a C program to generate employee salary slip using structure & pointer.

ALGORITHM:1. Start the program.2. Create a structure called Employee with empid,empname,dept,designation & salary

fields.3. Create a pointer to a structure.4. Call a function to generate the Salary slip of particular employee5. Stop the program.

PROGRAM:#include<stdio.h>struct employee

{char ename[25];int eid;char edes[20];char edept[20];int esal;};

void salaryslip(struct employee *e,int n){int id,i;printf("\nEnter the employee id to generate the salary slip\n");scanf("%d",&id);for(i=0;i<=n;i++){if((e+i)->eid==id){printf("\n-----------------------------------------------------------------------------");printf("\nNAME\t\tDEPARTMENT\t\tDESIGNATION\t\tSALARY");printf("\n----------------------------------------------------------------------");printf("\n%s\t\t%s\t\t%s\t\t%d",(e+i)->ename,(e+i)->edept,(e+i)->edes,(e+i)->esal);printf("\n--------------------------------------------------------------------------------");}}}

Page 29: tscse.files.wordpress.com€¦  · Web viewCS8261 C PROGRAMMING LABORATORY L T P C 0 0 4 2 . OBJECTIVES: To develop programs in C using basic constructs. To develop applications

void main(){struct employee emp[20],*emp1;int m,i;printf("Enter the no. of employee details");scanf("%d",&m);printf("\nEnter employee id, name, department, designation & salary\n");for(i=0;i<m;i++)

{scanf("%d%s%s%s

%d",&emp[i].eid,&emp[i].ename,&emp[i].edes,&emp[i].edept,&emp[i].esal);}

emp1=emp;salaryslip(emp1,m);

getch();}

OUTPUTEnter the no. of employee details2

Enter employee id, name, department, designation & salary101 A CSE AP 20000202 B ECE AP 25000

Enter the employee id to generate the salary slip101-----------------------------------------------------------------------------NAME DEPARTMENT DESIGNATION SALARY-----------------------------------------------------------------------------A CSE AP 20000-----------------------------------------------------------------------------

Page 30: tscse.files.wordpress.com€¦  · Web viewCS8261 C PROGRAMMING LABORATORY L T P C 0 0 4 2 . OBJECTIVES: To develop programs in C using basic constructs. To develop applications

RESULT:

Thus the program is written & executed successfully.

EX.NO.: 14 STUDENTS INTERNAL MARK SHEET

AIM:To Write a C program to compute internal marks of students for five different subjects

using structures and functions.ALGORITHM:

1. Start the program.2. Create a structure called student with the following fields,

Name Test marks for subjects Internal mark

3. Create a function called internal4. Call a function to compute and print the marks5. Stop the program.

PROGRAM:#include<stdio.h>

struct student{char name[20];int t[15][15];int mark[5];};

void internal( struct student);void main(){struct student s;int j,k;clrscr();

for(j=0;j<3;j++){printf("Enter the internal test %d marks for five subjects:\t",j+1);for(k=0;k<5;k++)

scanf("%d",&s.t[j][k]);}

internal(s);getch();

}

Page 31: tscse.files.wordpress.com€¦  · Web viewCS8261 C PROGRAMMING LABORATORY L T P C 0 0 4 2 . OBJECTIVES: To develop programs in C using basic constructs. To develop applications

void internal(struct student s1){

int i,j,k,sum[20],c=0;for(j=0;j<5;j++) {

c=0;for(k=0;k<3;k++) { c=c+s1.t[k][j]; }s1.mark[j]=((c/3)/5);

}for(i=0;i<5;i++)

printf("\nSubject %d Internal Mark (max. marks 20)= %d",i+1,s1.mark[i]);} OUTPUT: Enter the internal test 1 marks for five subjects: 10 10 10 10 10Enter the internal test 2 marks for five subjects: 70 80 90 10 80Enter the internal test 3 marks for five subjects: 90 90 90 90 90

Subject 1 Internal Mark (max. marks 20)= 11Subject 2 Internal Mark (max. marks 20)= 12Subject 3 Internal Mark (max. marks 20)= 12Subject 4 Internal Mark (max. marks 20)= 7Subject 5 Internal Mark (max. marks 20)= 12

Page 32: tscse.files.wordpress.com€¦  · Web viewCS8261 C PROGRAMMING LABORATORY L T P C 0 0 4 2 . OBJECTIVES: To develop programs in C using basic constructs. To develop applications

RESULT:

Thus the program is written & executed successfully.

Ex.No: 15 SEQUENTIAL ACCESS FILE

AIM:Write a C program to count the number of account holders whose balance is less than the

minimum balance using sequential access file.

ALGORITHM:1. Start the program2. Read choice to insert records & count minimum balance account 3. If choice is 1, then

Open a dat file in write mode Read the No. of records Write the records into the file using fprintf() function Close the file

4. If Choice is 2, then Open the file in Read mode Read the records one by one using fscanf(0 function until reach the end of file. Check the account balance with min bal. If account balance is less than min balance, then display the account details Close the file

5. Stop the program

PROGRAM:#include <stdio.h>

void insert();void count();

int main(void) { int choice = 0; while (choice != 3) { printf("\n1 insert records\n"); printf("2 Count min balance holders\n"); printf("3 Exit\n");

Page 33: tscse.files.wordpress.com€¦  · Web viewCS8261 C PROGRAMMING LABORATORY L T P C 0 0 4 2 . OBJECTIVES: To develop programs in C using basic constructs. To develop applications

printf("Enter choice:"); scanf("%d", &choice); switch(choice) { case 1: insert(); break; case 2: count(); break; } }}

void insert(){ unsigned int account,i; char name[30]; double balance; FILE* cfPtr; if ((cfPtr = fopen("clients.dat", "w")) == NULL) { puts("File could not be opened"); } else { int records,i=0; printf("Enter the No. of records "); scanf("%d", &records); while (i<records) { printf("Enter the account, name, and balance."); scanf("%d%29s%lf", &account, name, &balance); fprintf(cfPtr, "%d %s %.2f\n", account, name, balance); i++; } fclose(cfPtr); }}

void count(){ unsigned int account; char name[30]; double balance; float minBal = 5000.00; int count = 0; FILE *cfPtr;

Page 34: tscse.files.wordpress.com€¦  · Web viewCS8261 C PROGRAMMING LABORATORY L T P C 0 0 4 2 . OBJECTIVES: To develop programs in C using basic constructs. To develop applications

if ((cfPtr = fopen("clients.dat", "r")) == NULL) printf("File could not be opened"); else { printf("%-10s%-13s%s\n", "Account", "Name", "Balance"); fscanf(cfPtr, "%d%29s%lf", &account, name, &balance);

while (!feof(cfPtr)) { if (balance < minBal) { printf("%-10d%-13s%7.2f\n", account, name, balance); count++; } fscanf(cfPtr, "%d%29s%lf", &account, name, &balance); }

fclose(cfPtr); printf("The number of account holders whose balance is less than the minimum balance: %d", count); } }

Page 35: tscse.files.wordpress.com€¦  · Web viewCS8261 C PROGRAMMING LABORATORY L T P C 0 0 4 2 . OBJECTIVES: To develop programs in C using basic constructs. To develop applications

OUTPUT:1 insert records2 Count min balance holders3 ExitEnter choice:1Enter the No. of records 2Enter the account, name, and balance.1001 A 10000Enter the account, name, and balance.1002 B 300

1 insert records2 Count min balance holders3 ExitEnter choice:2Account Name Balance1002 B 300.00The number of account holders whose balance is less than the minimum balance: 11 insert records2 Count min balance holders3 ExitEnter choice:

Page 36: tscse.files.wordpress.com€¦  · Web viewCS8261 C PROGRAMMING LABORATORY L T P C 0 0 4 2 . OBJECTIVES: To develop programs in C using basic constructs. To develop applications

RESULT:

Thus the program is written & executed successfully.

Ex.No: 16 TOWERS OF HANOI

AIM:To Write a C program to solve towers of Hanoi using recursion.

ALGORITHM:1. Start the program2. Create a function called towers()3. Read Number of disks4. Call the function towers()5. Recursively call this function to move the disks from source to destination.6. Stop the program

PROGRAM:#include <stdio.h> void towers(int,char,char,char); void 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'); }

void towers(int num,char frompeg,char topeg,char auxpeg){ if (num == 1) { printf("\n Move disk 1 from peg %c to peg %c", frompeg, topeg); return; } towers(num - 1, frompeg, auxpeg, topeg); printf("\n Move disk %d from peg %c to peg %c", num, frompeg, topeg); towers(num - 1, auxpeg, topeg, frompeg);

Page 37: tscse.files.wordpress.com€¦  · Web viewCS8261 C PROGRAMMING LABORATORY L T P C 0 0 4 2 . OBJECTIVES: To develop programs in C using basic constructs. To develop applications

}

OUTPUT:

Enter the number of disks : 3The sequence of moves involved in the Tower of Hanoi are :

Move disk 1 from peg A to peg C Move disk 2 from peg A to peg B Move disk 1 from peg C to peg B Move disk 3 from peg A to peg C Move disk 1 from peg B to peg A Move disk 2 from peg B to peg C Move disk 1 from peg A to peg C

Page 38: tscse.files.wordpress.com€¦  · Web viewCS8261 C PROGRAMMING LABORATORY L T P C 0 0 4 2 . OBJECTIVES: To develop programs in C using basic constructs. To develop applications

RESULT:

Thus the program is written & executed successfully.

Ex.No: 17 RANDOM ACCESS FILE

AIM:To Write a C program to update telephone details of an individual or a company into a

telephone directory using random access file.

ALGORITHM:1. Start the program2. Store the telephone details into a file3. Read the data & Display it4. Enter the telephone number to be modified & the new number5. Use fseek() function to randomly access the record6. Copy the contents from source file to destination file7. Store the updated record into the new file8. Stop the program

PROGRAM:

#include<stdio.h>struct teledir{int no;char name[3];};

void main(){struct teledir t1,t2,t3;int i,n,p,newp;FILE *fp,*fp1;clrscr();fp=fopen("td.txt","w");printf("Enter the no of records\n");scanf("%d",&n);printf("Enter the record\n");for (i=0;i<n;i++)

{scanf("%d%s",&t1.no,t1.name);fwrite(&t1,sizeof(struct teledir),1,fp);

Page 39: tscse.files.wordpress.com€¦  · Web viewCS8261 C PROGRAMMING LABORATORY L T P C 0 0 4 2 . OBJECTIVES: To develop programs in C using basic constructs. To develop applications

}fclose(fp);fp=fopen("td.txt","r");

while(fread(&t2,sizeof(struct teledir),1,fp)!=NULL){printf("\t%d%s\n",t2.no,t2.name);}

printf("Enter number to be modified & a new number\n");scanf("%d%d",&p,&newp);fclose(fp);fp=fopen("td.txt","r+");fp1=fopen("td1.txt","w");while(fread(&t2,sizeof(struct teledir),1,fp)!=NULL)

{if(t2.no==p)

{fseek(fp,-sizeof(struct teledir),SEEK_CUR);t3.no=newp;strcpy(t3.name,t2.name);fwrite(&t3,sizeof(struct teledir),1,fp1);}

else{fwrite(&t2,sizeof(struct teledir),1,fp1);}

}fclose(fp);fclose(fp1);fp=fopen("td1.txt","r");while(fread(&t3,sizeof(struct teledir),1,fp)!=NULL)

{printf("\t%d\t%s\n",t3.no,t3.name);}

fclose(fp);

getch();}

OUTPUT:

Page 40: tscse.files.wordpress.com€¦  · Web viewCS8261 C PROGRAMMING LABORATORY L T P C 0 0 4 2 . OBJECTIVES: To develop programs in C using basic constructs. To develop applications

Enter the no of records3Enter the record111 abc222 xyz333 nmo 111abc 222xyz 333nmoEnter number to be modified & a new number222 9898 111 abc 9898 xyz 333 nmo

RESULT:Thus the program is written & executed successfully.