c lab 1st internal examination for eee 1st year_001

36
C Lab 1 st Internal Examination for EEE 1 st Year Program Code: Q1. 1S- Write a C program to find average of 5 integers a b c d e using typecasting 1B- Write a C program to arrange in ascending order using bubble sort using function /*1S-average of 5 integers a b c d e using typecasting*/ #include<stdio.h> #include<conio.h> void main() { int a,b,c,d,e; int total; float average; clrscr(); printf("enter the values of the 5 integers:\n"); scanf("%d%d%d%d%d",&a,&b,&c,&d,&e); total = a+b+c+d+e; average = (float)total/5; printf("average of 5 integers is %f",average); getch(); } /* 1B- To arrange in ascending order using bubble sort using function*/ #include<stdio.h> #include<conio.h> #define SIZE 5 void bublesort(int x[], int size); void main() { int a[SIZE],i; clrscr(); printf("enter the 5 array elements:\n"); for(i=0;i<SIZE;i++) { scanf("%d",&a[i]); } printf("array elements in original order are:\n\n"); 1 VCE | 1 st Year – programming lab/ Internal Exam programs/Syed Jaffar Ali

Upload: hemanth-pradeep

Post on 14-Oct-2014

1.648 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: C Lab 1st Internal Examination for EEE 1st Year_001

C Lab 1st Internal Examination for EEE 1st YearProgram Code:

Q1. 1S- Write a C program to find average of 5 integers a b c d e using typecasting1B- Write a C program to arrange in ascending order using bubble sort using function

/*1S-average of 5 integers a b c d e using typecasting*/#include<stdio.h>#include<conio.h>void main(){ int a,b,c,d,e; int total; float average; clrscr(); printf("enter the values of the 5 integers:\n"); scanf("%d%d%d%d%d",&a,&b,&c,&d,&e); total = a+b+c+d+e; average = (float)total/5; printf("average of 5 integers is %f",average); getch(); }

/* 1B- To arrange in ascending order using bubble sort using function*/#include<stdio.h>#include<conio.h>#define SIZE 5void bublesort(int x[], int size);

void main(){ int a[SIZE],i; clrscr(); printf("enter the 5 array elements:\n"); for(i=0;i<SIZE;i++) { scanf("%d",&a[i]); } printf("array elements in original order are:\n\n"); for(i=0;i<SIZE;i++) { printf ("%d\t",a[i]); } /*calling bubble sort*/bublesort(a, SIZE);getch();}void bublesort(int x[], int size)

1 VCE | 1st Year – programming lab/ Internal Exam programs/Syed Jaffar Ali

Page 2: C Lab 1st Internal Examination for EEE 1st Year_001

{ int pass, i, hold; for(pass=1;pass<size;pass++)

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

{ if(a[i]>a[i+1])

{ hold=a[i]; a[i]=a[i+1]; a[i+1]=hold;

} }}

printf("\n data items in descending order are\n\n"); for(i=0;i<size;i++) { printf(" %d\t",a[i]); } }

2 VCE | 1st Year – programming lab/ Internal Exam programs/Syed Jaffar Ali

Page 3: C Lab 1st Internal Examination for EEE 1st Year_001

Q2. 2S- Write a C program to find average using sentinel control2B- Write a C program to arrange in descending order using bubble sort using function

/* 2S- average using sentinel control */#include<stdio.h>#include<conio.h>void main(){ int i=0,marks; float total=0,average; clrscr(); printf("enter the marks of the students:\n"); printf("enter '-1' to end:\n"); scanf("%d",&marks); while(marks!=-1) { total=total+marks; i=i+1; scanf("%d",&marks); } if(i!=0) { average=(float)total/i; printf("class average is %f",average); } else { printf("no marks were entered:\n"); } getch(); }

/* 2B- To arrange in descending order using bubble sort using function*/#include<stdio.h>#include<conio.h>#define SIZE 5void bublesort(int x[], int size);

