C Lab Manual

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 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

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
()

(

(

)))) and evaluate the


function starting from the inner loop.
#include<stdio.h>
#include<conio.h>
#include<math.h>

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<=n;i++)
{
scanf("%f",&a[i]);
}
printf("enter the value of x");
scanf("%f",&x);
sum=a[n]*x;
for(i=n-1;i>=0;i--)
{
sum=(sum+a[i])*pow(x,i);
}
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<stdio.h>
#include<conio.h>
void 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==' ')
{
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<stdio.h>
#include<conio.h>
#include<math.h>
void 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));
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<n; i++)
scanf("%d", &num[i]);
}
float mean(int num[], int n)
{
int i, sum;
float m;
sum=0;
for(i=0; i<n; i++)
sum+=num[i];
m=(float)sum/(float)n;
return(m);
}
float variance(int num[], int n)
{
float var, m, term;
int i;
var=0;
m=mean(num, n);
for(i=0; i<n; i++)
{
term=((float)(num[i])-m)*((float)(num[i])-m);
var+=term/(float)n;
}
return(var);
}
float stddev(int num[], int n)
{
float sd;
sd=variance(num,n);
sd=sqrt(sd);
2return(sd);
}







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( )
{
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 <= 4 ; i++ )
printf ( "%d\t", arr[i] ) ;

for ( i = 1 ; i <= 4 ; i++ )
{
for ( j = 0 ; j < i ; j++ )
{
if ( arr[j] > arr[i] )
{
temp = arr[j] ;
arr[j] = arr[i] ;

for ( k = i ; k > j ; k-- )
arr[k] = arr[k - 1] ;

arr[k + 1] = temp ;
}
}
}

printf ( "\n\nArray after sorting:\n") ;

for ( i = 0 ; i <= 4 ; i++ )
printf ( "%d\t", arr[i] ) ;

}
/*merge sort */

#include <stdio.h>
void merge(int *left,int *right,int *result,int nl,int nr)
{
int i=0,j=0,k=0;
while(nl>0 && nr>0)
{
if(left[i] <= right[j])
{
result[k] = left[i];
i++;
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<6;i++)
printf("\n %d",c[i]);
}


18. Write a program that will generate an array of 1000 integer numbers from 0 to 3000
using the function srand()/rand_r() present in stdlib.h library. The function rand() will
return a pseudo random number from 0 to RAND_MAX which is the maximum value in
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<stdio.h>
#include<string.h>
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<iostream.h>
#include<stdio.h>
#include<string.h>
#include<process.h>
void main()
{
char str[50];
int i,l,f;
cout<<"\n\tProgram to check given string is palindrome or not";
cout<<"\n\tEnter a string:";
gets(str);
l=strlen(str);
for(i=0;i<l/2;i++)
{
if(str[i]!=str[l-i-1])
{
cout<<"\n\tNot palindrome";
exit(0);
}
}
cout<<"\n\tPalindrome";
}


21. Write a program that calculates and displays the reciprocal of an integer, both as a
common fraction and a decimal fraction.
A typical output line would be :
The reciprocal of 2 is 1/2 or 0.500
The program should display a Reciprocal undefined message for an inputof zero.

#include<stdio.h>
Main()
{
int a;
float b;
if(a==0)
{
Printf(reciprocal not possible\n);
}
B=(float)1/a;
Printf(the reciprocal of %d is 1/%d or %f,a,a,b);
}











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<stdio.h>
#include<conio.h>
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<l2;i++)
mainstr[i++]=insstr[i];
}
else
{
for(i=0;i<pos;i++)
temp[i]=mainstr[i];
for(j=0;j<l2;j++,i++)
temp[i]=insstr[j];
for(j=pos;j<l1;j++,i++)
temp[i]=mainstr[j];
temp[i]=NULL;
printf("After insertion the string is:");
puts(temp);
}
}

/*deletion*/
#include <stdio.h>
#include <conio.h>
#include <string.h>
void delchar(char *x,int a, int b);
void main()
{
char string[10];
int n,pos,p;
clrscr();
puts(Enter the string);
gets(string);
printf(Enter the position from where to delete);
scanf(%d,&pos);
printf(Enter the number of characters to be deleted);
scanf(%d,&n);
delchar(string, n,pos);
getch();
}
// Function to delete n characters
void delchar(char *x,int a, int b)
{
if ((a+b-1) <= strlen(x))
{
strcpy(&x[b-1],&x[a+b-1]);
puts(x);
}
}


