You are on page 1of 37

1.

Write a C program to find average of 3 numbers


#include<stdio.h>
main()
{
float a,b,c,avg;
printf("enter three numbers");
scanf("%f%f%f",&a,&b,&c);
avg=(a+b+c)/3;
printf("average is %f",avg);
getch();
}
2. Write a C program to find largest of 3 numbers
#include<stdio.h>
main()
{
float a,b,c;
printf("enter three numbers");
scanf("%f%f%f",&a,&b,&c);
if((a>b)&&(a>c))
printf("largest is %f",a);
else if((b>a)&&(b>c))
printf("largest is %f",b);
else
printf("largest is %f",c);
getch();
}
3. Write a C program to find average of n numbers given by the user
#include<stdio.h>
main()
{
float a,avg,sum=0;
int n,no;
printf("enter limit");
scanf("%d",&n);
no=n;
while(n>0)
{
printf("enter number");
scanf("%f",&a);
sum=sum+a;
n--;
}
avg=sum/no;
printf("average is %f",avg);
getch();
}
4. Write a C program to find largest of n numbers
#include<stdio.h>
main()
{
float a,max;
int n,no;
printf("enter limit");
scanf("%d",&n);
no=n;
printf("enter number");
scanf("%f",&a);
max=a;
while(n-1>0)
{
printf("enter number");
scanf("%f",&a);
if(a>max)
max=a;
n--;
}
printf("largest is %f",max);
getch();
}
5. Write a C program to check a number is odd or even
#include<stdio.h>
main()
{
int a;
printf("enter number");
scanf("%d",&a);
if(a%2==0)
printf("%d is even",a);
else
printf("%d is odd",a);
getch();
}
6. Write a C program to check a number is a prime number.
#include<stdio.h>
main()
{
int n,i=2,done=0;
printf("enter number");
scanf("%d",&n);
while (i<n)
{
if(n%i==0)
done=1;
i=i+1;
}
if (done==1)
printf("%d is not a prime number",n);
else
printf("%d is a prime number",n);

getch();

}
7. Write a C program to find factorial of a number

#include <stdio.h>

int main()
{
int i, Number;
long Factorial = 1;
printf("\n Please Enter any number to Find Factorial\n");
scanf("%d", &Number);
for (i = 1; i <= Number; i++)
{
Factorial = Factorial * i;
}

printf("\nFactorial of %d = %d\n", Number, Factorial);


return 0;
}

8. Write a C program to print n terms in Fibonacci series.


/* Fibonacci Series c language */
#include<stdio.h>
int main()
{
int n, first = 0, second = 1, next, c;
printf("Enter the number of terms\n");
scanf("%d",&n);
printf("First %d terms of Fibonacci series are :-\n",n);
for ( c = 0 ; c < n ; c++ )
{
if ( c <= 1 )
next = c;
else
{
next = first + second;
first = second;
second = next;
}
printf("%d\n",next);
}
return 0;
}

9. Write a C program to find sum of digits in a number and to check whether it is an


Armstrong number.
#include<stdio.h>
int main(){
int num,r,sum=0,temp;
printf("Enter a number: ");
scanf("%d",&num);
temp=num;
while(num!=0){
r=num%10;
num=num/10;
sum=sum+(r*r*r);
}
if(sum==temp)
printf("%d is an Armstrong number",temp);
else
printf("%d is not an Armstrong number",temp);

return 0;
}
10. Write a C program to find the reverse of a number and check whether it is a palindrome.
#include <stdio.h>
void main()
{
int num, temp, remainder, reverse = 0;

printf("Enter an integer \n");


scanf("%d", &num);
/* original number is stored at temp */
temp = num;
while (num > 0)
{
remainder = num % 10;
reverse = reverse * 10 + remainder;
num /= 10;
}
printf("Given number is = %d\n", temp);
printf("Its reverse is = %d\n", reverse);
if (temp == reverse)
printf("Number is a palindrome \n");
else
printf("Number is not a palindrome \n");
}
11. Write a C program to find roots of a quadratic equation.
int main()
{
double a, b, c, determinant, root1,root2, realPart, imaginaryPart;

printf("Enter coefficients a, b and c: ");


scanf("%lf %lf %lf",&a, &b, &c);

determinant = b*b-4*a*c;

// condition for real and different roots


if (determinant > 0)
{
// sqrt() function returns square root
root1 = (-b+sqrt(determinant))/(2*a);
root2 = (-b-sqrt(determinant))/(2*a);

printf("root1 = %.2lf and root2 = %.2lf",root1 , root2);


}

//condition for real and equal roots


else if (determinant == 0)
{
root1 = root2 = -b/(2*a);

printf("root1 = root2 = %.2lf;", root1);


}

// if roots are not real


else
{
realPart = -b/(2*a);
imaginaryPart = sqrt(-determinant)/(2*a);
printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart, imaginaryPart, realPart,
imaginaryPart);
}