void main(){ int a[SIZE],i; clrscr(); printf("enter the 5 array elements:\n"); for(i=0;i<SIZE;i++) { scanf("%d",&a[i]);

3 VCE | 1st Year – programming lab/ Internal Exam programs/Syed Jaffar Ali

Page 4: C Lab 1st Internal Examination for EEE 1st Year_001

} printf("array elements in original order are:\n\n"); for(i=0;i<SIZE;i++) { printf ("%d\t",a[i]); } /*calling bubble sort*/bublesort(a, SIZE);getch();}void bublesort(int x[], int size){ int pass, i, hold; for(pass=1;pass<size;pass++)

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

{ if(a[i]<a[i+1])

{ hold=a[i]; a[i]=a[i+1]; a[i+1]=hold;

} }}

printf("\n data items in descending order are\n\n"); for(i=0;i<size;i++) { printf(" %d\t",a[i]); } }

4 VCE | 1st Year – programming lab/ Internal Exam programs/Syed Jaffar Ali

Page 5: C Lab 1st Internal Examination for EEE 1st Year_001

Q3. 3S-i- Write a C program to evaluate the expression a=p(1+r)n using library functions,3S-ii-Write a C program to calculate the value of y=(5*x*x*x*+7*x*x+9) using library functions,3B- Write a C program to demonstrate linear search technique using functions

/*3S(i)-program to evaluate the expression a=p(1+r)(power n)*/

#include<stdio.h>#include<conio.h>#include<math.h>void main(){ float p,r; int n; double a; clrscr(); printf("enter the values of p,r,n:\n"); printf(" a-amount after n years\n p-origianl amount deposited\n" ); printf("r-rate of interest\n n-no of years\n"); scanf("%f%f%d",&p,&r,&n); a=p*pow(1+r,n); printf("amount after n years is %f",a); getch(); }

/*3S(ii)-to calculate the value of y=(5*x*x*x*+7*x*x+9)*/

#include<stdio.h>#include<conio.h>#include<math.h>void main(){ int x; float y; clrscr(); printf("enter the value of x:\n"); scanf("%d",&x); y=(5*pow(x,3)+7*pow(x,2)+9); printf("the value of the result is %f:",y); getch(); }

/*3B-linear search using functions*/#include<stdio.h>#include<conio.h>#define SIZE 10int linearsearch(const int array[],int key);

void main(){ int a[SIZE],x,searchkey,element,i; clrscr(); printf("enter 10 array elements:\n"); for(i=0;i<SIZE;i++) { scanf("%d",&a[i]); } printf("enter integer search key:\n"); scanf("%d",&searchkey); element = linearsearch(a,searchkey);

5 VCE | 1st Year – programming lab/ Internal Exam programs/Syed Jaffar Ali

Page 6: C Lab 1st Internal Examination for EEE 1st Year_001

if(element!=-1) printf("found value in element %d",element); else printf("value not found\n"); getch(); } int linearsearch(const int array[],int key) { int n; for(n=0;n<SIZE;n++)

{ if (array[n]==key)

{ return n;

} }

return -1; }

6 VCE | 1st Year – programming lab/ Internal Exam programs/Syed Jaffar Ali

Page 7: C Lab 1st Internal Examination for EEE 1st Year_001

Q4. 4S- i- Write a C program to sum the even integer numbers from 2 to 10000 using do...while repetition statements4S- ii-Write a C program to sum the even integer numbers from 2 to 10000 using for repetition statements4B- Write a C program for demonstrating Binary search of an array

/* 4Sii-C program to sum the even integer numbers from 2 to 10000 using do...while repetition statements */#include<stdio.h>void main(){ long int counter=1,sum=0;

do { if((counter%2)==0) { sum = sum+counter; } } while (++counter <= 10000); printf("Sum of Even integers from 2 to 10000 is:%ld",sum);

}

