You are on page 1of 18

Procedure to Execute C program in Unix

Step 1 : Step 2 : Step 3 : Step 4 : Step 5 : Step 6 : Step 7 : Step 8 : Step 9 : Step 9 : Select Run option from Start . Type Telnet IP address of Unix server (192.168.111.6) Enter into Unix environment by using login name and Password Type vi filename.c in $ prompt Press i to enter into Insert mode and type a program press Esc key and :wq for Save and Exit from vi editor Type cc filename.c in $ prompt to compile a program Type ./a.out to execute a program. Apply input and verify Output To Exit from Unix environment, Type exit in $ prompt.

Ex. No: 1 FACTORIAL & FIBONACCI SERIES USING FUNCTIONS Date:

AIM: To write a C program to find the factorial of the given number and generating the Fibonacci series using functions. ALGORITHM: Step 1: Start the Program. Step 2: Declare the variables and functions. Step 3: Get the option. Step 4: if the option is 1, calculate the factorial of given number using fact() function. Step 5: If the option is 2, generate the print the Fibonacci series using fib() function. Step 6: Return the result. Step 7: Stop the program.

PROGRAM: #include<stdio.h> main() { int ch,n,f,a; printf("\nFACTORIAL AND FIBONACCI SERIES USING FUNCTIONS\n"); printf("\nMENU\n\n1.Factorial\n2.Fibonacci Series\n"); printf("\nEnter your choice:"); scanf("%d",&ch); switch(ch) {

case 1: printf("\nEnter the number:"); scanf("%d",&n); f=fact(n); printf("\nThe factorial of %d is: %d\n",n,f); break; case 2: printf("\nEnter the range:"); scanf("%d",&a); fib(a); break; } } int fact(int n) { int f=1,i; while(n>0) { f=f*n; n--; } return f; }

int fib(int n) { int f1=-1,f2=1,f3=0,i; for(i=0;i<n;i++) { f3=f1+f2; f1=f2;

f2=f3; printf("%d\t",f3); } OUTPUT: FACTORIAL AND FIBONACCI SERIES USING FUNCTIONS MENU 1.Factorial 2.Fibonacci Series Enter your choice:1 Enter the number:3 The factorial of 3 is: 6 FACTORAIL AND FIBONACCI SERIES USING FUNCTIONS MENU 1.Factorial 2.Fibonacci Series Enter your choice:2 Enter the range:6 0 RESULT: Thus the C program to find the factorial value of given number and generating the Fibonacci series using functions was executed successfully. 1 1 2 3

Ex. No: 2 LINEAR SEARCH Date: PROGRAM:

AIM: To write a C program to perform linear search using function. ALGORITHM: Step 1: Start the program. Step 2: Get the Range. Step 3: Get the elements. Step 4: Get the element to be searched. Step 5: Using the function linear() search the element. Step 6: Print the result. Step 7: Stop the program.

PROGRAM: //LINEAR SEARCH USING FUNCTIONS #include<stdio.h> void linear(int [],int,int); main() { int n,i,x,a[10]; printf("\nEnter the limit:"); scanf("%d",&n); printf("\nEnter the elements:"); for(i=0;i<n;i++) { scanf("%d",&a[i]); } printf("\nEnter the element to be searched:"); scanf("%d",&x); linear(a,n,x); } void linear(int a[],int n,int x) { int i,flag=0; for(i=0;i<n;i++) { if(x==a[i]) { flag=1; break; } } if(flag==1) printf("\nElement %d is in the position %d",a[i],i+1);

else printf("\nElement is not in the list"); }

OUTPUT:

Enter the limit:5 Enter the elements:10 20 80 90 40 Enter the element to be searched:80 Element 80 is in the position 3

Enter the limit:5 Enter the elements:10 82 65 48 95 Enter the element to be searched:100 Element is not in the list

RESULT: Thus the C program to perform linear search using functions was executed successfully.

Ex. No: 3 REVERSE OF GIVEN STRING Date: AIM: To write a C program to read the characters and print reversely using recursion. ALGORITHM: Step 1: Start the program. Step 2: Declare the function reverse(). Step 3: Get the characters. Step 4: Recursively call the function reverse() Step 5: Print the result. Step 6: Stop the program.

PROGRAM:

//STRING REVERSE USING RECURSION #include<stdio.h> main() { void reverse(); printf("\nEnter the text:"); reverse(); } void reverse() { char c; if((c=getchar())!='\n')