return 0;
}
12. Write a C program to find the quadrant where point(x,y) lies.
void main()
{
int x, y;
printf("Enter the values for X and Y\n");
scanf("%d %d", &x, &y);
if (x > 0 && y > 0)
printf("point (%d, %d) lies in the First quandrant\n");
else if (x < 0 && y > 0)
printf("point (%d, %d) lies in the Second quandrant\n");
else if (x < 0 && y < 0)
printf("point (%d, %d) lies in the Third quandrant\n");
else if (x > 0 && y < 0)
printf("point (%d, %d) lies in the Fourth quandrant\n");
else if (x == 0 && y == 0)
printf("point (%d, %d) lies at the origin\n");
}
13. Write a C program to check for a leap year
int main()
{
int year;
printf("Enter a year: ");
scanf("%d",&year);
if(year%4 == 0)
{
if( year%100 == 0)
{
// year is divisible by 400, hence the year is a leap year
if ( year%400 == 0)
printf("%d is a leap year.", year);
else
printf("%d is not a leap year.", year);
}
else
printf("%d is a leap year.", year );
}
else
printf("%d is not a leap year.", year);

return 0;
}
14. Write a C program to convert temperature in Farenheit to celcius.
int main()
{
float celsius, fahrenheit;
// Reads temperature in fahrenheit
printf("Enter temperature in Fahrenheit: ");
scanf("%f", &fahrenheit);
// Fahrenheit to celsius conversion formula
celsius = (fahrenheit - 32) * 5 / 9;
// Print the result
printf("%.2f Fahrenheit = %.2f Celsius", fahrenheit, celsius);
return 0;
}
15. Write a Menu driven calculator program .
void main()
{
int a,b,c,ch;
clrscr();
printf(“\n1.add\n2.subtract\n3.multiply\n4.division\n5.remainder\n);
printf(“\nenter your choice\n”);
scanf(“%d”,&ch);
switch(ch)
{
case1:
printf(“\nenter values of a and b\n”);
scanf(“%d%d”,&a,&b);
c=a+b;
printf(“\nthe answer is %d”,c);
break;
case2:
printf(“\nenter values of a and b\n”);
scanf(“%d%d”,&a,&b);
c=a-b;
printf(“\nthe answer is %d”,c);
break;
case3:
printf(“\nenter values of a and b\n”);
scanf(“%d%d”,&a,&b);
c=a*b;
printf(“\nthe answer is %d”,c);
break;
case4:
printf(“\nenter values of a and b\n”);
scanf(“%d%d”,&a,&b);
c=a/b;
printf(“\nthe answer is %d”,c);
break;
case5:
printf(“\nenter values of a and b\n”);
scanf(“%d%d”,&a,&b);
c=a%b;
printf(“\nthe answer is %d”,c);
break;
default:
printf(“\nenter the correct choice”);
break;
}
getch();
}

16. Write a Menu driven program to find the area of circle, square, rectangle and triangle.
void main()
{
int fig_code;
float side, base, length, breadth, height, area, radius;
printf("-------------------------\n");
printf(" 1 --> Circle\n");
printf(" 2 --> Rectangle\n");
printf(" 3 --> Triangle\n");
printf(" 4 --> Square\n");
printf("-------------------------\n");
printf("Enter the Figure code\n");
scanf("%d", &fig_code);
switch(fig_code)
{
case 1:
printf("Enter the radius\n");
scanf("%f", &radius);
area = 3.142 * radius * radius;
printf("Area of a circle = %f\n", area);
break;
case 2:
printf("Enter the breadth and length\n");
scanf("%f %f", &breadth, &length);
area = breadth * length;
printf("Area of a Reactangle = %f\n", area);
break;
case 3:
printf("Enter the base and height\n");
scanf("%f %f", &base, &height);
area = 0.5 * base * height;
printf("Area of a Triangle = %f\n", area);
break;
case 4:
printf("Enter the side\n");
scanf("%f", &side);
area = side * side;
printf("Area of a Square=%f\n", area);
break;
default:
printf("Error in figure code\n");
break;
}
}