/* 4Si-C program to sum the even integer numbers from 2 to 10000 using for repetition statements */#include<stdio.h>void main(){ long int counter,sum=0;

for(counter=1; counter<=10000; counter++) { if((counter%2)==0) { sum = sum+counter; } } printf("Sum of Even integers from 2 to 10000 is:%ld",sum);

}

/* 4B-Binary search of an array */#include <stdio.h>#include <conio.h>#define SIZE 15int binarySearch( const int b[], int searchKey, int low, int high );

void main(){ int a[ SIZE ], i, key, result;

for ( i = 0; i <= SIZE - 1; i++ ) { a[ i ] = 2 * i; }

printf( "Enter a number between 0 and 28: " ); scanf( "%d", &key );

7 VCE | 1st Year – programming lab/ Internal Exam programs/Syed Jaffar Ali

Page 8: C Lab 1st Internal Examination for EEE 1st Year_001

result = binarySearch( a, key, 0, SIZE - 1 );

if ( result != -1 ) printf( "\n%d found in array element %d\n", key, result ); else printf( "\n%d not found\n", key );

getch(); }

int binarySearch( const int b[], int searchKey, int low, int high ){ int middle; while ( low <= high ) { middle = ( low + high ) / 2; if ( searchKey == b[ middle ] ) return middle; else if ( searchKey < b[ middle ] ) high = middle - 1; else low = middle + 1; } return -1; /* searchKey not found */}

Q5.

8 VCE | 1st Year – programming lab/ Internal Exam programs/Syed Jaffar Ali

Page 9: C Lab 1st Internal Examination for EEE 1st Year_001

5S- Write a C program to swap two integers without using third variable5B- Write a C program for addition and subtraction of two matrices

/*5s-program to swap two integers without using third variable*/#include<stdio.h>#include<conio.h>void main(){

int A,B;clrscr();printf("\t\t PROGRAM FOR SWAPING VALUES OF TWO VARIABLES \n");printf("\n Enter Two values:- ");printf("\n A= ");scanf("%d",&A);printf("B= ");scanf("%d",&B);A=A+B;B=A-B;A=A-B;printf("\n The values After Swaping:- ");printf("\n A= %d",A);printf("\n B= %d",B);getch();

}

/* 5B- addition and subtraction of two matrices */#include<stdio.h>void getdata();void matrix_add();void matrix_subtract();

int A[10][10],B[10][10],C[10][10],D[10][10];int r1,c1,r2,c2,i,j;

void main(){ getdata(); /* printing A and B matrices contents */ printf("\n A matrix is\n"); for(i=0; i<r2; i++) { for(j=0; j<c2; j++) { printf("%d\t",A[i][j]); } printf("\n"); } printf("\n B matrix is\n");

9 VCE | 1st Year – programming lab/ Internal Exam programs/Syed Jaffar Ali

Page 10: C Lab 1st Internal Examination for EEE 1st Year_001

for(i=0; i<r2; i++) { for(j=0; j<c2; j++) { printf("%d\t",B[i][j]); } printf("\n"); } matrix_add(); matrix_subtract();}void getdata(){ printf("\n Enter the size of A"); scanf("%d%d", &r1, &c1); printf("\n Enter the size of B"); scanf("%d%d", &r2, &c2);

if(r1 !=0 && c1 !=0) { printf("Enter the Elementsof A matrix"); for(i=0; i<r1; i++) for(j=0; j<c1; j++) scanf("%d", &A[i][j]); }

if(r2 !=0 && c2!=0) { printf("Enter the Elementsof B matrix"); for(i=0; i<r2; i++) for(j=0; j<c2; j++) scanf("%d", &B[i][j]); }}

