Professional Documents
Culture Documents
Problem Solving in C LAB - 2021 2-1
Problem Solving in C LAB - 2021 2-1
14. Write a program to read the data character by character from a file.
15. Write a program to createBook (ISBN,Title, Author, Price, Pages,
Publisher)structureand store book details in a file and perform the following operations
a. Add book details
b. Search a book details for a given ISBN and display book details, if available
c. Update a book details using ISBN
d. Delete book details for a given ISBN and display list of remaining Books
while(num!=0)
{
rem=num%10;
sum+=rem*rem*rem;
num/=10;
}
if(sum==x)
printf("%d is an Armstrong number\n", x);
else
printf("%d is Not an Armstrong number\n", x);
}
Output
1.
Enter any Number
153
153 is an Armstrong number
2.
if(num>0)
{
while(num!=0)
{
rem=num%10;
sum+=rem;
num/=10;
}
printf("sum of the individual digit of %d is %d
\n",x,sum);
}
else
printf("%d is negative number\n", x);
}
#include <stdio.h>
main()
{
int i, n, t1 = 0, t2 = 1, nextTerm = 0;
4. Write a program to find both the largest and smallest number in a list of integer values
#include<stdio.h>
int main(){
int a[50],size,i,big,small;
big=a[0];
for(i=1;i<size;i++){
if(big<a[i])
big=a[i];
}
printf("Largest element: %d",big);
small=a[0];
for(i=1;i<size;i++){
if(small>a[i])
small=a[i];
}
printf("Smallest element: %d",small);
return 0;
}
Sample Output:
Enter the size of the array: 4
Enter 4 elements in to the array: 2 7 8 1
Largest element: 8
Smallest element: 1
#include <stdio.h>
void swap(int, int); // function prototype
int main()
{
int a,b;
printf("Enter two numbers: ");
scanf("%d%d",&a,&b);
printf("Before swapping\n");
printf("a = %d\n",a);
printf("b = %d\n",b);
swap(a,b); // function call by reference
printf("After swapping\n");
printf("a = %d\n",a);
printf("b = %d\n",b);
return 0;
}
Output
Enter two numbers:
30
40
Before swapping
a = 30
b = 40
After swapping
#include <stdio.h>
void swap(int *, int *); // function prototype
int main()
{
int a,b;
printf("Enter two numbers: ");
scanf("%d%d",&a,&b);
printf("Before swapping\n");
printf("a = %d\n",a);
printf("b = %d\n",b);
swap(&a,&b); // function call by reference
printf("After swapping\n");
printf("a = %d\n",a);
printf("b = %d\n",b);
return 0;
}
#include <stdio.h>
int main()
{
int r=0, c=0, a[100][100], b[100][100],i, j;
void addmatrices(int x[100][100],int y[100][100],int,int);
addmatrices(a,b,r,c);
return 0;
}
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
if(j==c-1)
{
printf("\n\n");
}
}
Output
Enter number of rows
2
Enter number of columns
2
#include<stdio.h>
int fact(int);
int main(){
int num,f;
printf("\nEnter a number: ");
scanf("%d",&num);
f=fact(num);
printf("\nFactorial of %d is: %d",num,f);
return 0;
}
multiply[c][d] = sum;
sum = 0;
}
}
printf("\n");
}
}
}
Output
#include<stdio.h>
#include<string.h>
main()
{
char str1[100],str2[100],str3[100],str4[100];
clrscr();
printf("Enter string1 and string2 \n");
gets(str1);
gets(str2);
printf("Length of str1 = %d \t str2 =
%d\n",strlen(str1),strlen(str2));
printf("string comparison\n");
if(strcmp(str1,str2)==0)
printf("Both strings are equal\n");
else
printf("Both strings are not equal\n");
/*concatenating strings */
strcat(str3,str2);
printf("concatenated string is %s\n\n",str3);
/* reversing string */
strcpy(str4,strrev(str1));
printf("Reversed string is %s \n\n",str4);
}
int main()
{
int array[MAX_SIZE];
int size;
int i, j, temp;
return 0;
}
Output
Enter size of array: 5
Enter elements in array: 15
7
8
6
14
Elements of array in sorted ascending order: 6 7 8 14 15
12. Write a program to calculate the salaries of all employees using Employee (ID,
Name, Designation, Basic Pay, DA, HRA, Gross Salary, Deduction, Net Salary)
structure.
a. DA is 30 % of Basic Pay
b. HRA is 15% of Basic Pay
c. Deduction is 10% of (Basic Pay + DA)
d. Gross Salary = Basic Pay + DA+ HRA
e. Net Salary = Gross Salary - Deduction
#include<stdio.h>
struct employee
{
int id;
char name[35];
Incrementing a Pointer
#include <stdio.h>
int main () {
Output
Address of var[0] = bf882b30
Value of var[0] = 10
Address of var[1] = bf882b34
Value of var[1] = 100
Address of var[2] = bf882b38
Value of var[2] = 200
Decrementing a Pointer
#include <stdio.h>
int main () {
return 0;
}
Output
Address of var[2] = bfedbcd8
Value of var[2] = 200
Address of var[1] = bfedbcd4
Value of var[1] = 100
Address of var[0] = bfedbcd0
14. Write a program to read the data character by character from a file.
#include<stdio.h>
main()
{
char ch;
FILE *f1;
f1=fopen("abc.txt","r");
if(f1==NULL)
printf("File not found \n");
else
{
printf("The content of file is \n");
while((ch=fgetc(f1))!=EOF)
{
printf("%c",ch);
}
}
Fclose(f1);
}
OUTPUT
Note :- Before executing above program, first you need to create abc.txt file and save it in
C:\Turboc2\ - directory
void display(void)
{
f1=fopen("book1.txt","r");
clrscr();
printf("Entered book details are \n");
if(f1==NULL)
{
printf("File not found");
exit(1);
}
while(fread(&b,sizeof(b),1,f1)==1)
{
printf("ISBN number =%d \n", b.isbn);
printf("Title = %s \n",b.title);
printf("Author = %s \n",b.author);
printf("Price = %d \n",b.price);
printf("Pages = %d \n",b.pages);
printf("Publisher = %s\n",b.publisher);
}
fclose(f1);
}
void search(void)
{
int isbn1;
int pos=-1;
f1=fopen("book1.txt","r");
clrscr();
printf("Entered isbn to search book details are \n");
scanf("%d",&isbn1);
if(f1==NULL)
void update(void)
{
FILE *f2=fopen("book2.txt","w+");
int isbn1,pos=-1;
f1=fopen("book1.txt","r+");
clrscr();
printf("Enter isbn to update book details are \n");
scanf("%d",&isbn1);
if(f1==NULL)
{
printf("File not found");
exit(1);
}
f1=fopen("book1.txt","r+");
clrscr();
printf("Enter isbn to delete book details are \n");
scanf("%d",&isbn1);
if(f1==NULL)
{
printf("File not found");
exit(1);
}
rewind(f1);
while(fread(&b,sizeof(b),1,f1)==1)
{
if(b.isbn==isbn1)
{
pos=0;
}
else
{
fwrite(&b,sizeof(b),1,f2);
}
}
fclose(f1);
f1=fopen("book1.txt","w+");
rewind(f2);
while(fread(&b,sizeof(b),1,f2)==1)
{
fwrite(&b,sizeof(b),1,f1);
}
if(pos==-1)
printf("File Not found to delete \n");
fclose(f1); fclose(f2);
}