Programs C Programming Engineering Notes
Programs C Programming Engineering Notes
1. Design and develop a flowchart or an algorithm that takes three coefficients (a, b, and c) of a
Quadratic equation (ax2+bx+c=0) as input and compute all possible roots. Implement a C program for
the developed flowchart/algorithm and execute the same to output the possible roots for a given set of
coefficients with appropriate messages.
#include<stdio.h>
#include<math.h>
void main()
{
float a,b,c,d,x1,x2;
printf("Enter the non zero co-efficient for a,b&c\n");
scanf("%f%f%f",&a,&b,&c);
d=(b*b)-(4*a*c);
if(d==0)
{
x1=x2=(-b)/(2*a);
printf("Roots are equal x1 = x2 = %f\n",x1);
}
else if(d>0)
{
printf("roots are real and distinct\n");
x1=(-b)/(2*a)+sqrt(d)/(2*a);
x2=(-b)/(2*a)-sqrt(d)/(2*a);
printf("root1=%f\n\nroot2=%f\n\n",x1,x2);
}
else
{
printf("roots are imaginary\n");
x1=(-b)/(2*a);
x2=sqrt(fabs(d))/(2*a);
printf("root1=%f+i%f\n",x1,x2);
printf("root2=%f-i%f\n",x1,x2);
}
}
2. Design and develop an algorithm to find the reverse of an integer number NUM and check whether it
is PALINDROME or NOT. Implement a C program for the developed algorithm that takes an integer
number as input and output the reverse of the same with suitable messages. Ex: Num: 2014, Reverse:
4102, Not a Palindrome.
#include<stdio.h>
void main()
{
int rev=0,n,temp,rem;
}
3a. Design and develop a flowchart to find the square root of a given number N. Implement a C program
for the same and execute for all possible inputs with appropriate messages.
Note: Don’t use library function sqrt(n).
#include<stdio.h>
void main()
{
float n,x,root;
int i;
3b. Design and develop a C program to read a year as an input and find whether it is leap year or not.
Also consider end of the centuries.
#include<stdio.h>
void main()
{
int year;
printf("enter a year:\n");
scanf("%d",&year);
else
printf("%d is not a leap year\n",year);
4. Design and develop an algorithm to evaluate polynomial f(x) = a4x4 + a3x3 + a2x2 + a1x + a0, for a
given value of x and its coefficients using Horner’s method. Implement a C program for the same and
execute the program with different set of values of coefficients and x.
#include<stdio.h>
void main()
{
int sum;
int x,i,n,a[20];
printf("enter the degree of polynomial\n");
scanf("%d",&n);
printf("enter the co-efficients of the polynomial\n");
for(i=0;i<=n;i++)
{
scanf("%d",&a[i]);
}
printf("enter the values of x\n");
scanf("%d",&x);
sum=a[n];
for(i=n-1;i>0;i--)
sum=(sum+a[i])*x;
sum=sum+a[0];
printf("\n the value of polynomial is %d\n",sum);
getch();
}
5. Draw the flowchart and Write a C Program to compute Sin(x) using Taylor series approximation
given by Sin(x) = x - (x3/3!) + (x5/5!) - (x7/7!) + ……. Compare your result with the built- in Library
function. Print both the results with appropriate messages.
#include<stdio.h>
#include<math.h>
#define pi 3.1426
void main()
{
int n,i;
float degree,x,term,sum;
printf("\n enter the value of n\n");
scanf("%d",&n);
printf("enter degrees\n");
scanf("%f",°ree);
x=degree*pi/180;
term=x;
sum=term;
for(i=3;i<=n;i+=2)
{
term=-term*x*x/(i*(i-1));
sum=sum+term;
}
printf("sin(%f)=%f\n",degree,sum);
printf("using library functions \n");
printf("sin(%f)=%f\n",degree,sin(x));
getch();
}
6. Develop an algorithm, implement and execute a C program that reads N integer numbers and arrange
them in ascending order using Bubble Sort.
#include<stdio.h>
void main()
{
int a[20],i,j,n,temp;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\n elements after sorting are\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
}
7. Develop, implement and execute a C program that reads two matrices A (m x n ) and B (p x q ) and
Compute product of matrices A and B. Read matrix A and matrix B in row major order and in column
major order respectively. Print both the input matrices and resultant matrix with suitable headings and
output should be in matrix format only. Program must check the compatibility of orders of the matrices
for multiplication. Report appropriate message in case of incompatibility.
#include<stdio.h>
#include<stdlib.h>
void main()
{
int a[20][20],b[20][20],c[20][20],i,j,m,n,p,q,k;
8. Develop, implement and execute a C program to search a Name in a list of names using Binary
searching Technique.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char names[10][10], key[10];
int i,j,n,low,high,mid,res;
void main()
{
char src[20],dest[20];
int i;
printf("enter the source string\n");
scanf("%s",src);
i=0;
while(src[i]!='\0')
{
dest[i]=src[i];
i++;
}
dest[i]='\0';
printf("\n source string is %s",src);
printf("\n destination string is %s",dest);
}
ii. Read a sentence and print frequency of vowels and total count of consonants.
#include<stdio.h>
void main()
{
char str[80],ch;
int i,vc=0,cc=0;
printf("enter a sentence\n");
scanf("%s",str);
i=0;
while(str[i]!='\0')
{
ch=tolower(str[i]);
if(isalpha(ch))
{
if((ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'))
{
vc++;
}
else
{
cc++;
}
}
i++;
}
printf("No. of vowels in %d\n",vc);
printf("No. of consonants in %d\n",cc);
}
10. a. Design and develop a C function RightShift(x ,n) that takes two integers x and n as input and
returns value of the integer x rotated to the right by n positions. Assume the integers are unsigned. Write
a C program that invokes this function with different values for x and n and tabulate the results with
suitable headings.
#include<stdio.h>
unsigned int rightrot(unsigned int x,int n)
{
int i;
for(i=1;i<=n;i++)
{
if(x%2==0)
x=x>>1;
else
x=(x>>1)+32768;
}
return x;
}
void main()
{
unsigned int x,res;
int n;
b. Design and develop a C function isprime(num) that accepts an integer argument and returns 1 if the
argument is prime, a 0 otherwise. Write a C program that invokes this function to generate prime
numbers between the given range.
#include<stdio.h>
void main()
{
int m,n,i,flag=0;
}
11. Draw the flowchart and write a recursive C function to find the factorial of a number, n!, defined by
fact(n)=1, if n=0. Otherwise fact(n)=n*fact(n-1). Using this function, write a C program to compute the
binomial coefficient nCr. Tabulate the results for different values of n and r with suitable messages.
#include<stdio.h>
int fact(int n)
{
if(n==0)
return 1;
else
return (n*fact(n-1));
}
void main()
{
int n,r;
float ncr;
clrscr();
printf("enter the value of n and r\n");
scanf("%d%d",&n,&r);
ncr=(float)fact(n)/(fact(n-r)*fact(r));
printf("Binomial Coefficient of %d C%d=%f\n",n,r,ncr);
}
12. Given two university information files “studentname.txt” and “usn.txt” that contains students
Name and USN respectively. Write a C program to create a new file called “output.txt” and copy the
content of files “studentname.txt” and “usn.txt” into output file in the sequence shown below . Display
the contents of output file “output.txt” on to the screen.
#include<stdio.h>
#include<stdlib.h>
void main()
{
FILE *fp1,*fp2,*fp3;
char name[20];
int usn;
fp1=fopen("name.txt","r");
fp2=fopen("usn.txt","r");
fp3=fopen("output.txt","w");
for(;;)
{
if(fscanf(fp1,"%s",name)>0)
{
if(fscanf(fp2,"%d",&usn)>0)
{
fprintf(fp3,"%s %d\n",name,usn);
}
else break;
}
else break;
}
fclose(fp1);
fclose(fp2);
fclose(fp3);
fp3=fopen("output.txt","r");
printf("\n...............\n");
printf("NAME\tUSN\n");
printf("\n...............\n");
while(fscanf(fp3,"%s %d\n",name, &usn)>0)
{
printf("%s \t%d\n",name,usn);
}
fclose(fp3);
13. Write a C program to maintain a record of n student details using an array of structures with four
fields (Roll number, Name, Marks, and Grade). Assume appropriate data type for each field. Print the
marks of the student, given the student name as input.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct student
{
int rollno,marks;
char name[20];
char grade[2];
};
void main()
{
int i,n;
struct student s[10];
char key[20];
clrscr();
printf("enter the number\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the %d student details\n",i+1);
printf("enter the roll number:\n");
scanf("%d",&s[i].rollno);
printf("enter the student name:\n");
scanf("%s",s[i].name);
printf("enter the marks:\n");
scanf("%d",&s[i].marks);
printf("enter the grade:\n");
scanf("%s",s[i].grade);
}
14. Write a C program using pointers to compute the sum, mean and standard deviation of all elements
stored in an array of n real numbers.
#include<stdio.h>
#include<math.h>
void main()
{
int n,i;
float a[20],sum,mean, var, sd;
sum=0;
for(i=0;i<n;i++)
sum=sum+ *(a+i);
printf(“\n sum=%f”,sum);
mean=sum/n;
sum=0;
for(i=0;i<n;i++)
sum=sum+(*(a+i)-mean)*(*(a+i)-mean);
var=sum/n;
sd=sqrt(var);
printf("\n Mean=%f \n Variance=%f \n Std
Dev=%f",mean,var,sd);