void matrix_add() {

if (r1 == r2 && c1 == c2) { for(i=0; i<r2; i++) { for(j=0; j<c2; j++) { C[i][j] = A[i][j] + B[i][j]; } } }

10 VCE | 1st Year – programming lab/ Internal Exam programs/Syed Jaffar Ali

Page 11: C Lab 1st Internal Examination for EEE 1st Year_001

printf("\n Result of A+B is:=\n"); for(i=0; i<r2; i++) { for(j=0; j<c2; j++) { printf("%d\t",C[i][j]); } printf("\n"); } } void matrix_subtract() {

if (r1 == r2 && c1 == c2) { for(i=0; i<r2; i++) { for(j=0; j<c2; j++) { D[i][j] = A[i][j] - B[i][j]; } } } printf("\n Result of A-B is:=\n"); for(i=0; i<r2; i++) { for(j=0; j<c2; j++) { printf("%d\t",D[i][j]); } printf("\n"); } }

Q6. 6S-i- Write a C Program for finding the maximum of three integers using functions6S-ii- Write a C Program for generation of faces of dice for 30 times6B- Write a C Program for matrix multiplication A*B of nxn order

11 VCE | 1st Year – programming lab/ Internal Exam programs/Syed Jaffar Ali

Page 12: C Lab 1st Internal Examination for EEE 1st Year_001

/* 6Si- Finding the maximum of three integers using functions*/#include <stdio.h>int maximum( int x, int y, int z ); /* function prototype */

void main(){ int a, b, c; printf( "Enter three integers: " ); scanf( "%d%d%d", &a, &b, &c ); printf( "Maximum is: %d\n", maximum( a, b, c ) );}/* Function maximum definition */int maximum( int x, int y, int z ){ int max = x; if ( y > max ) max = y; if ( z > max ) max = z; return max;}

/* 6Sii- generation of faces of dice for 30 times */#include <stdio.h>#include <stdlib.h>

void main(){ int i; for ( i = 1; i <= 30; i++ ) { printf( "%10d", 1 + ( rand() % 6 ) ); if ( i % 5 == 0 ) printf( "\n\n" ); }

}

/*6B- matrix multiplication A*B of nxn order*/#include<stdio.h>void getdata(int x[][10], int r, int c);void matrix_mul();void print_result(int y[][10], int r, int c);

int A[10][10],B[10][10],C[10][10];int r1,c1,r2,c2,i,j,k;

void main(){ printf("\n Enter the size of A"); scanf("%d%d", &r1, &c1); printf("Enter the Elements of A matrix"); getdata(A,r1,c1); printf("\n Enter the size of B"); scanf("%d%d", &r2, &c2); printf("Enter the Elements of B matrix"); getdata(B,r2,c2); matrix_mul();

12 VCE | 1st Year – programming lab/ Internal Exam programs/Syed Jaffar Ali

Page 13: C Lab 1st Internal Examination for EEE 1st Year_001

/* Printing the ALL Results */

printf("\n A matrix is:\n"); print_result(A,r1,c1); printf("\n B matrix is: \n"); print_result(B,r2,c2);printf(" \nout put for expression A*B is:\n"); print_result(C,r1,c2);}

void getdata(int x[][10], int r, int c){ if(r!=0 && c !=0) {

for(i=0; i<r; i++){ for(j=0; j<c; j++){ scanf("%d", &x[i][j]); } } } }

void matrix_mul() {

if (r2 == c1) { for(i=0; i<r1; i++){ for(j=0; j<c2; j++){ C[i][j]=0; for(k=0; k<r2; k++){ C[i][j] = C[i][j] + (A[i][k])*(B[k][j]); } } } }}

void print_result(int y[][10], int r, int c) { for(i=0; i<r; i++){ for(j=0; j<c; j++){ printf("%d\t",y[i][j]); } printf("\n");

13 VCE | 1st Year – programming lab/ Internal Exam programs/Syed Jaffar Ali

Page 14: C Lab 1st Internal Examination for EEE 1st Year_001

} }

Q7. 7S- Write a C program for finding factorial without using recursion7B- Write a C program to demonstrate Bubble sort using pointers