17.write a program to display sum of square of odd element and sum of


cube of even elements.
#include<stdio.h>
main()
{
int i,arr[30],n,sq=0,cu=0;
printf("enter the limit");
scanf("%d",&n);
for (i=0;i<n;i++)
{
printf("enter the element");
scanf("%d",&arr[i]);
if(arr[i]%2==0)
sq=sq+arr[i]*arr[i];
else
cu=cu+arr[i]*arr[i]*arr[i];
}

printf("squaresum=%d \n cube sum = %f\n",sq,cu);


getch();
}
18. write a program to concatenate two strings
#include<stdio.h>
main()
{
char s1[20],s2[20],i=0,j;
printf(“enter the string1\n”);
gets(s1);
printf(“enter the string 2 \n”);
gets(s2);
while(s1[i]!=’\0’)
{
i=i+1;
}
for(j=0;s2[j]!=’\0’;j++)
{
s1[i]=s2[j];
i=i+1;
}
s1[i]=’\0’;
printf(“The concatenated string is \n”);
puts(s1);
}
3.write a program to find sum of diagonal elements in matrix
include<stdio.h>

int main(){
  int a[10][10],i,j,sum=0,m,n;

  printf("\nEnter the row and column of matrix: ");


  scanf("%d %d",&m,&n);

  printf("\nEnter the elements of matrix: ");


  for(i=0;i<m;i++)
      for(j=0;j<n;j++)
           scanf("%d",&a[i][j]);
  printf("\nThe matrix is\n");

  for(i=0;i<m;i++){
      printf("\n");
      for(j=0;j<m;j++){
      printf("%d\t",a[i][j]);
      }
 }
 for(i=0;i<m;i++){
     for(j=0;j<n;j++){
          if(i==j)
              sum=sum+a[i][j];
     }
 }
 printf("\n\nSum of the diagonal elements of a matrix is: %d",sum);

 return 0;
}

19. Write a program using structures to read the following information of employees::
Employee name, employee id, age, experience and salary. The program should print
the following:i)List of all employees with details ii)List of employees who have
experience less than 5 years but salary more than 25000.

