C Lab Manual
C Lab Manual
C Lab Manual
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
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<conio.h>
#include<stdio.h>
#include<math.h>
void 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);
}
getch();
}
2. Write a C program to simulate a simple calculator that performs arithmetic operations
like 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<stdio.h>
#include<conio.h>
void 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;
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<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int num, num1, rev, rem;
clrscr();
printf("Enter the number to be reversed\n");
scanf("%d", &num);
if(num>9999||num<1000)
{
printf("Kindly enter a four digit number\n");
getch();
return;
}
num1=num;
rev=0;
while(num1>0)
{
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();
}
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<stdio.h>
#include<conio.h>
#include<stdlib.h>
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<n;i++)
{
scanf("%d",&a[i]);
}
printf("enter the key");
scanf("%d",&key);
low=0;
high=n-1;
while(low<=high)
{
mid =( low + high)/2;
if (key==a[mid])
{
printf("successful search");
getch();
exit(0);
}
else if(key<a[mid])
high=mid-1;
else
low=mid+1;
}
printf("unsuccessful search");
getch();
}
5. Write a C program to input N integer numbers into a single dimension array.
Sort them in ascending order using bubble sort technique. Print both the given
array and the sorted array with suitable headings
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
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<n;i++)
{
scanf("%d",&a[i]);
}
for(j=1;j<n;j++)
{
for(i=0;i<n-j;i++)
{
if(a[i]>a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
}
printf("sotred ele are\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
getch();
}
6. Write a C program to generate all the prime numbers between 1 and n, where n is a
value supplied by the user
#include<stdio.h>
int main()
{
int n,i,j;
printf("enter number for prime : ");
scanf("%d",&n);
if(n==0)
{
printf("invalid number entered");
return 0;
}
printf("1 ");
for(i=2;i<=n;i++)
{
for(j=2;j<=i;j++)
{
if(i%j==0)
{
if(i==j)
{
printf("%d ",j);
}
break;
}
}
}
return 0;
}
7. Write a C program to read two matrices A(M x N) and B(P x Q) and compute the
product of A and B after checking compatibility for multiplication. Output the input
matrices and the resultant matrix with suitable headings and format. (Using two
dimension arrays where array size M, N, P,Q 3).
#include<stdio.h>
#include<conio.h>
void input(int k[20][20], int, int);
void multiplication(int [20][20], int, int, int [20][20], int, int,int [20][20]);
void display(int [20][20], int, int);
void main()
{
int a[20][20], b[20][20], c[20][20];
int m, n, p, q;
clrscr();
printf("Enter the values of the order of the first matrix mxn \n");
scanf("%d%d", &m, &n);
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<m; i++)
{
for(j=0; j<n; j++)
{
scanf("%d", &num[i][j]);
}
}
}
void multiplication(int a[20][20], int m, int n, int b[20][20], int p, int q, int c[20][20])
{
int i, j, k;
if(n!=p)
{
printf("The matrices are not compatible for multiplication \n");
getch();
return;
}
for(i=0; i<m; i++)
{
for(j=0; j<q; j++)
{
c[i][j]=0;
for(k=0; k<n; k++)
{
c[i][j]+=(a[i][k]*b[k][j]);
}
}
}
}
void display(int num[20][20], int m, int n)
{
int i , j;
for(i=0; i<m; i++)
{
printf("| ");
for(j=0; j<n; j++)
{
printf("%d ", num[i][j]);
}
printf("|\n");
}
}
8. Write C user defined functions
(i) To input N integer numbers into a single dimension array.
(ii) To conduct a linear search.
Using these functions, write a C program to accept the N integer numbers & given key
integer number and conduct a linear search. Report success or failure in the form of a
suitable message.
#include<stdio.h>
#include<conio.h>
int n;
void input(int[]);
void search(int[]);
void main()
{
int num[50], key, pos, i;
clrscr();
entry:
printf("Enter the number of elements in the list\n");
scanf("%d", &n);
if(n<1)
{
printf("Enter valid values \n");
getch();
goto entry;
}
input(num);
search(num);
getch();
}
void input(int num[])
{
int i;
printf("Enter the values of the array\n");
for(i=0; i<n; i++)
scanf("%d", &num[i]);
}
void search(int num[])
{
int key, i, pos;
printf("Enter the number being searched for \n");
scanf("%d", &key);
pos=-1;
for(i=0; i<n; i++)
{
if(num[i]==key)
{
pos=i;
}
}
if(pos==-1)
printf("Search failure. The number wasn't found \n");
else
printf("Search successful. Position is %d\n", (pos+1));
}
9. Write C user defined functions
(i) To input N integer numbers into a single dimension array.
(ii) To sort the integer numbers in descending order using selection sort technique.
(iii) To print the single dimension array elements.
Using these functions, write a C program to input N integer numbers into a single
dimension array, sort them in descending order, and print both the given array &
the sorted array with suitable headings.
#include<stdio.h>
#include<conio.h>
int num[20], n;
void input();
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<n; i++)
scanf("%d", &num[i]);
}
void sort()
{
int i,j, temp, pos;
for(i=0; i<n-1; i++)
{
pos=i;
for(j=i; j<n; j++)
{
if(num[j]>num[pos])
{
pos=j;
}
}
temp=num[i];
num[i]=num[pos];
num[pos]=temp;
}
}
void display()
{
int i;
for(i=0; i<n; i++)
printf("%d\n", num[i]);
}
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<stdio.h>
#include<conio.h>
struct 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<n; i++)
{
printf("\nRecord no: %d\n", (i+1));
printf("Enter the name: ");
fflush(stdin);
gets(s[i].name);
printf("Enter the roll no: ");
scanf("%d", &s[i].rollno);
for(j=0; j<3; j++)
{
printf("Enter the marks for test %d: ", (j+1));
scanf("%d", &s[i].marks[j]);
}
}
for(i=0; i<n-1; i++)
{
for(j=0; j<n-i-1; j++)
{
if(s[j].rollno>s[j+1].rollno)
{
temp=s[j];
s[j]=s[j+1];
s[j+1]=temp;
}
}
}
printf("\nThe sorted records are as follows \n");
for(i=0; i<n; i++)
{
printf("\nRoll no: %d\n", s[i].rollno);
printf("Name: %s\n", s[i].name);
for(j=0; j<3; j++)
{
printf("Marks of test %d: %d\n", (j+1),
s[i].marks[j]);
}
}
getch();
}
11. Write a C program to copy a string using pointers, to compare two strings using
pointers and to concatenate two strings using pointers
/*strcopy*/
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[20], str2[20];
char *sr1, *sr2;
int len1, len2;
clrscr();
printf("Enter the first string\n");
gets(str1);
sr1=&str1[0];
sr2=&str2[0];
while(*sr!='\0')
{
*sr2=sr1;
sr2++;
sr1++;
}
*sr2='\0';
printf("The original string is: %s\nThe copied string is: %s\n",
str1, str2);
getch();
}
/*concat*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void 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 horny
12. Write a C program to evaluate the given polynomial ()
for given value of x and the coefficients using Horners method. (Using single
dimension arrays to store coefficients) [Hint: Rewrite the given polynomial as
()
(
(