C Popupdated
C Popupdated
#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;
#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;
#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;
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];
}
void main()
{
float x, sum_sin, sum_cos, term_sin, term_cos;
int i = 2;
printf("Enter degree:");
scanf("%f", &x);
#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);
}
}
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);
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;
ptr=a;
for(i=0;i<n;i++)
{
sumstd=sumstd+pow(*ptr - mean,2);
ptr++;
}
std = sqrt(sumstd/n);
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;
if (sourceFile == NULL) {
printf("Failed to open the source file.\n");
return 1;
}
if (targetFile == NULL) {
printf("Failed to create or open the target file.\n");
fclose(sourceFile);
return 1;
}
return 0;
}