struct employee
{
int emp_id;
char name[20];
float salary;
int exp;
int age;
};
main( )
{
struct employee e[100];
int n,i;
printf("enter the number of employees");
scanf("%d",&n);
for (i=0;i<n;i++)
{
printf("Enter the employee id of employee:");
scanf("%d",&e[i].emp_id);
printf ("Enter the name of employee:");
scanf("%s",&e[i].name);
printf ("Enter the salary of employee:");
scanf("%f",&e[i].salary);
printf ("Enter the experience of employee:");
scanf("%d",&e[i].exp);
printf ("Enter the age of employee:");
scanf("%d",&e[i].age);
}
printf("\n EMPLOYEE DETAILS \n");
for (i=0;i<n;i++)
{
printf ("The employee id of employee is : %d", e[i].emp_id);
printf ("\nThe name of employee is : %s", e[i].name);
printf ("\nThe salary of employee is : %f", e[i].salary);
printf ("\nThe department of employee is : %d", e[i].exp);
printf ("\nThe age of employee is : %d\n\n", e[i].age);
}
printf("\n DETAILS OF EMPLOYEE HAVING EXPERIENCE LESS THAN 5 YEARS & SALARY MORE THAN
25000\n");
for (i=0;i<n;i++)
{
if (e[i].exp<5 && e[i].salary>25000)
{
printf ("The employee id of employee is : %d", e[i].emp_id);
printf ("\nThe name of employee is : %s", e[i].name);
printf ("\nThe salary of employee is : %f", e[i].salary);
printf ("\nThe department of employee is : %d", e[i].exp);
printf ("\nThe age of employee is : %d\n\n", e[i].age);
}
}
getch();
}
20. Write a recursive function to print sum of digits in a number.
void sum(int n,int s)
{
int a;
if (n>0)
{
a=n%10;
s=s+a;
n=n/10;
sum(n,s);
}
else
printf(" sum of digits is %d",s);
}

main()
{
int x,f,su=0;
printf("Enter the number:");
scanf("%d",&x);
sum(x,su);
getch();
}

21. Write a program to display prime numbers up to n using functions.


void prime(int n)
{
int j,i,done;
for(j=2;j<n;j++)
{
i=2;
done=0;
while (i<j)
{
if(j%i==0)
done=1;
i=i+1;
}
if (done==0)
printf("%d \n",j);
}
}
main()
{
int n;
printf("enter number");
scanf("%d",&n);
prime(n);
getch();
}

22. Write a program to read a file and write the content into another file

#include<stdio.h>
#include<process.h>

void main() {
FILE *fp1, *fp2;
char a;
clrscr();

fp1 = fopen("test.txt", "r");


if (fp1 == NULL) {
puts("cannot open this file");
exit(1);
}

fp2 = fopen("test1.txt", "w");


if (fp2 == NULL) {
puts("Not able to open this file");
fclose(fp1);
exit(1);
}

do {
a = fgetc(fp1);
fputc(a, fp2);
} while (a != EOF);

fcloseall();
getch();
}
23.Write a C program to find the reverse of a string using pointers.
#include<stdio.h>
#include<string.h>
main()
{
int i=0,j,len;
char s[30],*str;
printf("enter the string: ");
gets(s);
len=strlen(s);
str=s;
while (len>=0)
{
printf("%c",*(str+len));
len--;
}
getch();
}

24.Write a C program to read an array and pass this array to a function


which sorts the list in ascending order.
#include<stdio.h>
sort( int a[], int n);
main()
{
int i,j,a[30],n,temp,key,flag=0;
printf("enter the limit");
scanf("%d",&n);
for (i=0;i<n;i++)
{
printf("enter the element");
scanf("%d",&a[i]);
}
sort(a,n);
getch();
}
sort( int a[], int n)
{
int i,j,temp;
for ( i=0;i<n;i++)
{
for (j=0;j<n;j++)
{
if(a[i]<a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("elements in sorted order");
for (i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
}
25.An array contains zero and non zero elements. Pack the non zero
elements to occupy the last few locations. Write a C program to
implement this using pointers.
#include<stdio.h>
main()
{
int i,j,a[30],n,l,h,new[30];
printf("enter the limit");
scanf("%d",&n);
for (i=0;i<n;i++)
{
printf("enter the element");
scanf("%d",a+i);
}
l=0;
h=n-1;
for (i=0;i<n;i++)
{
if(a[i]==0)
{ *(new+l)=a[i];
l++;}
else
{ *(new+h)=a[i];
h--;}
}

printf("ordered array elements");


for (i=0;i<n;i++)
{
printf("%d\n",*(new+i));
}
getch();
}
26.Write a C program to read the contents of a file and find the average of
all the elements in that file.
#include<stdio.h>
int main()
{
FILE *f1;
float s=0,avg;
int count=0;
float val;
char a;
f1=fopen("input.txt","r");
while(!feof(f1))
{
fscanf(f1,"%f",&val);
s=s+val;
count++;
}
avg=s/count;
fclose(f1);
printf("sum=%f",s);
printf("average=%f",avg);
getch();
}
27.Write a C program to write a string in to the file and display the count
of words, characters and vowels in that string.
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
FILE *f1;
int wc=1,vc=0,cc=0,i=0;
char a[100];
printf("enter the string");
gets(a);
puts(a);
while(i<=strlen(a))
{
if (a[i]=='a'||a[i]=='e'||a[i]=='i'||a[i]=='o'||a[i]=='u'||a[i]=='A'||a[i]=='E'||
a[i]=='I'||a[i]=='O'||a[i]=='U')
vc++;
else if (a[i]==' '||a[i]=='.'||a[i]=='?'||a[i]=='!'||a[i]==';'||a[i]=='\n')
wc++;
else
cc++;
i++;
}
printf ("no of vowels= %d",vc);
printf ("no of characters= %d",cc);
printf ("no of words= %d",wc);
f1=fopen("strg.txt","w");
fputs(a,f1);
fclose(f1);
getch();
}
28. Write a C program to print n prime numbers.
#include<stdio.h>
main()
{
int n, i = 3, count, c;

printf("Enter the number of prime numbers required\n");


scanf("%d",&n);

if ( n >= 1 )
{
printf("First %d prime numbers are :\n",n);
printf("2\n");
}

for ( count = 2 ; count <= n ; )


{
for ( c = 2 ; c <= i - 1 ; c++ )
{
if ( i%c == 0 )
break;
}
if ( c == i )
{
printf("%d\n",i);
count++;
}
i++;
}
getch();
}
29. Write a C program to read a matrix and to find the normal of matrix.
main ()
{
int array[10][10];
int i, j, m, n, sum = 0, sum1 = 0, a = 0, normal;

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


scanf("%d %d", &m, &n);
printf("Enter the n coefficients of the matrix \n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", &array[i][j]);
a = array[i][j] * array[i][j];
sum1 = sum1 + a;
}
}
normal = sqrt(sum1);
printf("The normal of the given matrix is = %d\n", normal);
getch();
}
30. Write a program to read a matrix and convert it into upper triangular matrix and
find sum of upper triangular matrix elements by using functions.- 3marks
#include<stdio.h>
void upper(int a[][10],int r,int c)
{
int sum=0,I,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
If(i>=j)
{
sum=sum+a[i][j];
}
}
}
printf(“%d”,sum);
}
void main()
{
int a[10][10],r,c,I,j;
printf(“enter the rows and columns”);
scanf(“%d”,&r,&c);
printf(“enter the elements);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf(“%d”,&a[i][j]);
}
}
upper(a,r,c);
}
31. Write a c program to read a file input.txt which contains a list of numbers. Sort the
numbers using selection sort and write it into the file sort.txt . Read a key element
and search whether the element is present in the file sort.txt using binary search
and display the position of key element
#include<stdio.h>
void main()
{
FILE *fp1,*fp2
int a[50],I,j,n,small,pos,tmp,item,l,u,m,flag=0;
fp1=fopen(“input.txt”,”r”);
fp2=fopen(“sort.txt”,”w”);
i=0;
while (!feof(fp1))
{
fscanf(fp,“%d”,a[i]);
i++
}
n=i;
for(i=0;i<n-1;i++)
{
small=a[i];
pos=I;
for(j=I;j<n;j++)
{
If(a[j]<small)
{
small=a[j]
pos=j;
}
}
tmp=a[i];
a[i]=a[pos];
a[pos]=tmp;
fprintf(fp1,”%d”,a[i]
}
fclose(fp1);
fclose(fp2);
printf(“enter the serach item”);
scanf(“%d”,&item);
fp2=fopen(“sort.txt”,”r”);
i=0;
while(!feof(fp2)
{
fscanf(“%d”,&a[i]);
i++
}
l=0;
u=n-1;
while(l<=u)
{
m=(l+u)/2;
if(a[m]=item)
{
flag=1;
break;
}
else if(item>a[m])
l=m+1;
else
u=m-1;
}
If(flag==1)
printf(“found %d%d”,item,m+1);
else
printf(“not found”);
fclose(fp2);
}

32. Write a C program to input a string into a file and search whether the given
substring is present in that file or not.
#include<stdio.h>
#include<string.h>
void main()
{
FILE *fp;
fp=fopen(“string.txt,”w”);
char s[20],ss[20];
printf(“enter a string”);
gets(s);
puts(s,fp);
fclose(fp);
printf(“enter a substring”);
gets(ss);
fp=fopen(“string.txt”,”r”);
while(!=feof(fp))
{
fscanf(fp,”%s”,s);
if(strstr(s,ss)>0)
{
printf(“string is found”);
break;
}
else
{
printf(“string not found”);
}
fclose(fp);
}

33. Write a program to read a file (number.txt) and find the even sum and write the
even sum into the(even.txt) And find the odd sum and write the odd sum into
the(odd.txt)
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
FILE *f1,*f2,*f3;
int a,osum=0;
int esum=0;
f1=fopen("C:\\Python27\\mima\\input.txt","r");
f2=fopen("C:\\Dev-Cpp\\mima\\odd.txt","w");
f3=fopen("C:\\Dev-Cpp\\mima\\even.txt","w");
while((a=getw(f1))!=EOF)
{
if (a%2==0)
{
putw(a,f3);
esum=esum+a;
}
else
{
putw(a,f2);
osum=osum+a;
}
}
fclose(f1);
fclose(f2);
fclose(f3);
f2=fopen("C:\\Dev-Cpp\\mima\\odd.txt","r");
f3=fopen("C:\\Dev-Cpp\\mima\\even.txt","r");
printf("\nodd file content");
while((a=getw(f2))!=EOF)
printf("%d\n",(int)a);
printf("\nsum of odd numbers is %d",osum);
printf("\neven file content");
while((a=getw(f3))!=EOF)
printf("%d\n",a);
printf("\nsum of even numbers is %d",esum);
fclose(f2);
fclose(f3);
getch();
}
34. Write a C program to add two number using command line arguments.
#include<stdio.h>
void main(int argc, char * argv[]) {
. int i, sum = 0;
if (argc != 3) {
printf("You have forgot to type numbers.");
exit(1);
}
printf("The sum is : ");
for (i = 1; i < argc; i++)
sum = sum + atoi(argv[i]);
printf("%d", sum);

}
35. Write a C program to read the string from a file and write the reverse into
another file.
# include <stdio.h>
# include <conio.h>
# include <process.h>
void main()
{
FILE *f1,*f2;
char file1[20],file2[20];
char ch;
int n;
printf("Enter the file1 name:");
scanf("%s",file1);
printf("Enter the file2 name:");
scanf("%s",file2);
f1=fopen(file1,"r");
f2=fopen(file2,"w");
if(f1==NULL || f2==NULL)
{
printf("Cannot open file");
exit(1);
}
printf("Characters to read from end of file :");
scanf("%d",&n);
fseek(f1,-n,SEEK_SET);
while(!feof(f1))
{
ch=fgetc(f1);
fputc(ch,f2);
}
fcloseall();
getche();
36. Program to Add Two Matrices

#include <stdio.h>
int main(){
int r, c, a[100][100], b[100][100], sum[100][100], i, j;

printf("Enter number of rows (between 1 and 100): ");


scanf("%d", &r);
printf("Enter number of columns (between 1 and 100): ");
scanf("%d", &c);

printf("\nEnter elements of 1st matrix:\n");

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


for(j=0; j<c; ++j)
{
printf("Enter element a%d%d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}

printf("Enter elements of 2nd matrix:\n");


for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("Enter element a%d%d: ",i+1, j+1);
scanf("%d", &b[i][j]);
}

// Adding Two matrices

for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
sum[i][j]=a[i][j]+b[i][j];
}

// Displaying the result


printf("\nSum of two matrix is: \n\n");

for(i=0;i<r;++i)
for(j=0;j<c;++j)
{

printf("%d ",sum[i][j]);

if(j==c-1)
{
printf("\n\n");
}
}

return 0;
}
37. Remove Characters in String Except Alphabets

#include<stdio.h>

int main()
{
char line[150];
int i, j;
printf("Enter a string: ");
gets(line);

for(i = 0; line[i] != '\0'; ++i)


{
while (!( (line[i] >= 'a' && line[i] <= 'z') || (line[i] >= 'A' && line[i] <= 'Z') || line[i] == '\0') )
{
for(j = i; line[j] != '\0'; ++j)
{
line[j] = line[j+1];
}
line[j] = '\0';
}
}
printf("Output String: ");
puts(line);
return 0;
}
38. Store Information and Display it Using Structure

#include <stdio.h>
struct student
{
char name[50];
int roll;
float marks;
} s;

int main()
{
printf("Enter information:\n");

printf("Enter name: ");


scanf("%s", s.name);

printf("Enter roll number: ");


scanf("%d", &s.roll);

printf("Enter marks: ");


scanf("%f", &s.marks);

printf("Displaying Information:\n");

printf("Name: ");
puts(s.name);

printf("Roll number: %d\n",s.roll);

printf("Marks: %.1f\n", s.marks);

return 0;
}
39. C Program to Write a Sentence to a File

#include <stdio.h>
#include <stdlib.h> /* For exit() function */
int main()
{
char sentence[1000];
FILE *fptr;

fptr = fopen("program.txt", "w");


if(fptr == NULL)
{
printf("Error!");
exit(1);
}
printf("Enter a sentence:\n");
gets(sentence);
fprintf(fptr,"%s", sentence);
fclose(fptr);

return 0;
}
40. Program to replace a substring with another sub string
void main()
{
char s[100],ch[10][10]={"\0"},sbstr[20],sbrp[20];
int i,j,k,l;
clrscr();
printf("Enter the string::");
gets(s);
printf("Enter the substring that u want to replace::");
gets(sbstr);
printf("Enter the new substring::");
gets(sbrp);
l=strlen(s);
i=0; j=0; k=0;

while(i<l) // conversion of string to words


{
while(s[i]!=' ')
{
ch[k][j++]=s[i++];
}
j=0;
k++;
i++;
}

for(i=0;i<k;i++)
{
if((strcmp(ch[i],sbstr))==0)
{

strcpy(ch[i],sbrp);
}
}
printf("\n string after replacement is::");
for(i=0;i<k;i++)
printf("%s ",ch[i]);
getch();
}
41. .Program to delete a substring from a given string
void main()
{
char s[100],ch[10][10]={"\0"},sbstr[20];
int i,j,k,l;
clrscr();
printf("Enter the string::");
gets(s);
printf("Enter the substring to be deleted::");
gets(sbstr);
l=strlen(s);
i=0; j=0; k=0;

while(i<l) // conversion of string to words


{
while(s[i]!=' ')
{
ch[k][j++]=s[i++];
}
j=0;
k++;
i++;
}

for(i=0;i<k;i++)
{
if((strcmp(ch[i],sbstr))==0)
{
for(j=i;j<k;j++)
{
strcpy(ch[j],ch[j+1]);
}
}
}

k--;
printf("\n string after deletion is::");
for(i=0;i<k;i++)
printf("%s ",ch[i]);
getch();
}
42. program to find square sum and cube sum of array elements using pointers.
#include<stdio.h>
main()
{
int i,arr[30],n,sq=0,cu=0;
printf("enter the limit");
scanf("%d",&n);
for (i=0;i<n;i++)
{
printf("enter the element");
scanf("%d",&arr[i]);
if(arr[i]%2==0)
sq=sq+arr[i]*arr[i];
else
cu=cu+arr[i]*arr[i]*arr[i];
}

printf("squaresum=%d \n cube sum = %f\n",sq,cu);


getch();
}
43. program to read an array and to pass this array to a function where sum and average of
array elements is found
#include<stdio.h>
average( int b[], int n)
{
int x=0,sum=0;
float avg;
printf("array element\tarray value\taddress\n");
while(x<n)
{
printf("arr[%d]\t%d\t%d\n",x,*b,b);
sum=sum+*b;
b++;
x++;
}

avg=(float)sum/n;
printf("sum=%d \n average = %f\n",sum,avg);
}
main()
{
int i,arr[30],n,*a;
float avg;
printf("enter the limit");
scanf("%d",&n);
for (i=0;i<n;i++)
{
printf("enter the element");
scanf("%d",&arr[i]);
}
average(arr,n);

getch();
}

44. Write a C program to check whether matrix is symmetric or not


main()
{
int m, n, c, d, matrix[10][10], transpose[10][10];

printf("Enter the number of rows and columns of matrix\n");


scanf("%d%d",&m,&n);
printf("Enter the elements of matrix\n");
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
scanf("%d",&matrix[c][d]);
for( c = 0 ; c < m ; c++ )
{
for( d = 0 ; d < n ; d++ )
{
transpose[d][c] = matrix[c][d];
}
}
if ( m == n ) /* check if order is same */
{
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < m ; d++ )
{
if ( matrix[c][d] != transpose[c][d] )
break;
}
if ( d != m )
break;
}
if ( c == m )
printf("Symmetric matrix.\n");
}
else
printf("Not a symmetric matrix.\n");

return 0;
}
45. Write a C program to create a structure of book containing details book id, book
name, author and price. Display the details of book requested by the user by
specifying book id.
struct book
{
int book_id;
char name[20];
float price;
char author[20];
};
main( )
{
struct book e[100];
int n,i,id;
printf("enter the number of books");
scanf("%d",&n);
for (i=0;i<n;i++)
{
printf("Enter the book id of book:");
scanf("%d",&e[i].book_id);
printf ("Enter the name of book:");
scanf("%s",&e[i].name);
printf ("Enter the price of book:");
scanf("%f",&e[i].price);
printf ("Enter the name of author:");
scanf("%s",&e[i].author);
}
printf("\n Enter the id of book which u need.\n");
scanf("%d",& id);
printf("\n BOOK DETAILS \n");
for (i=0;i<n;i++)
{
if (e[i].book_id==id)
{
printf ("The book id of book is : %d", e[i].book_id);
printf ("\nThe name of book is : %s", e[i].name);
printf ("\nThe price of book is : %f", e[i].price);
printf ("\nThe author of book is : %s", e[i].author);
}
}
getch();
}

46. Write a C program to open a file “input.txt” containing numbers. Write the
largest number among them in to a new file “output.txt”.
#include<stdio.h>
int main()
{
FILE *f1;
int a[25],max;
int no,i=0;
f1=fopen("input.txt","r");
while(!feof(f1))
{
fscanf(f1,"%d",&no);
printf("%d\n",no);
a[i]=no;
i++;
}
max=a[0];
while(i-1>0)
{
if(a[i-1]>max)
max=a[i-1];
i--;
}
printf("largest is %d",max);
fclose(f1);
f1=fopen("op.txt","w");
fprintf(f1,"%d",max);
getch();
}
47. program to create employee structure to display DETAILS OF EMPLOYEE HAVING
EXPERIENCE LESS THAN 5 YEARS & SALARY MORE THAN 25000 using pointers
#include <stdio.h>
#include <conio.h>
struct employee
{
int emp_id;
char name[20];
int salary;
int exp;
int age;
};
main( )
{
struct employee e[100],*p[10];
int n,i;

printf("enter the number of employees");


scanf("%d",&n);
for (i=0;i<n;i++)
p[i]=&e[i];
for (i=0;i<n;i++)
{
printf("Enter the employee id of employee:");
scanf("%d",&p[i]->emp_id);
printf ("Enter the name of employee:");
scanf("%s",&p[i]->name);
printf ("Enter the salary of employee:");
scanf("%d",&p[i]->salary);
printf ("Enter the experience of employee:");
scanf("%d",&p[i]->exp);
printf ("Enter the age of employee:");
scanf("%d",&p[i]->age);
}
printf("\n EMPLOYEE DETAILS \n");
for (i=0;i<n;i++)
{
printf ("The employee id of employee is : %d", p[i]->emp_id);
printf ("\nThe name of employee is : %s", p[i]->name);
printf ("\nThe salary of employee is : %d", p[i]->salary);
printf ("\nThe department of employee is : %d", p[i]->exp);
printf ("\nThe age of employee is : %d\n\n", p[i]->age);
}
printf("\n DETAILS OF EMPLOYEE HAVING EXPERIENCE LESS THAN 5 YEARS &
SALARY MORE THAN 25000\n");
for (i=0;i<n;i++)
{
if (p[i]->exp<5 && p[i]->salary>25000)
{
printf ("The employee id of employee is : %d", p[i]->emp_id);
printf ("\nThe name of employee is : %s", p[i]->name);
printf ("\nThe salary of employee is : %d", p[i]->salary);
printf ("\nThe department of employee is : %d", p[i]->exp);
printf ("\nThe age of employee is : %d\n\n", p[i]->age);
}
}
getch();
}

48. Program to mutiply two matrices


#include <stdio.h>

int main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];

printf("Enter the number of rows and columns of first matrix\n");


scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");

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


for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);