/* 7S- Finding factorial without using recursion */#include<stdio.h>void main(){ long int fact=1,i,n; printf("\nEnter the integer no whose factorial has to be calculated:\n"); scanf("%ld",&n);

14 VCE | 1st Year – programming lab/ Internal Exam programs/Syed Jaffar Ali

Page 15: C Lab 1st Internal Examination for EEE 1st Year_001

for(i=n; i>=1; i--) { fact=fact*i; } printf("\n the Factorial of %ld is: %ld",n,fact);}

/*7B-bubble sort using pointers*/#include<stdio.h>#include<conio.h>#define SIZE 10void bubblesort(int *const b,const int size);void main(){int a[SIZE]={1,3,2,4,5,7,6,8,10,9};int i;printf("data items in original order\n");for(i=0;i<SIZE;i++){printf("%d \t",a[i]);}bubblesort(a,SIZE);printf("data items in ascending order\n");for(i=0;i<SIZE;i++){printf("%d\t",a[i]);}}void bubblesort(int *const b,const int size){void swap(int* x,int *y);int pass,j;for(pass=1;pass<size;pass++){for(j=0;j<(size-pass);j++){if(b[j]>b[j+1]){swap(&b[j],&b[j+1]);}}}}void swap(int *x,int *y){int t;t=*x;*x=*y;*y=t;

15 VCE | 1st Year – programming lab/ Internal Exam programs/Syed Jaffar Ali

Page 16: C Lab 1st Internal Examination for EEE 1st Year_001

}

Q8. 8S- Write C program to find Factorial of 1 to 10 integers using Recursive function8B- Write a C program to print address label of students using structures

/* 8S- Recursive factorial function */#include <stdio.h>long factorial( long number);

void main(){ int i;

for ( i = 1; i <= 10; i++ ) printf( "%2d! = %ld\n", i, factorial( i ) ); }

16 VCE | 1st Year – programming lab/ Internal Exam programs/Syed Jaffar Ali

Page 17: C Lab 1st Internal Examination for EEE 1st Year_001

long factorial( long number ){ if ( number <= 1 ) return 1; else return ( number * factorial( number - 1 ) );}

/*8B-address label of students using structures*/#include<stdio.h>#include<conio.h>void main(){struct student{char name[20];char rollno[15];char section[5];int age;};struct student details[40];int i;for(i=0;i<5;i++){printf("enter the student details: Name, Age, RollNo and Section \n");fflush(stdin);gets(details[i].name);scanf("%d",&details[i].age);fflush(stdin);gets(details[i].rollno);fflush(stdin);gets(details[i].section);}for(i=0;i<5;i++){printf("\n\nthe student[%d] details are:",i+1);printf("Name: %s",details[i].name);printf("\n\t\t\t Age: %d",details[i].age);printf("\n\t\t\t RollNo: %s", details[i].rollno);printf("\n\t\t\t Section:%s", details[i].section);}getch();}

17 VCE | 1st Year – programming lab/ Internal Exam programs/Syed Jaffar Ali

Page 18: C Lab 1st Internal Examination for EEE 1st Year_001

Q9. 9S- Write a C program to summarize total and average using Arrays9B- Write a C program to create a sequential file and write the contents and print it

/* 9S- Summarise total and avg using arrays */#include <stdio.h>#define SIZE 20

void main(){ int a[ SIZE ]; int i, total = 0, avg; for ( i = 0; i <= SIZE - 1; i++ ) { printf("Enter the value for Array of 20 size:"); scanf("%d",&a[i]); total += a[ i ]; }

18 VCE | 1st Year – programming lab/ Internal Exam programs/Syed Jaffar Ali

Page 19: C Lab 1st Internal Examination for EEE 1st Year_001

avg=(float)total/i; printf( "Total of array element values is %d\n", total ); printf( "Avarage of array element values is %d\n", avg );}

/* 9B-Create a sequential file and write the contents and print it */#include <stdio.h>

