[go: up one dir, main page]

0% found this document useful (0 votes)
60 views14 pages

C Popupdated

The document contains 12 code snippets demonstrating various programming concepts in C language. The code snippets include programs to: 1) Simulate a simple calculator 2) Compute the roots of a quadratic equation 3) Calculate electricity bill based on units consumed 4) Display patterns using loops 5) Perform binary search on integers 6) Multiply matrices and validate rules of multiplication 7) Compute trigonometric functions using Taylor series 8) Sort numbers using bubble sort algorithm 9) Implement string operations using functions 10) Read and compute student marks using structures 11) Calculate sum, mean, standard deviation of array elements using pointers 12) Copy contents from one text file to another

Uploaded by

aarzu qadri
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)
60 views14 pages

C Popupdated

The document contains 12 code snippets demonstrating various programming concepts in C language. The code snippets include programs to: 1) Simulate a simple calculator 2) Compute the roots of a quadratic equation 3) Calculate electricity bill based on units consumed 4) Display patterns using loops 5) Perform binary search on integers 6) Multiply matrices and validate rules of multiplication 7) Compute trigonometric functions using Taylor series 8) Sort numbers using bubble sort algorithm 9) Implement string operations using functions 10) Read and compute student marks using structures 11) Calculate sum, mean, standard deviation of array elements using pointers 12) Copy contents from one text file to another

Uploaded by

aarzu qadri
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/ 14

PART A

1. Simulation of a simple calculator.

#include<stdio.h>
#include<stdlib.h>
main()
{
float a,b,result;
char op;
printf("Enter mathematical Expression\n");
scanf("%f%c%f",&a,&op,&b);
switch(op)
{
case '+':result=a+b;
break;
case '-':result=a-b;
break;
case '*':result=a*b;
break;
case '/':result=a/b;
break;
case '%':result=(int)a%(int)b;
break;
default:printf("Enter proper Expression\n");
exit(0);
}
printf("The result is %f",result);
}
2. Develop a program to compute the roots of a quadratic equation by accepting the
coefficients. Print appropriate messages.
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
main()
{
float a,b,c,disc;

printf("Enter a , b and c values\n");


scanf("%f%f%f",&a,&b,&c);
if(a==0)
{
printf("invalid input\n");
exit(0);
}
disc=b*b-4*a*c;
if (disc==0)
{
printf("Roots are real and equal\n");
printf("Root1=Root2=%f \n",-b/(2*a));
}
else
if(disc>0)
{
printf("Roots are real and distinct\n");
printf("Root1= %f ",(-b+sqrt(disc))/(2*a));
printf("Root2= %f ",(-b-sqrt(disc))/(2*a));
}
else
{
printf("Roots are Imaginary\n");
printf("Root1= %f+i%f \n ",-b/(2*a),sqrt(fabs(disc))/(2*a));
printf("Root1= %f-i%f \n ",-b/(2*a),sqrt(fabs(disc))/(2*a));
}
}
3. An electricity board charges the following rates for the use of electricity:
For the First 200 units: 80 P per unit
For the Next 100 units: 90 P per unit
Beyond 300 units: Rs. 1.00 per unit
All users are charged a minimum of Rs. 100 as meter charge. If the total amount is more
than Rs. 400, then an additional surcharge of 15% of total amount is charged. Write a
program to read the name of the user and number of units consumed and print out the
charges

#include<stdio.h>
main()
{
char name[20];
int units;
float amount;
printf("Enter the consumer's name\n");
scanf("%s",name);
printf("Enter the consumed units\n");
scanf("%d",&units);

if(units<=200)
amount=units*0.80;
else if (units<=300)
amount=(units-200)*0.90+160;
else
amount=units-50;

amount=amount+100;
if(amount>400)
amount=1.15*amount;

printf("\n Electricity Bill of %s is %f \n",name,amount);


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

#include<stdio.h>
void main()
{
int i,j,space,rows;
printf("Input number of rows :");
scanf("%d",&rows);
for(i=1; i<=rows; i++)
{
/* print blank spaces */
for(space = 1;space<= rows - i; space++)
printf(" ");
/* Display number in ascending order upto
middle*/
for(j=1;j<=i;j++)
printf("%d",j);
/* Display number in reverse order after middle */
for(j=i-1;j>=1;j--)
printf("%d",j);
printf("\n");
}
}
5. Implement a Binary search on integers.

#include<stdio.h>
#include<stdlib.h>
main()
{
int i,a[10],n,key,low,mid,high;
printf("\n enter the size of array\n");
scanf("%d",&n);
printf("\n enter the elements in ascending order\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n enter the number to be searched \n");
scanf("%d",&key);
low=0;
high=n-1;
while(low<=high)
{
mid=(high+low)/2;
if(key==a[mid])
{
printf("\n The number is found at %d position",mid+1);
exit(0);
}
if(key<a[mid])
high=mid-1;
else
low=mid+1;
}
printf("\n the number is not found \n");
}
6. Implement Matrix multiplication and validate the rules of multiplication.

#include <stdio.h>
#include<stdlib.h>
main()
{
int a[10][10],b[10][10],c[10][10];
int m,n,p,q,i,j,k;

printf("\n Enter the order of the matrix A :");


scanf("%d%d",&m,&n);
printf("\n Enter the elements of matrix A \n");
for(i = 0 ; i < m ; i++)
for(j = 0 ; j < n ; j++)
scanf("%d",&a[i][j]);
printf("\n Enter the order of the matrix B :");
scanf("%d%d",&p,&q);
printf("\n Enter the elements of matrix B \n");
for(i = 0 ; i < p ; i++)
for(j = 0 ; j < q ; j++)
scanf("%d",&b[i][j]);

if(n!= p)
{
printf(“\n Matrix multiplication is not possible\n”);
exit(0);
}
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] = c[i][j] + a[i][k] * b[k][j];
}