printf("Enter the number of rows and columns of second matrix\n");


scanf("%d%d", &p, &q);

if (n != p)
printf("Matrices with entered orders can't be multiplied with each other.\n");
else
{
printf("Enter the elements of second matrix\n");

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


for (d = 0; d < q; d++)
scanf("%d", &second[c][d]);
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++) {
for (k = 0; k < p; k++) {
sum = sum + first[c][k]*second[k][d];
}

multiply[c][d] = sum;
sum = 0;
}
}

printf("Product of entered matrices:-\n");

for (c = 0; c < m; c++) {


for (d = 0; d < q; d++)
printf("%d\t", multiply[c][d]);

printf("\n");
}
}

return 0;
getch();
}
49. program to find normal of a matrix using pointers
#include <stdio.h>
#include <math.h>

main ()
{
int a[10][10];
int i, j, m, n, sum = 0, sum1 = 0;float normal;

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


scanf("%d %d", &m, &n);
printf("Enter the n coefficients of the matrix \n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", *(a+i)+j);
sum1 = sum1 +(*(*(a+i)+j))* (*(*(a+i)+j)) ;
}
}
normal = sqrt(sum1);
printf("The normal of the given matrix is = %f\n", normal);

getch();
}
50. program to check palindrome string using pointers
#include<stdio.h>
#include<string.h>
main()
{
int i=0,j,len,flag=0;
char s[30],rev[30],*str;
printf("enter the string: ");
gets(s);
len=strlen(s);
str=s;
while (*str!='\0')
{
rev[len-1-i]=*str;
i++;
str++;
}
rev[len]='\0';
printf("reverse string: ");
puts(rev);
str=s;
for (i=0;i<len;i++)
{
if(*str!=rev[i])
{
printf("not palindrome");
flag=1;
break;
}
str++;
}
if(flag==0)
printf("palindrome");
getch();
}

You might also like