23. the following
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 <stdio.h>
#include <conio.h>

void main()
{
int arr[10][10];
int i, j, row, col, rowsum, colsum,sumall=0;

/* Function declaration */
int Addrow(int A[10][10], int k, int c);
int Addcol(int A[10][10], int k, int c);

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<row; i++)
{
for(j=0; j< col; j++)
{
scanf("%d", &arr[i][j]);
}
}

printf("Input matrix is\n");
for(i=0; i<row; i++)
{
for(j=0;j<col;j++)
{
printf("%3d", arr[i][j]);
}
printf("\n");
}

/* computing row sum */
for(i=0; i<row; i++)
{
rowsum = Addrow(arr,i,col);
printf("Sum of row %d = %d\n", i+1, rowsum);
}

for(j=0; j<col; j++)
{
colsum = Addcol(arr,j,row);
printf("Sum of column %d = %d\n", j+1, colsum);
}

/* computation of all elements */
for(j=0; j< row; j++)
{
sumall = sumall + Addrow(arr,j,col);
}

printf("Sum of all elements of matrix = %d\n", sumall);

}

/* Function to add each row */
int Addrow(int A[10][10], int k, int c)
{
int rsum=0, i;
for(i=0; i< c; i++)
{
rsum = rsum + A[k][i];
}
return(rsum);
}

/* Function to add each column */
int Addcol(int A[10][10], int k, int r)
{
int csum=0, j;
for(j=0; j< r; j++)
{
csum = csum + A[j][k];
}
return(csum);
}
/*transpose of a matrix */
#include <stdio.h>
#include <conio.h>

void main()
{
int i,j,M,N;
int A[10][10], B[10][10];

int transpose(int A[][10], int r, int c); /*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<M;i++)
{
for(j=0;j<N;j++)
{
scanf("%d",&A[i][j]);
}
}

printf("Matrix A is\n");
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
printf("%3d",A[i][j]);
}
printf("\n");
}

/* Finding Transpose of matrix*/
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
B[i][j] = A[j][i];
}
}

printf("Its Transpose is\n");
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
printf("%3d",B[i][j]);
}
printf("\n");
}

} /*End of main()*/



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<stdio.h>
#include<conio.h>
struct 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<n; i++)
{
printf("\nRecord no: %d\n", (i+1));
printf("Enter name: ");
scanf("%s", s[i].name);
printf("Enter roll no: ");
scanf("%d", &s[i].rollno);
printf("Enter the marks for test 1, test 2 and test 3: ");
for(j=0; j<3; j++)
scanf("%d", &s[i].marks[j]);
}
printf("Enter the roll no being searched for \n");
scanf("%d", &key);
pos=-1;
for(i=0; i<n; i++)
{
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<3; i++)
{
printf("Marks for test %d: %d\n", (i+1),
s[pos].marks[i]);
}
}
printf("\n");
for(i=0; i<3; i++)
{
avg[i]=0.0;
high[i]=s[0].marks[i];
for(j=0; j<n; j++)
{
avg[i]+=(float)s[j].marks[i];
if(high[i]<s[j].marks[i])
{
high[i]=s[j].marks[i];
}
}
avg[i]=avg[i]/(float)n;
}
for(i=0; i<3; i++)
printf("The average of test %d is %f\n", (i+1), avg[i]);
printf("\n");
for(i=0; i<3; i++)
printf("Highest in test %d is %d\n", (i+1), high[i]);
getch();
}




25. The total distance travelled by vehicle in t seconds is given by distance = ut+1/2at
2

