Professional Documents
Culture Documents
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;
}
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;
determinant = b*b-4*a*c;
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;
}
}
int main(){
int a[10][10],i,j,sum=0,m,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();
}
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();
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();
}
if ( n >= 1 )
{
printf("First %d prime numbers are :\n",n);
printf("2\n");
}
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;
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
sum[i][j]=a[i][j]+b[i][j];
}
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);
#include <stdio.h>
struct student
{
char name[50];
int roll;
float marks;
} s;
int main()
{
printf("Enter information:\n");
printf("Displaying Information:\n");
printf("Name: ");
puts(s.name);
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;
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;
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;
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];
}
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();
}
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;
int main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
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");
multiply[c][d] = sum;
sum = 0;
}
}
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;
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();
}