0% found this document useful (0 votes)
36 views

C Programs For Turbo C - 240516 - 093056

Uuuu

Uploaded by

rafathfirdous217
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

C Programs For Turbo C - 240516 - 093056

Uuuu

Uploaded by

rafathfirdous217
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Principles of programming using C Lab(BPOPS103/203)

Program 1
Simple Calculator
1. Simulation of a Simple Calculator.
#include <stdio.h>
#include<conio.h>
int main()
{
int a, b, op;
clrscr();
printf(" 1.Addition\n 2.Subtraction\n 3.Multiplication\n 4.Division\n 5.Remainder\n");
printf("Enter the values of a & b: ");
scanf("%d %d",&a,&b);
printf("Enter your Choice : ");
scanf("%d",&op);
switch(op)
{
case 1 :
printf("Sum of %d and %d is : %d",a,b,a+b);
break;
case 2 :
printf("Difference of %d and %d is : %d",a,b,a-b);
break;
case 3 :
printf("Multiplication of %d and %d is : %d",a,b,a*b);
break;
case 4 :
printf("Division of Two Numbers is %d : ",a/b);
break;
case 5:
printf("Remainder of %d and %d is : %d",a,b,a%b);
break;
default :
printf(" Enter Your Correct Choice.");
break;
}
getch();
return 0;
}
Output:

Prof. Bhagyashree K,Dept Of CSE,BrCE


Principles of programming using C Lab(BPOPS103/203)

Enter the values of a & b:


5
5
Enter your choice
1
Sum of a and b is : 10

Prof. Bhagyashree K,Dept Of CSE,BrCE


Principles of programming using C Lab(BPOPS103/203)