void main(){ int account; char name[ 30 ]; double balance; FILE *cfPtr; /* cfPtr = clients.dat file pointer */

if ( ( cfPtr = fopen( "clients.txt", "w" ) ) == NULL ) printf( "File could not be opened\n" ); else { printf( "Enter the account, name, and balance.\n" ); printf( "Enter EOF to end input.\n" ); printf( "? " ); scanf( "%d%s%lf", &account, name, &balance );

while ( !feof( stdin ) ) { fprintf( cfPtr, "%d %s %.2f\n",

account, name, balance ); printf( "? " ); scanf( "%d%s%lf", &account, name, &balance );

}

fclose( cfPtr ); }

if ( ( cfPtr = fopen( "clients.txt", "r" ) ) == NULL ) printf( "File could not be opened\n" ); else { printf( "the accountno, name, and balance are.\n" ); printf("%-15d%-13s%s", "Account No", "Name", "Balance"); fscanf(cfptr,"%d%s%lf", &account, name, &balance );

while ( !feof( cfptr ) ) {

printf( "%-15d %-13s %7.2f\n", account, name, balance );

19 VCE | 1st Year – programming lab/ Internal Exam programs/Syed Jaffar Ali

Page 20: C Lab 1st Internal Examination for EEE 1st Year_001

fscanf( cfptr,"%d%s%lf", &account, name, &balance ); } fclose( cfPtr ); } }

Q10. 10S-Write a C program to print the Student poll for rating the quality of food10B- Write a C program for finding nCr using Recursive factorial function

/* 10S-Student poll program for quality of food */#include <stdio.h>#define RESPONSE_SIZE 40#define FREQUENCY_SIZE 11void main(){ int answer, rating, frequency[ FREQUENCY_SIZE ] = { 0 }; int responses[ RESPONSE_SIZE ] = { 1, 2, 6, 4, 8, 5, 9, 7, 8, 10, 1, 6, 3, 8, 6, 10, 3, 8, 2, 7, 6, 5, 7, 6, 8, 6, 7, 5, 6, 6, 5, 6, 7, 5, 6, 4, 8, 6, 8, 10 };

for ( answer = 0; answer <= RESPONSE_SIZE - 1; answer++ ) ++frequency[ responses [ answer ] ];

printf( "%s%17s\n", "Rating", "Frequency" );

for ( rating = 1; rating <= FREQUENCY_SIZE - 1; rating++ ) printf( "%6d%17d\n", rating, frequency[ rating ] );}

20 VCE | 1st Year – programming lab/ Internal Exam programs/Syed Jaffar Ali

Page 21: C Lab 1st Internal Examination for EEE 1st Year_001

/* 10B- nCr Recursive factorial function */#include <stdio.h>

double factorial( long a);

void main(){ int n, r; long int result; printf("Enter n and r Values\n"); scanf("%d%d", &n, &r); result = factorial(n) / ( factorial(n-r) * factorial(r)); printf("Result is: %ld",result) ;

}

double factorial( long a ){ if ( a <= 1 ) return 1; else return ( a * factorial( a - 1 ) );}Q11.

11S- Write a C program to demonstrate passing arrays to a function by reference and value11B- Write a C program for File compare and where ever they differ print the contents of two

Files.

\*11s-passing arrays to a function by reference and value*\#include<stdio.h>#define SIZE 5void arraay1(int b[],int size);void array2(int e);void main(){int a[SIZE]={0,1,2,3,4},i;printf("effect of passing entire array by reference\n");printf("original elements of array are\n");for(i=0;i<SIZE;i++){printf("%d\t,a[i]");}array1(a,SIZE);printf("\n the array values after modification in function\n");for(i=0;i<SIZE;i++){printf("%d\t,a[i]");}

21 VCE | 1st Year – programming lab/ Internal Exam programs/Syed Jaffar Ali

Page 22: C Lab 1st Internal Examination for EEE 1st Year_001