reverse(); putchar(c); }

OUTPUT:

Enter the text: computer programming lab - ii

ii - bal gnimmargorp retupmoc

RESULT: Thus the C program to reverse the given string using recursion was executed successfully.

Ex. No: 4 SWAPPING USING POINTERS Date:

AIM: To write a C program to swap the 2 given numbers using pointers. ALGORITHM: Step 1: Start the program. Step 2: Get the 2 numbers. Step 3: Call the function swap() by passing address of the variables. Step 4: Interchange the values by using the temporary variable t. Step 5: Return and print the result. Step 6: Stop the program.

PROGRAM: //SWAP THE 2 NUMBERS USING POINTERS #include<stdio.h> main() { int a,b; printf("\nEnter the 2 numbers:"); scanf("%d %d",&a,&b); printf("\nBefore Swapping:%d %d\n",a,b); swap(&a,&b); printf("\nAfter Swapping:%d %d\n",a,b); }

swap(int *a,int *b) { int t; t=*a; *a=*b; *b=t; }

OUTPUT: Enter the 2 numbers:4 6

Before Swapping:4 6

After Swapping:6 4

RESULT: Thus the C program to swap the 2 given numbers using recursion was executed successfully.

Ex. No: 5 EMPLOYEE DETAILS USING FILES Date:

AIM: To write a C program for handling employee details using Files. ALGORITHM: Step 1: Start the program. Step 2: Read the filename. Step 3: Open a specified file. Step 4: Get the range. Step 5: Set a loop, to get the name, salary and age. Step 6: Read the name, salary, age from the file. Step 7: Print the name, salary and age into the file. Step 8: Close the file. Step 9: Stop the program.

PROGRAM: //EMPLOYEE DETAILS USING FILE #include<stdio.h> main() { FILE *fp1; int age,i,n; char filename[25],name[25];

float salary; printf("\nEnter the filename:"); scanf("%s",filename); fp1=fopen(filename,"w"); printf("\nEnter the range:"); scanf("%d",&n); printf("\nEnter the Name, Salary and Age:\n"); for(i=0;i<n;i++) { scanf("%s %f %d",&name,&salary,&age); fprintf(fp1,"%s \t %8.2f \t %d \n",name,salary,age); } fclose(fp1); fprintf(stdout,"\n\n"); fp1=fopen(filename,"r"); printf("\n Name \t Salary \t Age \n"); printf("\n"); for(i=0;i<n;i++) { fscanf(fp1,"%s %f %d",name,&salary,&age); fprintf(stdout,"%s \t %8.2f \t %d \n",name,salary,age); } fclose(fp1); }

OUTPUT: Enter the filename:ggk

Enter the range:3

Enter the Name, Salary and Age: dhars 26000 4 gopi 50000 25 dhans 20000 30

Name

Salary

Age

dhars 26000.00 gopi dhans 50000.00 20000.00

4 25 30

RESULT: Thus the C program for handling the employee details using file was executed successfully.

Ex. No: 6 STRING CONCATENATION Date:

AIM: To write a C program to perform String concatenation using Dynamic Memory Allocation.

ALGORITHM: Step 1: Start the program. Step 2: Declare the character pointers. Step 3: Allocate the memory for strings. Step 4: Read the Strings. Step 5: Get the choice. Step 6: Perform concatenation operation. Step 7: Print the result. Step 8: Stop the Program.

PROGRAM:

//STRING CONCATENATION #include<stdio.h> #include<malloc.h> #define length 10

main() { char *s1,*s2,*s3,c; int i,k; s1=(char *)malloc(length *sizeof(char)); s2=(char *)malloc(length *sizeof(char)); s3=(char *)malloc(2*length *sizeof(char)); printf("\nEnter the String1: "); scanf("%s",s1); printf("\nEnter the String2: "); scanf("%s",s2); i=0; while((c=*(s1+i))!='\0') { s3[i]=c; i++; } k=0; while((c=*(s2+k))!='\0') { s3[i+k]=c; k++; } s3[i+k]='\0'; printf("\nConcatenated String: %s",s3); } OUTPUT:

Enter the String1: sasi Enter the String2: dhars

Concatenated String: sasidhars

RESULT: Thus the C program to perform string concatenation operation using dynamic memory allocation was executed successfully.

You might also like