Program 2
Quadratic equation
2. Develop a program to compute the roots of a quadratic equation by accepting the
coefficients. Print the appropriate messages.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<conio.h>
void main()
{
float a,b,c,disc;
float root1,root2,realp,imagp;
printf("enter values of a,b,c\n");
scanf("%f%f%f',&a,&b,&c);
if(a==0 && b==0 && c==0)
{
printf("roots cannot be determined\n");
exit(0);
}
else
{
disc=b*b-4*a*c;
if(disc>0)
{
root1=(-b+sqrt(disc))/(2*a);
root2=(-b-sqrt(disc))/(2*a);
printf("roots are real and distinct\n");
printf("root1=%f\n",root1);
printf("root2=%f\n",root2);
}
else if(disc==0)
{
root1=-b/(2*a);
root2=root1;

Prof. Bhagyashree K,Dept Of CSE,BrCE


Principles of programming using C Lab(BPOPS103/203)

printf(“roots are equal\n”);


printf(“root1=%f\n”,root1);
printf(“root2=%f\n”,root2);
}
else if(disc<0)
{
realp=-b/(2*a);
imagp=sqrt(abs(disc)/(2*a));
printf("roots are complex\n");
printf("root1=%f+i%f\n",realp,imagp);
printf("root2=%f-i%f\n",realp,imagp);
}
getch();
return 0;
}

Output:
C PROGRAMMING -Output

• Enter the values of a, b, c


o 1
o 2
o 1
o roots are real and equal
o rOOt1=-1.000000
o root2=-1.000000
• Enter the values of a, b, c
o 2.3
o 4
o 5.6
o roots are complex
o roots1=-0.869565 + i2.758386 o roots2=-0.869565 - i2.758386
• Enter values of a, b, c
0

Prof. Bhagyashree K,Dept Of CSE,BrCE


Principles of programming using C Lab(BPOPS103/203)

0
0
o roots cannot be determined

Prof. Bhagyashree K,Dept Of CSE,BrCE


Principles of programming using C Lab(BPOPS103/203)

Program 3
Electricity bill generation
3. An electricity board charges the following rates for the use of electricity: for the first 200 units 80
paise per unit: for the next 100 units 90 paise per unit: beyond 300 units rupees 1 per unit. All users
are charged a minimum of rupees 100 as a meter charge. If the total amount is more than Rs 400,
then an additional surcharge of 15% of the total amount is charged. Write a program to read the
name of the user, the number of units consumed, and print out the charges.

#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
int cust_no, unit_con;
float charge,surcharge=0, amt, total_amt;
char nm[25];
printf("Enter the customer IDNO :\t");
scanf("%d",&cust_no);
printf("Enter the customer Name :\t");
scanf("%s",nm);
printf("Enter the unit consumed by customer :\t");
scanf("%d",&unit_con);
if (unit_con <200 )
charge = 0.80;
else if (unit_con>=200 && unit_con<300)
charge = 0.90;
else
charge = 1.00;
amt = unit_con*charge;
if (amt>400)
surcharge = amt*15/100.0;

Prof. Bhagyashree K,Dept Of CSE,BrCE


Principles of programming using C Lab(BPOPS103/203)

total_amt = amt+surcharge;
printf("\t\t\t\nElectricity Bill\n\n");
printf("Customer IDNO :\t%d",cust_no);
printf("\nCustomer Name :\t%s",nm);
printf("\nunit Consumed :\t%d",unit_con);
printf("\nAmount Charges @Rs. %4.2f per unit :\t%0.2f",charge,amt);
printf("\nSurchage Amount :\t%.2f",surcharge);
printf("\nMinimum meter charge Rs :\t%d",100);
printf("\nNet Amount Paid By the Customer :\t%.2f",total_amt+100);
getch();
return 0;

Output:
 Enter the customer IDNO:
o 002
 Enter the customer Name:
o Abhi
 Enter the unit consumed by customer:
o 493
Electricity Bill

 Customer IDNO: 2
 Customer Name: Abhi
 Unit Consumed: 493
 Amount Charges @Rs. 1.00 per cent unit: 493.00
 Surcharge Amount: 73.95
 Minimum meter charge Rs: 100
 Net Amount Paid by the customer: 666.95

Prof. Bhagyashree K,Dept Of CSE,BrCE


Principles of programming using C Lab(BPOPS103/203)

Program 4
Pattern printing

4. Write a C Program to display the following by reading the number of rows as input,
1
121
12321
1234321
---------------------------

#include <stdio.h>
#include<conio.h>
main()
{
int i,j,k,l,n=5;
for(i=1;i<=n;i++)
{
for(k=n;k>=i;k--)
printf(“ “);
for(j=1;j<=i;j++)
printf(“%d”,j);
for(l=j-2;l>0;l--)
printf(“%d”,l);
printf(“/n”);

}
getch();
return 0;
}

Output:
1
121
12321
1234321
---------------------------

Prof. Bhagyashree K,Dept Of CSE,BrCE


Principles of programming using C Lab(BPOPS103/203)

Program 5
Binary Search
5. Implement Binary Search on Integers.

#include <stdio.h>
#include<conio.h>
int main()
{
int c, first, last, middle, n, search, array[100];

printf("Enter number of elements\n");


scanf("%d", &n);

printf("Enter %d integers\n", n);

for (c = 0; c < n; c++)


scanf("%d", &array[c]);

printf("Enter value to find\n");


scanf("%d", &search);

first = 0;
last = n - 1;
middle = (first+last)/2;

while (first <= last) {


if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.\n", search, middle+1);
break; }
else
last = middle - 1;

middle = (first + last)/2;


}
if (first > last)
printf("Not found! %d isn't present in the list.\n", search);

getch();

return 0;

Prof. Bhagyashree K,Dept Of CSE,BrCE


Principles of programming using C Lab(BPOPS103/203)

Output:

Enter number of elements


7
Enter 7 integers
-4
5
8
9
11
43
485
Enter value to find
11
11 found at location 5

Prof. Bhagyashree K,Dept Of CSE,BrCE


Principles of programming using C Lab(BPOPS103/203)

Program 6
Matrix Multiplication
6. Implement Matrix multiplication and validate the rules of multiplication.

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int main(){
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
system("cls");
printf("enter the number of row=");
scanf("%d",&r);
printf("enter the number of column=");
scanf("%d",&c);
printf("enter the first matrix element=\n");
for(i=0;i<r;i++) {
for(j=0;j<c;j++) {
scanf("%d",&a[i][j]);
} }
printf("enter the second matrix element=\n");
for(i=0;i<r;i++) {
for(j=0;j<c;j++) {
scanf("%d",&b[i][j]);
} }
printf("multiply of the matrix=\n");
for(i=0;i<r;i++) {
for(j=0;j<c;j++) {
mul[i][j]=0;
for(k=0;k<c;k++) {
mul[i][j]+=a[i][k]*b[k][j];
}
} }
//for printing result
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
getch();
return 0;
}

Prof. Bhagyashree K,Dept Of CSE,BrCE


Principles of programming using C Lab(BPOPS103/203)

Output:
enter the number of row=3
enter the number of column=3
enter the first matrix element=
111
222
333
enter the second matrix element=
111
222
333
multiply of the matrix=
666
12 12 12
18 18 18

Prof. Bhagyashree K,Dept Of CSE,BrCE


Principles of programming using C Lab(BPOPS103/203)

Program 7
Compute Sine and Cosine of an Angle
7. Compute sin(x)/cos(x) using Taylor series approximation. Compare you result with the built in
library function. Print both the results with appropriate inferences.

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<conio.h>
int fact(int m);
void main()
{
int x,n,i;
float rad, res, sum=0;
printf("Enter degree\n");
scanf("%d",&x);
printf("Enter number of terms\n");
scanf("%d",&n);
rad=x*3.14/180;
for(i=1;i<=n;i+=2)
{
if ((i-1)%4==0)
sum=sum+pow(rad,i)/fact(i);
else
sum=sum-pow(rad,i)/fact(i);
}
printf("Calculate sin(%d) = %f", x,sum);
printf("\nLibrary sin(%d) = %f", x,sin(rad));
}
int fact(int m)
{
int i,f=1;
for(i=1;i<=m;i++)
{
f=f*i;
}
return f;
}

Output
Enter degree

30

Prof. Bhagyashree K,Dept Of CSE,BrCE


Principles of programming using C Lab(BPOPS103/203)

Enter the number of terms

Calculate sin(30) = 0.523333

Library sin(30) = 0.499770

Prof. Bhagyashree K,Dept Of CSE,BrCE


Principles of programming using C Lab(BPOPS103/203)

Program 8
Bubble Sort
8. Sort the given set of N numbers using Bubblesort.

#include<stdio.h>
#include<conio.h>
int main()

{
int i,j,n,temp;
int a[20];
printf("enter the value of n");
scanf("%d",&n);
printf("Enter the numbers in unsorted order:\n");
for(i=0;i<n;i++)
scanf("%d", &a[i]);
// bubble sort logic
for(i=0;i<n;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("The sorted array is\n");


for(i=0;i<n;i++)

{
printf("%d\n",a[i]);

getch();

return 0;

Prof. Bhagyashree K,Dept Of CSE,BrCE


Principles of programming using C Lab(BPOPS103/203)

Output:

Enter the value of n


4
Enter the numbers one by one in unsorted order:
2

3
5
4
The sorted array is

2
3
4
5

Prof. Bhagyashree K,Dept Of CSE,BrCE


Principles of programming using C Lab(BPOPS103/203)

Program 9
String Operations
9. Write functions to implement string operations such as compare, concatenate, string length.
Convince the parameter passing techniques.

#include<stdio.h>
#include<stdlib.h>
int fnMyStrCmp(const char*, const char*);
void fnMyStrCat(char*, const char*);
int fnMyStrLen(const char*);
#include<conio.h>
int main()
{
int iChoice;
char acStr1[30], acStr2[30];
int iLen;
printf("\n=====================\n");
printf("STRING OPERATIONS");
printf("\n=====================\n");
for(;;)
{
printf("\nEnter two strings\n");
printf("\nString 1 : "); scanf("%s", acStr1);
printf("\nString 2 : "); scanf("%s", acStr2);
printf("\n1.String Compare\n2.String Concatenate\n3.String Length");
printf("\nEnter your choice : "); scanf("%d", &iChoice);
switch(iChoice)
{
case 1: if(fnMyStrCmp(acStr1, acStr2) == 0)
printf("\nTwo strings are equal");
else if(fnMyStrCmp(acStr1, acStr2) > 0)
printf("\nString %s is greater than String %s",
acStr1,acStr2);
else
printf("\nString %s is greater than String %s",
acStr2,acStr1);
break;
case 2: fnMyStrCat(acStr1, acStr2);
printf("\nConcatenated String is\n%s", acStr1);
break;
case 3: iLen = fnMyStrLen(acStr1);
printf("\nLength of String %s is %d\n", acStr1, iLen);
iLen = fnMyStrLen(acStr2);

Prof. Bhagyashree K,Dept Of CSE,BrCE


Principles of programming using C Lab(BPOPS103/203)

printf("\nLength of String %s is %d\n", acStr2, iLen);


break;

}
printf("\nPress 1 to continue and 0 to quit : "); scanf("%d", &iChoice);
if(0==iChoice)
{
break;
}
}
return 0;
}
int fnMyStrCmp(const char *s1, const char *s2)
{
int k;
for(k=0; s1[k] == s2[k] && s1[k]!='\0'&& s2[k]!='\0'; k++);
if( k==(fnMyStrLen(s1)) && k==(fnMyStrLen(s2)) )
{
return 0;
}
else if(s1[k] > s2[k])
{
return 1;
}
else
{
return -1;
}
}
void fnMyStrCat(char *dest, const char *src)
{
int dest_len, i;
dest_len = fnMyStrLen(dest);
for (i = 0 ; src[i] != '\0'; i++)

dest[dest_len + i] = src[i];
dest[dest_len + i] = '\0';
}
int fnMyStrLen(const char *str)
{
int iLen;
for(iLen=0; str[iLen] != '\0'; iLen++);
return iLen;
}

Prof. Bhagyashree K,Dept Of CSE,BrCE


Principles of programming using C Lab(BPOPS103/203)

Output:

STRING OPERATIONS =====================

Enter two strings

String 1 : shiva

String 2 : shankar

1.String Compare
2.String Concatenate
3.String Length
Enter your choice : 2

Concatenated String is shivashankar


Press 1 to continue and 0 to quit : 1

Prof. Bhagyashree K,Dept Of CSE,BrCE


Principles of programming using C Lab(BPOPS103/203)

Program 10
C Structures
11. Implement structures to read, write, compute average- marks and the students scoring above
and below the average marks for a class of N students

#include<stdio.h>
#include<conio.h>
struct student
{
char usn[10];
char name[10];
float m1,m2,m3;
float avg,total;
};
void main()
{
struct student s[20];
int n,i;
float tavg,sum=0.0;
printf("Enter the number of student=");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the detail of %d students\n",i+1);
printf("\n Enter USN=");
scanf("%s",s[i].usn);
printf("\n Enter Name=");
scanf("%s",s[i].name);
printf("Enter the three subject score\n");
scanf("%f%f%f",&s[i].m1,&s[i].m2,&s[i].m3);
s[i].total=s[i].m1+s[i].m2+s[i].m3;
s[i].avg=s[i].total/3;
}
for(i=0;i<n;i++)
{
if(s[i].avg>=35)
printf("\n %s has scored above the average marks",s[i].name);
else
printf("\n %s has scored below the average marks",s[i].name);
}
getch();
return 0;
}

Prof. Bhagyashree K,Dept Of CSE,BrCE


Principles of programming using C Lab(BPOPS103/203)

Output:
Enter the number of students

 Enter the details of 1 students


oEnter USN = 100
oEnter the name = Rude
oEnter the three subject score = 10 21 15
 Enter the details of 1 students
o Enter USN = 222
o Enter the name = Krish
o Enter the three subject score = 11 9 10
Rude has scored above the average marks

Krish has scored below the average marks

Prof. Bhagyashree K,Dept Of CSE,BrCE


Principles of programming using C Lab(BPOPS103/203)

Program 11
Pointers and Arrays
12. Develop a 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>
#include<conio.h>
int main(){
int n , i;
float x[20],sum,mean;
float variance , deviation;
printf("Enter the value of n \n");
scanf("%d",&n);
printf("enter %d real values \n",n);
for (i=0;i<n;i++){
scanf("%f",(x+i));
}
sum=0;
for(i=0;i<n;i++){
sum= sum+*(x+i);
}
printf("sum=%f\n",sum);
mean=sum/n;
sum=0;
for(i=0;i<n;i++)
{
sum=sum+(*(x+i)-mean)*(*(x+i)-mean);
}
variance=sum/n;

Prof. Bhagyashree K,Dept Of CSE,BrCE


Principles of programming using C Lab(BPOPS103/203)

deviation=sqrt(variance);
printf("mean(Average)=%f\n",mean);
printf("variance=%f\n",variance);
printf("standard deviation=%f\n",deviation);
getch();
return 0;
}

Output:

Enter the value of n

Enter the 5 real values

23

Sum = 38.0000

Mean ( Average ) = 7.6000

Variance = 63.039997

Standard deviation = 7.9397

Prof. Bhagyashree K,Dept Of CSE,BrCE


Principles of programming using C Lab(BPOPS103/203)

Program 12
Copy a text file to another
12. Write a C program to copy a text file to another, read both the input file name and target file name.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

int main(void)

FILE *fp1,*fp2; int ch; char fname1[100], fname2[100];

printf("\nEnter File name to be copied\n");

scanf("%s",fname1);

fp1 = fopen(fname1,"r");

if(fp1 == NULL)

printf("\nInput File %s doesn't exist\n", fname1);

exit(0);

printf("\nEnter target File name\n");

scanf("%s",fname2);

fp2 = fopen(fname2,"w");

while((ch=fgetc(fp1)) != EOF)

fputc(ch,fp2);

printf("\nFile %s successfully created\n",fname2);

Prof. Bhagyashree K,Dept Of CSE,BrCE


Principles of programming using C Lab(BPOPS103/203)

fclose(fpl);

fclose(fp2);

return 0;

Output

Enter File name to be copied


out9.c

Enter target File name


out99.c

File out99.c successfully created

Prof. Bhagyashree K,Dept Of CSE,BrCE

You might also like