printf("effect of passing array elements by value:\n");printf("\n the value of a[3] is %d",a[3]);array2(a[3])printf("value of a[3] is%d",a[3]);}void array1(int b[],int size){int j;for(j=o;j<SIZE;j++){b[j]*=4;}}void array2(int e){printf("value in array 2 is%d\n",e*=5);}

/* 11B- File compare where ever they differ print the contents of two files */#include<stdio.h>#include<stdlib.h>#include<conio.h>void main(){ FILE *fp1,*fp2; int c1, c2; clrscr(); fp1=fopen("fileD.txt","r"); if(fp1 == NULL) printf("could not open the file:\n"); else fp2=fopen("fileE.txt","r"); if(fp2 == NULL) printf("could not open the file:\n"); else { printf("\n"); while (((c1=fgetc(fp1)) != EOF) && ((c2=fgetc(fp2)) != EOF)) { if (c1!=c2) { printf("\nFileD Character differed is= "); putc(c1,stdout); printf("\t FileE Character differed is= "); putc(c2,stdout);

22 VCE | 1st Year – programming lab/ Internal Exam programs/Syed Jaffar Ali

Page 23: C Lab 1st Internal Examination for EEE 1st Year_001

} } } getch(); }

Q12. 12S- Write a C program for Finding Area and perimeter for Square or rectangle using function12B- Write a C program for File concatenation (i.e adding A and B files in to C file )

/* 12S- Finding Area and perimeter for Square or rectangle using function */#include<stdio.h>#include<conio.h>void square();void rectangle();

void main(){ float side,width,height; int choice; clrscr(); printf("caluculation of area,perimeter of squre and rectangle :\n\n"); printf("\n acept choice \n\n 1. square \n\n2. rectangle:\n"); printf("\n\n\enter choice:"); scanf("%d",&choice); if(choice ==1) { square(); } else { rectangle(); } getch();}void square()

23 VCE | 1st Year – programming lab/ Internal Exam programs/Syed Jaffar Ali

Page 24: C Lab 1st Internal Examination for EEE 1st Year_001

{ float side,area,perimeter; printf("enter side of the square;\n"); scanf("%f",&side); area=side*side; perimeter=4*side; printf("\nArea is= %f \t Perimeter is=%f",area,perimeter);}void rectangle(){

float width, height,area,perimeter; printf("enter width and height of the square;\n"); scanf("%f%f",&width,&height); area=width*height; perimeter=2*(width+height); printf("\nArea is= %f \t Perimeter is=%f",area,perimeter);

}/* 12B- File concatenation (i.e Adding A and B files in to C file) */#include<stdio.h>#include<conio.h>#include<stdlib.h>void main(){ FILE *fpA, *fpB, *fpC; char c; fpA = fopen("fileA.txt","r"); if(fpA == NULL) { printf("Unable to open file:\n"); exit(0); } else { fpC=fopen("fileC.txt","w"); while(1) { c=fgetc(fpA); if(c == EOF) break; else fputc(c, fpC); } fclose(fpA); } fpB=fopen("fileB.txt","r"); if(fpB == NULL) { printf("could not open file");

24 VCE | 1st Year – programming lab/ Internal Exam programs/Syed Jaffar Ali

Page 25: C Lab 1st Internal Examination for EEE 1st Year_001

exit(0); } else { while(1) { c=fgetc(fpB); if(c == EOF) break; else fputc(c,fpC); } fclose(fpB); fclose(fpC); } fpC=fopen("fileC.txt","r"); if(fpC == NULL) { printf("file could not open:\n"); } else { while((c=fgetc(fpC))!=EOF) { printf("%c",c); } getch(); }}

25 VCE | 1st Year – programming lab/ Internal Exam programs/Syed Jaffar Ali

Page 26: C Lab 1st Internal Examination for EEE 1st Year_001