where u and a are the initial velocity (m/sec.) and acceleration (m/sec
2
). Write C
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 <stdio.h>
#include <math.h>
void main()
{
int tim_intrval, counter,time;float accl, distance=0, velos;
clrscr();
printf("<===========PROGRAM FOR CALC TOTAL DISTANCE TRAVELED BY
AVECHIAL===========>");
printf("\n\n\n\t\t\tNO OF TIME INTERVALS : ");
scanf("%d",&tim_intrval);
for(counter = 1; counter <= tim_intrval; counter++)
{
printf("\n\t\t\tAT T%d TIME(sec) : ",counter);
scanf("%d",&time);
printf("\t\t\tVELOCITY AT %d sec (m/sec) : ",time);
scanf("%f",&velos);
printf("\t\t\tACCLERATION AT %d sec (m/sec^2): ",time);
scanf("%f",&accl);
distance += (velos*time + (accl*pow(time,2))/2);
}
printf("\n\n\n\tTOTAL DISTANCE TRAVELLED BY VEHICLE IN %d
INTERVALS OFTIME : %f",tim_intrval,distance);
}

26. Write a C program to find whether a given number is prime or not. Output the given
number with suitable message (using looping constructs).
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

void main()
{
int n,i;
clrscr();
printf("enter the number\n");
scanf("%d",&n);
for(i=2;i<n;i++)
{
if(n%i==0)
{
printf("number is not prime");
getch();
exit(0);
}
}
printf("number is prime");

getch();
}



27. Write a C program to generate Pascal's triangle and Flyods Triangle.
/*floyds triangle*/
# include <stdio.h>
# include <conio.h>
void main()
{
int r, i, j, c = 1 ;
clrscr() ;
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 <= i ; j++)
{
printf("%-6d", c) ;
c++ ;
}
printf("\n\n") ;
}
getch() ;
}

/*pascals triangle*/

# include <stdio.h>
# include <conio.h>
void main()
{
int b, p, q, r, x ;
clrscr() ;
b = 1 ;
q = 0 ;
printf("Enter the number of rows : ") ;
scanf("%d", &p) ;
printf("\nPascal's triangle is : \n\n") ;
while (q < p)
{
for(r = 40 - 3 * q ; r > 0 ; --r)
printf(" ") ;
for(x = 0 ; x <= q ; ++x)
{
if((x == 0) || (q == 0))
b = 1 ;
else
b = (b * (q - x + 1)) / x ;
printf("%6d", b) ;
}
printf("\n\n") ;
++q ;
}
getch() ;
}


28. Write a C program to construct a pyramid of numbers.
#include <stdio.h>
#include <conio.h>
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<stdio.h>
#include<conio.h>
void main( )
{
int r, c, num=1 ,len;
clrscr( );
printf("Enter the required no. of rows:");
scanf("%d", &len);
for( r=1; r<=len; r++ )
{
printf("\n");
for(c=1 ; c<=r; c++)
{
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<stdio.h>
#include<math.h>
void 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 First
Number:");
scanf("%lf",&w1.realpart);
printf("\n Imaginary Part of First number:");
scanf("%lf",&w1.imgpart);
printf("\n Real Part of Second Number:");
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);
else
printf("\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<stdio.h>
#include<conio.h>
#define MaxN 90
void 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<=n; i++)
scanf("%f%f", &arr_x[i], &arr_y[i]);
printf("Enter the value of x at which value of y is to be
calculated: ");
scanf("%f", &x);
for (i=0; i<=n; i++)
{
numerator=1;
denominator=1;
for (j=0; j<=n; j++)
if(j!=i)
{
numerator *= x-arr_x[j];
denominator *= arr_x[i]-arr_x[j];
}
y+=(numerator/denominator)*arr_y[i];
}
printf("When x=%4.1f y=%7.1f\n",x,y);
getch();
}

Newton-Gregory forward interpol ati on. :

#include<stdio.h>
#include<conio.h>
#define MaxN 100
#define Order_of_diff4
void main ()
{
float arr_x[MaxN+1], arr_y[MaxN+1], numerator=1.0,
denominator=1.0, x, y, p, h,diff_table[MaxN+1][Order_of_diff+1];
int i,j,n,k;
clrscr();
printf("Enter the value of n \n");
scanf("%d",&n);
printf("Enter the values of x and y");
for(i=0; i<=n; i++)
scanf("%f%f", &arr_x[i], &arr_y[i]);
printf("Enter the value of x at which value of y is to be
calculated");
scanf("%f", &x);
h=arr_x[1]-arr_x[0];
for(i=0; i<=n-1; i++)
diff_table[i][1]=arr_y[i+1]-arr_y[i];
/*Creating the difference table and calculating first
orderdifferences*/
for(j=2; j<=Order_of_diff; j++)
/*Calculating higher order differences*/
for(i=0; i<=n-j; i++)
diff_table[i][j]=diff_table[i+1][j-1] -diff_table[i][j-1];
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<=Order_of_diff; k++)
{
numerator *=p-k+1;denominator *=k;
y +=(numerator/denominator)*diff_table[i][k];
}
printf("When x=%6.1f, y=%6.2f\n",x, y);
getch();
}

You might also like