c lab manual
Post on 11-Oct-2015
52 Views
Preview:
DESCRIPTION
TRANSCRIPT
-
5/21/2018 c Lab Manual
1/40
R. V. COLLEGE OF ENGINEERING,
BANGALORE-560059(AUTONOMOUS INSTITUTION AFFILIATED TO VTU, BELGAUM)
Department of Information Science and Engineering
PROGRAMMING IN C
LAB MANUAL
(12CS13)
I SEMESTER
LAB INCHARGE:
GEETHA V
ASST PROFESSOR
DEPT OF ISE
RVCE
-
5/21/2018 c Lab Manual
2/40
1. Write a C program to find and output all the roots of a given quadratic equation,
for non-zero coefficients. (Using ifelse statement).
#include
#include
#includevoid main()
{
int a, b, c;float root1, root2, real1, real2, ip1, ip2, D;
clrscr();
printf("Enter the co-efficients of quadratic equation\n");printf("Enter a, b, and c in ax^2+bx+c \n");
scanf("%d%d%d", &a, &b, &c);
if(a==0)
{
printf("First co-efficient shouldn't be zero \n");getch();
return;}
D=(float)(b*b)-(float)(4*a*c);
if(D==0)
{root1=(float)(-1*b)/(float)(2*a);
root2=root1;
printf("Roots are real and equal. They are: \n");printf("%f\n%f\n", root1, root2);
}else if(D>0)
{printf("Roots are real and distinct. They are: \n");
D=sqrt(D);
root1=(float)(-1*b)/(float)(2*a)+(float)(D)/(float)(2*a);root2=(float)(-1*b)/(float)(2*a)-(float)(D)/(float)(2*a);
printf("%f\n%f\n", root1, root2);}
else{
printf("Roots are imaginary. They are: \n");D=abs(D);
D=sqrt(D);real1=(float)(-1*b)/(float)(2*a);
real2=real1;
ip1=(float)(D)/(float)(2*a);ip2=(float)(-1*ip1);
printf("%f+i%f\n%f+i%f\n", real1, ip1, real2, ip2);
-
5/21/2018 c Lab Manual
3/40
}
getch();
}
2.Write a C program to simulate a simple calculator that performs arithmetic operationslike addition, subtraction, multiplication, and division only on integers. Error message
should be reported, if any attempt is made to divide by zero. (Using switch statement).
#include
#includevoid main()
{
int a, b, sum, dif, prod, div;
char op;clrscr();
printf("Enter the operands\n");scanf("%d%d", &a,&b);
fflush(stdin);
printf("Enter the operator: + - * or / \n");
scanf("%c",&op);
switch(op)
{case '+': sum=a+b;printf("The sum is %d\n", sum);
break;
case '-': dif=a-b;printf("The difference is %d\n", dif);
break;
case '*': prod=a*b;
printf("The product is %d\n", prod);break;
case '/': if(b==0)
{ printf("Division by zero is not possible \n");getch();
return;
}div=a/b;
printf("The quotient is %d\n", div);
break;
-
5/21/2018 c Lab Manual
4/40
default: printf("Kindly enter the correct operator \n");
}
getch();}
3. Write a C program to reverse a given four-digit integer number and check whether
it is a palindrome or not. Output the given number with suitable message (using
looping constructs).
#include#include
#include
void main()
{int num, num1, rev, rem;
clrscr();printf("Enter the number to be reversed\n");scanf("%d", &num);if(num>9999||num0){
rem=num1%10;
rev=rev*10+rem;num1/=10;
}
printf("The revers of %d is %d\n", num, rev);
if(rev==num){
printf("The number is a palindrome\n");
}else{
printf("The number is not a palindrome\n");
}getch();
}
-
5/21/2018 c Lab Manual
5/40
4. Write a C program to input N real numbers in ascending order into a single dimension
array. Conduct a binary search for a given key integer number and report success or
failure in the form of a suitable message.
#include
#include
#include
void main()
{
int n,i,a[10],key,mid,low,high;
clrscr();
printf("enter the value of n\n");
scanf("%d",&n);
printf("enter the array elements");
for(i=0;i
-
5/21/2018 c Lab Manual
6/40
{
mid =( low + high)/2;
if (key==a[mid])
{
printf("successful search");
getch();
exit(0);
}
else if(key
-
5/21/2018 c Lab Manual
7/40
#include
#include
#include
void main()
{
int n,i,a[10],j,temp;
clrscr();
printf("enter the value of n\n");
scanf("%d",&n);
printf("enter the elements of array");
for(i=0;i
-
5/21/2018 c Lab Manual
8/40
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
}
printf("sotred ele are\n");
for(i=0;i
-
5/21/2018 c Lab Manual
9/40
}
printf("1 ");
for(i=2;i
-
5/21/2018 c Lab Manual
10/40
input(a, m, n);
printf("Enter the values of the order of the second matrix pxq\n");
scanf("%d%d", &p , &q);input(b, p, q);
multiplication(a, m, n, b, p, q, c);
printf("The first matrix is as follows \n");display(a, m, n);printf("The second matrix is as follows \n");
display(b, p, q);
printf("The product matrix is as follows \n");display(c, m, q);
getch();
}
void input(int num[20][20], int m, int n){
int i, j;
printf("Enter the %d elements of the matrix\n", (m*n));for(i=0; i
-
5/21/2018 c Lab Manual
11/40
void display(int num[20][20], int m, int n)
{
int i , j;for(i=0; i
-
5/21/2018 c Lab Manual
12/40
void input(int num[])
{
int i;printf("Enter the values of the array\n");
for(i=0; i
-
5/21/2018 c Lab Manual
13/40
void sort();void display();void main(){clrscr();
printf("Enter the size of the array \n");scanf("%d", &n);input();sort();printf("The sorted array is as follows \n");display();getch();}void input(){int i;printf("Enter the %d elements of the array \n", n);for(i=0; i
-
5/21/2018 c Lab Manual
14/40
10. Create a structure called student with the following members student name, roll- no,
marks in three tests. Write a C program to create N records and sort them using bubble
sort and display sorted records.
#include
#includestruct student{char name[20];int rollno;int marks[3];};void main(){int i, j, k, n;struct student s[20];
struct student temp;clrscr();printf("Enter the number of records created \n");scanf("%d", &n);printf("Enter the required data for each record \n");for(i=0; i
-
5/21/2018 c Lab Manual
15/40
printf("\nThe sorted records are as follows \n");for(i=0; i
-
5/21/2018 c Lab Manual
16/40
#include#includevoid main(){char str1[20], str2[20];
int len1, len2;char *sr1, *sr2;clrscr();printf("Enter the first string\n");gets(str1);printf("Enter the string to be added\n");gets(str2);len1=strlen(str1);sr1=&str1[len1];sr2=&str2[0];while(*sr2!='\0'){
*sr1=*sr2;sr1++;sr2++;
}*sr1='\0';printf("The concatenated string is as follows : %s\n", str1);getch();}
Im so horny12. Write a C program to evaluate the given polynomial for given value of x and the coefficients using Horners method. (Using singledimension arrays to store coefficients) [Hint: Rewrite the given polynomial as
( ) and evaluate thefunction starting from the inner loop.
#include
#include
-
5/21/2018 c Lab Manual
17/40
#include
void main()
{
int n,i;
float a[30],x,sum=0;
clrscr();
printf("enter the value of n");
scanf("%d",&n);
printf("enter n+1 co-efficients");
for(i=0;i=0;i--)
{
sum=(sum+a[i])*pow(x,i);
}
-
5/21/2018 c Lab Manual
18/40
sum=sum+a[0];
printf("result=%f",sum);
getch();
}
13. Write a C program to open a file in read mode and copy the content of the file to new
file. Opening of the file should be in binary mode only.
14. Write a C program to open a file and if it exists count number of characters, number of
words and number of lines and display the results. If file does not exists display
appropriate error message.
#include#includevoid main(){FILE *fp;char ch;int now=0, noc=0, nol=0, word_start=0;clrscr();
fp=fopen("test.txt", "r");if(fp==NULL)
printf("File doesn't exist\n");else{
while(ch){
ch=fgetc(fp);if(ch==EOF)
break;noc++;
if(ch=='\n'){
nol++;if(word_start)
now++;word_start=0;
}else if(ch=='\t'||ch==' ')
-
5/21/2018 c Lab Manual
19/40
{if(word_start)
now++;word_start=0;
}
else word_start=1;
}if(noc!=0)
nol++;if(word_start)
now++;fclose(fp);printf("No of lines= %d\nNo of characters= %d\nNo of words=%d\n", nol, noc, now);}getch();}
15.Write C user defined functions
(i) To input N real numbers into a single dimension array.
(ii) Compute their mean.
(iii) Compute their variance
(iv) Compute their standard deviation
#include#include#includevoid input(int[], int);float mean(int[], int);float stddev(int[], int);float variance(int[], int);void main()
{int a[50];int n;clrscr();printf("Enter the number of elements \n");scanf("%d", &n);input(a, n);printf("The mean of the set of numbers is : %f\n", mean(a,n));
-
5/21/2018 c Lab Manual
20/40
printf("The variance of the set of numbers is: %f\n",variance(a, n));printf("The standard deviation for the set of numbers is: %f\n",stddev(a, n));getch();
}void input(int num[], int n){int i;printf("Enter the %d elements of the array\n", n);for(i=0; i
-
5/21/2018 c Lab Manual
21/40
16. Write a C program to count the lines, words and characters in a given text
#include(stdio.h) // place this '' instead of '(' & ')'
before stdio.h
#include(conio.h)
#include(ctype.h)
int main( )
{
char ch;
int line=0, space=0, ct=0;
clrscr( );printf("Enter the required no. of lines:\n");
while((ch=getchar( ))!=EOF)
{
if(ch==10) { line++; }
if(isspace(ch)) { space++; }
ct++;
}
printf("\nNumber of Lines : %d", line+1);
printf("\nNumber of Words: %d", space+1);
printf("\nTotal Number of Characters: %d", ct);
getch( );
return 0;
}
17. Write C programs that implement the following sorting methods using functions to sort
a given list of signed integers in ascending order:
i) Insertion sort ii) Merge sort
/*insertion sort */
#include "stdio.h"
void main( )
-
5/21/2018 c Lab Manual
22/40
{int arr[5] = { 25, 17, 31, 13, 2 } ;int i, j, k, temp ;
printf ( "Insertion sort.\n" ) ;
printf ( "\nArray before sorting:\n") ;
for ( i = 0 ; i j ; k-- )arr[k] = arr[k - 1] ;
arr[k + 1] = temp ;}
}}
printf ( "\n\nArray after sorting:\n") ;
for ( i = 0 ; i 0 && nr>0){
if(left[i]
-
5/21/2018 c Lab Manual
23/40
k++;nl--;
}else{
result[k] = right[j];j++;k++;nr--;
}}while(nl>0){
result [k] = left[i];i++;k++;nl--;
}while(nr>0){
result[k] = right[j];j++;k++;nr--;
}}
void main()
{int a[] = {11,33,95};int b[] = {45,82,94};int c[6],i;printf("\n The first array is: {11,33,95}");printf("\n The second array is: {45,82,94}");merge(a,b,c,3,3);printf("\n The sorted list is: ");
for(i=0;i
-
5/21/2018 c Lab Manual
24/40
the int type. In order to limit your number from 0 to n, make the returned value %
(n+1).Use this array to conduct interpolation search
19. Write a program to read a string and write it in reverse order.
#include#include
main()
{
char str[50],revstr[50];
int i=0,j=0;
printf("Enter the string to be reversed : ");
scanf("%s",str);
for(i=strlen(str)-1;i>=0;i--)
{revstr[j]=str[i];
j++;
}
revstr[j]='\0';
printf("Input String : %s",str);
printf("\nOutput String : %s",revstr);
getch();
}
20. Write a program to check that the input string palindrome or not.
#include#include#include#includevoid main(){char str[50];int i,l,f;cout
-
5/21/2018 c Lab Manual
25/40
exit(0);}}cout
-
5/21/2018 c Lab Manual
26/40
22. Write a C program that uses functions to perform the following operations:
i) To insert a sub-string in to given main string from a given position.
ii) To delete n Characters from a given position in a given string.
/*insertion*/
#include
#include
main(){
char mainstr[100],insstr[30],temp[50];
int l1,l2,l,i,j,pos;
clrscr();
printf("Enter a main string:");gets(mainstr);
printf("Enter the sub string:");gets(insstr);
printf("\n Enter the position to be inserted:");
scanf("%d",&pos);
l2=strlen(insstr);l1=strlrn(mainstr);
if(pos>l1)
{
for(i=0;i
-
5/21/2018 c Lab Manual
27/40
for(j=pos;j
-
5/21/2018 c Lab Manual
28/40
i. Sum of the elements of the row
ii. Sum of the elements of the column
iii. Sum of all the elements of the matrix
iv. Sum of both diagonal elements of a matrix
v. Transpose of a matrix.
#include #include
voidmain(){intarr[10][10];inti, j, row, col, rowsum, colsum,sumall=0;
/* Function declaration */intAddrow(intA[10][10], intk, intc);
intAddcol(intA[10][10], intk, intc);
clrscr();
printf("Enter the order of the matrix\n");scanf("%d %d", &row, &col);
printf("Enter the elements of the matrix\n");for(i=0; i
-
5/21/2018 c Lab Manual
29/40
}
for(j=0; j
-
5/21/2018 c Lab Manual
30/40
inttranspose(intA[][10], intr, intc); /*Function prototype*/
clrscr();
printf("Enter the order of matrix A\n");scanf("%d %d", &M, &N);
printf("Enter the elements of matrix\n");for(i=0;i
-
5/21/2018 c Lab Manual
31/40
24. Create a structure called student with the following members student name, roll-no,
marks in three tests. Write a C program to create N records and
(i) Search on roll-no and display all the records
(ii) Average marks in each test
(iii) Highest in each test.
#include#includestruct student
{int rollno;char name[20];int marks[3];}s[20];void main(){int i, j, n, key, pos;int high[3];float avg[3];clrscr();
printf("Enter the number of records \n");scanf("%d", &n);printf("Enter the required data for each record\n");for(i=0; i
-
5/21/2018 c Lab Manual
32/40
if(s[i].rollno==key){
pos=i;break;
}
}if(pos==-1)printf("Search failure. Such a record does not exist \n");
else{
printf("Search successful. Details are as follows: \n");printf("Name: %s\n", s[pos].name);printf("Roll no: %d\n", s[pos].rollno);for(i=0; i
-
5/21/2018 c Lab Manual
33/40
program to find the distance travelled at regular intervals of time given the values of u
and a. The program should provide the flexibility to the user to select his own time
intervals and repeat the calculations for different values of u and a.
#include
#include void main(){int tim_intrval, counter,time;float accl, distance=0, velos;clrscr();printf("");printf("\n\n\n\t\t\tNO OF TIME INTERVALS : ");scanf("%d",&tim_intrval);for(counter = 1; counter
-
5/21/2018 c Lab Manual
34/40
printf("enter the number\n");
scanf("%d",&n);
for(i=2;i
-
5/21/2018 c Lab Manual
35/40
printf("Enter the number of rows : ") ;scanf("%d", &r) ;printf("\nFloyd's triangle is : \n\n") ;for(i = 0 ; i < r ; i++){
for(j = 0 ; j 0 ; --r)printf(" ") ;for(x = 0 ; x
-
5/21/2018 c Lab Manual
36/40
28. Write a C program to construct a pyramid of numbers.
#include
#include
void main()
{int height, line, i;
clrscr();
scanf("%d", &height);
for (i = 0; i < height - 1; ++i)
printf(" ");
printf("1\n");
for (line = 1; line < height; ++line)
{
for (i = 0; i < height - line - 1; ++i)
printf(" ");
for (i = 0; i < line; ++i)
printf("%d", line + 1);
printf(" ");
for (i = 0; i < line; ++i)
printf("%d", line + 1);
printf("\n");
}
getch();
}
#include
#include
void main( )
{
int r, c, num=1 ,len;
clrscr( );
printf("Enter the required no. of rows:");scanf("%d", &len);
for( r=1; r
-
5/21/2018 c Lab Manual
37/40
printf("%2d ", num);
num++;
}
}
getch( );}
29. Write a C program that uses functions to perform the following operations:
i) Addition of two complex numbers
ii) Multiplication of two complex numbers
(Note: represent complex number using a structure.)
#include
#includevoid arithmetic(int opern);struct comp{double realpart;double imgpart;};void main(){int opern;clrscr();printf("\n\n \t\t\t***** MAIN MENU *****");printf("\n\n Select your option: \n 1 : ADD\n 2 : MULTIPLY\n 0 : EXIT\n\n\t\t Enter your Option[]\b\b");scanf("%d",&opern);
switch(opern){case 0:exit(0);case 1:case 2:arithmetic(opern);default:main();}}void arithmetic(int opern)
{struct comp w1, w2, w;printf("\n Enter two Complex Numbers (x+iy):\n Real Part of FirstNumber:");scanf("%lf",&w1.realpart);printf("\n Imaginary Part of First number:");scanf("%lf",&w1.imgpart);printf("\n Real Part of Second Number:");
-
5/21/2018 c Lab Manual
38/40
scanf("%lf",&w2.realpart);printf("\n Imaginary Part of Second Number:");scanf("%lf",&w2.imgpart);switch(opern){/*addition of complex number*/case 1:
w.realpart = w1.realpart+w2.realpart;w.imgpart = w1.imgpart+w2.imgpart;break;/*multiplication of complex number*/case 2:w.realpart=(w1.realpart*w2.realpart)-(w1.imgpart*w2.imgpart);w.imgpart=(w1.realpart*w2.imgpart)+(w1.imgpart*w2.realpart);break;}if (w.imgpart>0)printf("\n Answer =%lf+%lfi",w.realpart,w.imgpart);elseprintf("\n Answer = %lf%lfi",w.realpart,w.imgpart);getch();main();}
30. Write C programs to implement the Lagrange interpolation and Newton- Gregory
forward interpolation
Lagrange interpolation:
#include#include#define MaxN 90void main(){float arr_x[MaxN+1], arr_y[MaxN+1], numerator, denominator, x, y=0;int i, j, n;clrscr();printf("Enter the value of n: \n");scanf("%d", &n);printf("Enter the values of x and y: \n");for(i=0; i
-
5/21/2018 c Lab Manual
39/40
for (j=0; j
-
5/21/2018 c Lab Manual
40/40
i=0;while(!(arr_x[i]>x))/* Finding x0 */i++;i--;p=(x-arr_x[i])/h;
y=arr_y[i];for (k=1; k
top related