Q13. 13S-Write a C program to convert string to upper case using pointers and functions13B write a C program to find Fibonacci number of any integer using recursive technique

/* 13S- convert string to upper case using pointers and functions*/#include<stdio.h>#include<ctype.h>void convertuppercase(char *sptr);void main(){

char string[]="Character and $ 32.86";printf("The string before conversion is : \n%s",string);convertuppercase(string);printf("The string after conversion is : \n%s",string);

}void convertuppercase(char *sptr){

while(*sptr!='\0')if(islower(*sptr)){

*sptr=toupper(*sptr);}++sptr;

}

/*13-B write a C program to find Fibonacci number of any integer using recirsive technique*/#include<stdio.h>#include<conio.h>

long fibonacci(long n);

void main(){

26 VCE | 1st Year – programming lab/ Internal Exam programs/Syed Jaffar Ali

Page 27: C Lab 1st Internal Examination for EEE 1st Year_001

long result,number; clrscr(); printf("\n enter an integer"); scanf("%ld",&number);

result = fibonacci(number); printf("\n Fibonacci of (%ld) = %ld",number,result);

getch();}

long fibonacci(long n){if(n==0||n==1)return n;elsereturn fibonacci(n-1)+fibonacci(n-2);}

27 VCE | 1st Year – programming lab/ Internal Exam programs/Syed Jaffar Ali

Page 28: C Lab 1st Internal Examination for EEE 1st Year_001

Q14. 14S- Write a C program to find the Area of circle for r1=6.25 & r2=2.5 using macro with Arguments14B- Write a C program to demonstrate conditional compilation directives

/* 14S- macros with arguments */#include<stdio.h>#define AREA(x) (3.14159*x*x)void main(){float r1=6.25,r2=2.5,a;a=AREA(r1);printf("\nArea of a Circle for r1 = %f is a1 = %f",r1,a);a=AREA(r2);printf("\nArea of a Circle for r2 = %f is a2 = %f",r2,a);}

/*14B- conditional compilation directives */#include<stdio.h> #include<conio.h>#define Z 4#ifdef Z#if Z<5

#define I 50

void main(){ int i,j=1;

clrscr(); for(i=1; i<=I; i++) {gotoxy(i,j);printf("%d",i);j++;}getch();}#else#ifndef W#define W 50void main(){ int i,j=50; clrscr(); for(i=1; i<=W; i++) {gotoxy(i,j);printf("%d",i);j--;}getch();}#endif#endif#endif

28 VCE | 1st Year – programming lab/ Internal Exam programs/Syed Jaffar Ali

Page 29: C Lab 1st Internal Examination for EEE 1st Year_001

Q15. 15S- Write a C Program to find the given year is leap or not15B- Write a C Program to print plus symbol on screen using macros

/* 15S- C Program to find the given year is leap or not */#include<stdio.h>#include<conio.h>

void main(){int year,i;clrscr();printf("\nenter any number to continue or -1 to exit");scanf("%d",&i);while (i!=-1){printf("enter any 4 digit year\n");scanf("%d",&year);

if (year%4==0&&year%100!=0||year%400==0)printf("the Year is: Leap year");elseprintf("The Year is: Not a Leap year");printf("\nenter any number to continue or -1 to exit");scanf("%d",&i);}getch();}

/* 15B- printing plus symbol on screen using macros */ #include<stdio.h> #include<conio.h> #define HLINE for(i=0; i<79; i++)\

printf("%c",196);

#define VLINE(X,Y) { gotoxy(X,Y);\printf("%c",179);}

void main() {

29 VCE | 1st Year – programming lab/ Internal Exam programs/Syed Jaffar Ali

Page 30: C Lab 1st Internal Examination for EEE 1st Year_001

int i,y; clrscr();

gotoxy(1, 25); HLINE for(y=1; y<50; y++) VLINE(39, y);}

30 VCE | 1st Year – programming lab/ Internal Exam programs/Syed Jaffar Ali