printf("\n MATRIX C \n");


for(i = 0 ; i < m ; i++)
{
for(j = 0 ; j < q ; j++)
printf(" %d ", c[i][j]);
printf("\n");
}
}
7 . D e v e l o p a P ro g r a m t o c o m p u t e S i n ( x ) / C o s ( x ) u s i n g Ta y l o r s e r i e s
approximation.Compare your result with the built- in Library function. Print both the
results with appropriate messages.
#include<stdio.h>
#include<math.h>
#include <stdio.h>
#include <math.h>

void main()
{
float x, sum_sin, sum_cos, term_sin, term_cos;
int i = 2;

printf("Enter degree:");
scanf("%f", &x);

x = x * (3.14159265 / 180); // Convert degrees to radians


term_sin = sum_sin = x;
term_cos = sum_cos = 1.0;

while (fabs(term_sin) > 0.000001 || fabs(term_cos) > 0.000001)


{
term_sin = -term_sin * x * x / (i * (i + 1));
term_cos = -term_cos * x * x / (i * (i - 1));
sum_sin = sum_sin + term_sin;
sum_cos = sum_cos + term_cos;
i = i + 2;
}

printf("Result using Taylor series approximation: %f\n", (sum_sin/sum_cos));


printf("Result using built-in library function: %f\n", sin(x)/cos(x));
// printf("Result using built-in library function: %f\n", tan(x));
}
8. Sort the given set of N numbers using Bubble sort.

#include<stdio.h>
main()
{
int i, j, n, num[20], temp;
printf("Enter the value of n\n");
scanf("%d", &n);
printf("Enter numbers\n");
for(i=0; i<n; i++)
scanf("%d", &num[i]);
for(i=0; i<n - 1 ; i++)
for(j=0; j<n -1- i ; j++)
if(num[j] > num[j+1])
{
temp = num[j];
num[j] = num[j+1];
num[j+1] = temp;
}
printf("\nSorted array is:");
for(i=0; i<n; i++)
printf("%d\t", num[i]);
}
9. Write functions to implement string operations such as compare, concatenate, string
length. Use the parameter passing techniques.

#include<stdio.h>
length(char str1[])
{
int i=0;
while(str1[i]!='\0')
i++;
printf("Length of first string is %d",i);
}

compare(char str1[],char str2[])


{
int i;
for(i=0;str1[i]!='\0';i++)
{
if(str1[i]!=str2[i])
break;
}
if(str1[i]==str2[i])
printf("\n Strings are same \n");
else
printf("\n Strings are not same \n");

}
concat(char str1[],char str2[])
{
int i=0,j=0;
while(str1[i]!='\0')
i++;
while(str2[j]!='\0')
str1[i++]=str2[j++];

str1[i++]='\0';
puts(str1);
}

main()
{
char str1[20],str2[20];
printf("Enter string 1: ");
gets(str1);
printf("Enter string 2: ");
gets(str2);
length(str1);
compare(str1,str2);
concat(str1,str2);
}

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

#include<stdio.h>
struct STUDENT
{
char name[20];
float marks;
};
void main()
{
struct STUDENT s[20];
int i , n ;
float avg , sum=0;
printf("Enter the number of students\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the name and marks");
scanf("%s %f",s[i].name,&s[i].marks);
}
printf("\n Students details are\n");
printf("\n NAME \t MARKS \n");
for(i=0;i<n;i++)
printf("%s \t %f \n",s[i].name,s[i].marks);

for(i=0;i<n;i++)
sum= sum + s[i].marks ;

avg = sum/n;
printf("Average marks=%f\n",avg);

printf("list of students scored above average marks\n");


for(i=0;i<n;i++)
{
if(s[i].marks>avg)
printf(“%s\n",s[i].name);
}
printf("list of students scored beow average marks\n") ;
for(i=0;i<n;i++)
{
if(s[i].marks<avg)
printf(“ %s\n",s[i].name);
}
}

11. 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>
main()
{
float a[10],*ptr,mean,std,sum=0,sumstd=0;
int n,i;
ptr=a;

printf("Enter the no of elements: \n");


scanf("%d",&n);
printf("Enter array elements: \n");
for(i=0;i<n;i++)
{
scanf("%f",&a[i]);
sum=sum+*ptr;
ptr++;
}
mean=sum/n;

ptr=a;
for(i=0;i<n;i++)
{
sumstd=sumstd+pow(*ptr - mean,2);
ptr++;
}
std = sqrt(sumstd/n);

printf ("Sum=%f\n", sum);


printf("Mean=%f\n",mean);
printf("Standard deviation=%f\n",std);
}

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 <stdlib.h>

int main() {
FILE *sourceFile, *targetFile;
char sourceFileName[100], targetFileName[100];
char ch;

// Input the source file name


printf("Enter the source file name: ");
scanf("%s", sourceFileName);

// Open the source file for reading


sourceFile = fopen(sourceFileName, "r");

if (sourceFile == NULL) {
printf("Failed to open the source file.\n");
return 1;
}

// Input the target file name


printf("Enter the target file name: ");
scanf("%s", targetFileName);
// Open the target file for writing
targetFile = fopen(targetFileName, "w");

if (targetFile == NULL) {
printf("Failed to create or open the target file.\n");
fclose(sourceFile);
return 1;
}

// Copy the contents of the source file to the target file


while ((ch = fgetc(sourceFile)) != EOF) {
fputc(ch, targetFile);
}
printf("File copy completed.\n");

// Close the source and target files


fclose(sourceFile);
fclose(targetFile);

return 0;
}

You might also like