Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

I Sem. B.Sc.

– Computer Science - Problem Solving in C LAB

1. Write a program to check whether the given number is Armstrong or not.


2. Write a program to find the sum of individual digits of a positive integer.
3. Write a program to generate the first n terms of the Fibonacci sequence.
4. Write a program to find both the largest and smallest number in a list of integer values
5. Write a program to demonstrate reflection of parameters in swapping of two integer
values using Call by Value&Call by Address
6. Write a program that uses functions to add two matrices.
7. Write a program to calculate factorial of given integer value using recursive functions
8. Write a program for multiplication of two N X N matrices.
9. Write a program to perform various string operations.
10. Write a program to search an element in a given list of values.
11. Write a program to sort a given list of integers in ascending order.
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

13. Write a program to illustrate pointer arithmetic.

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

Prepared by S.H. Khaseem M.C.A., M.B.A., Lecturer in Computer Science,KVSRDC, 9010797739 1


1. Write a program to check whether the given number is Armstrong or not.
_________________________________________________________________
Definition of Arms Strong number :-
Armstrong number is a number that is equal to the sum of cubes of its digits.
Example :- 153
13+53+33 is equivalent to the entered number 153. So it is an Armstrong number.
______________________________________________________________________
Program
/* program to check whether the given number is Armstrong or not */
#include<stdio.h>
main()
{
int num,x,rem,sum=0;
clrscr();
printf("Enter any Number \n");
scanf("%d",&num);
x=num;

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.

Prepared by S.H. Khaseem M.C.A., M.B.A., Lecturer in Computer Science,KVSRDC, 9010797739 2


Enter any Number
352
352 is Not an Armstrong number

2. Write a program to find the sum of individual digits of a positive integer.


____________________________________________________________________
Definition :- if entered number is positive then perform the sum of individual digit of a
given number other display message as “entered number is negative”;
Example:-
Input number = 257
Sum = 2 + 5 +7
________________________________________________________________________
/* program to perform the sum of the given positive number */
#include<stdio.h>
main()
{
int num,x,rem,sum=0;
clrscr();
printf("Enter any Number \n");
scanf("%d",&num);
x=num;

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);
}

Prepared by S.H. Khaseem M.C.A., M.B.A., Lecturer in Computer Science,KVSRDC, 9010797739 3


Output
Enter any Number
257
sum of the individual digit of 257 is 14

3. Write a program to generate the first n terms of the Fibonacci sequence.

#include <stdio.h>
main()
{
int i, n, t1 = 0, t2 = 1, nextTerm = 0;

printf("Enter the number of terms: ");


scanf("%d", &n);

printf("Fibonacci Series: ");

for (i = 1; i <= n; ++i)


{
/* Prints the first two terms.*/
if(i == 1)
{
printf("%d, ", t1);
continue;
}
if(i == 2)
{
printf("%d, ", t2);
continue;
}
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
printf("%d, ", nextTerm);
}

Prepared by S.H. Khaseem M.C.A., M.B.A., Lecturer in Computer Science,KVSRDC, 9010797739 4


}
Output
Enter the number of terms: 10
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34

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;

printf("\nEnter the size of the array: ");


scanf("%d",&size);
printf("\nEnter %d elements in to the array: ", size);
for(i=0;i<size;i++)
scanf("%d",&a[i]);

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

Prepared by S.H. Khaseem M.C.A., M.B.A., Lecturer in Computer Science,KVSRDC, 9010797739 5


5. Write a program to demonstrate reflection of parameters in swapping of two integer
values using Call by Value&Call by Address.

swapping of two integer values using Call by Value

#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;
}

void swap(int x, int y) // function definition


{
int temp;
temp = x;
x = y;
y = temp;
}

Output
Enter two numbers:
30
40
Before swapping
a = 30
b = 40
After swapping

Prepared by S.H. Khaseem M.C.A., M.B.A., Lecturer in Computer Science,KVSRDC, 9010797739 6


a = 30
b = 40

swapping of two integer values using Call by Reference

#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;
}

void swap(int *x, int *y) // function definition


{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
Output
Enter two numbers:
30
40
Before swapping
a = 30
b = 40
After swapping
a = 40
b = 30

Prepared by S.H. Khaseem M.C.A., M.B.A., Lecturer in Computer Science,KVSRDC, 9010797739 7


6. Write a program that uses functions to add two matrices.

#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);

printf("Enter number of rows \n");


scanf("%d", &r);
printf("Enter number of columns \n");
scanf("%d", &c);

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

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


for(j=0; j<c; ++j)
scanf("%d",&a[i][j]);

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


for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
scanf("%d", &b[i][j]);

addmatrices(a,b,r,c);
return 0;
}

void addmatrices(int x[100][100],int y[100][100],


int r,int c)
{
int sum[100][100],i,j;
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
sum[i][j]=x[i][j]+y[i][j];
}

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

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

Prepared by S.H. Khaseem M.C.A., M.B.A., Lecturer in Computer Science,KVSRDC, 9010797739 8


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

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

Output
Enter number of rows
2
Enter number of columns
2

Enter elements of 1st matrix:


10 20
30 40
Enter elements of 2nd matrix:
11
11
Sum of two matrix is:
11 21
31 41

Prepared by S.H. Khaseem M.C.A., M.B.A., Lecturer in Computer Science,KVSRDC, 9010797739 9


7. Write a program to calculate factorial of given integer value using recursive functions.

#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;
}

int fact(int n){


if(n==1)
return 1;
else
return(n*fact(n-1));
}
output
Enter a number: 5
Factorial of 5 is: 120

8. Write a program for multiplication of two N X N matrices.


#include <stdio.h>
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

Prepared by S.H. Khaseem M.C.A., M.B.A., Lecturer in Computer Science,KVSRDC, 9010797739 10


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");
}
}

}
Output

Enter the number of rows and columns of first matrix


2
2
Enter the elements of first matrix
11
11
Enter the number of rows and columns of second matrix
2

Prepared by S.H. Khaseem M.C.A., M.B.A., Lecturer in Computer Science,KVSRDC, 9010797739 11


2
Enter the elements of second matrix
11
11
Product of entered matrices:-
2 2
2 2
9. Write a program to perform various string operations.

#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");

/* copying string1 to string3 */


strcpy(str3,str1);
printf("Copied string is %s\n\n", str3);

/*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);
}

Prepared by S.H. Khaseem M.C.A., M.B.A., Lecturer in Computer Science,KVSRDC, 9010797739 12


Output
Enter string1 and string2
kvsr
college
Length of str1 = 4 str2 = 7
string comparison
Both strings are not equal
Copied string is kvsr
concatenated string is kvsrcollege
Reversed string is rsvk
10. Write a program to search an element in a given list of values.
#include<stdio.h>
void main()
{
int a[100],n,i,pos=-1,val;
printf("Enter the size \n");
scanf("%d",&n);
printf("Enter %d elements \n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the number to search \n");
scanf("%d",&val);
for(i=0;i<n;i++)
{
if(a[i]==val)
{
printf("Element found at %d\n",i+1);
pos=0;
}
}
if(pos==-1)
printf("Number not found");
}
Output
Enter the size
5
Enter 5 elements
10

Prepared by S.H. Khaseem M.C.A., M.B.A., Lecturer in Computer Science,KVSRDC, 9010797739 13


20
30
40
50
Enter the number to search
20
Element found at 2
11. Write a program to sort a given list of integers in ascending order.
#include <stdio.h>
#define MAX_SIZE 100

int main()
{
int array[MAX_SIZE];
int size;
int i, j, temp;

/* Input size of array */


printf("Enter size of array: ");
scanf("%d", &size);

/* Input elements in array */


printf("Enter elements in array: ");
for(i=0; i<size; i++)
{
scanf("%d", &array[i]);
}

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


{
/*
* Place the currently selected element array[i]
* to its correct place.
*/
for(j=i+1; j<size; j++)
{
/*
* Swap if currently selected array element
* is not at its correct position.
*/
if(array[i] > array[j])
{

Prepared by S.H. Khaseem M.C.A., M.B.A., Lecturer in Computer Science,KVSRDC, 9010797739 14


temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}

/* Print the sorted array */


printf("\nElements of array in sorted ascending
order: ");
for(i=0; i<size; i++)
{
printf("%d\t", array[i]);
}

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];

Prepared by S.H. Khaseem M.C.A., M.B.A., Lecturer in Computer Science,KVSRDC, 9010797739 15


int basic;
float da,hra,gs,ded,ns;
}emp[100];
void main()
{
int ch=0,i=0,j=0;
clrscr();
do
{
printf("Enter Employee Id,name,baic \n");
scanf("%d",&emp[i].id);
flushall();
gets(emp[i].name);
flushall();
scanf("%d",&emp[i].basic);
emp[i].da=emp[i].basic*30.0/100.0;
emp[i].hra=emp[i].basic*15.0/100.0;
emp[i].ded=(emp[i].basic+emp[i].da)*10.0/100.0;
emp[i].gs=emp[i].basic+emp[i].da+emp[i].hra;
emp[i].ns=emp[i].gs-emp[i].ded;
i++;
printf("Do you want contiue pre 1 to continue, 0-to
exit\n");
scanf("%d",&ch);
}while(ch==1);
printf("Emplyee details are \n");
for(;j<i;j++)
{
printf("Employee %d details are\n",j+1);
printf("Employee ID = %d\n",emp[j].id);
printf("Employee Name=%s\n",emp[j].name);
printf("Basic Pay=%d\n",emp[j].basic);
printf("Da=%f\n",emp[j].da);
printf("Hra=%f\n",emp[j].hra);
printf("Gross Salary=%f\n",emp[j].gs);
printf("Deductions =%f\n",emp[j].ded);
printf("Nest Salary=%f\n",emp[j].ns);

Prepared by S.H. Khaseem M.C.A., M.B.A., Lecturer in Computer Science,KVSRDC, 9010797739 16


}
}
Output

13. Write a program to illustrate pointer arithmetic.

Incrementing a Pointer
#include <stdio.h>

const int MAX = 3;

int main () {

int var[] = {10, 100, 200};


int i, *ptr;

/* let us have array address in pointer */


ptr = var;

for ( i = 0; i < MAX; i++) {

printf("Address of var[%d] = %x\n", i, ptr );


printf("Value of var[%d] = %d\n", i, *ptr );

/* move to the next location */


ptr++;
}

Prepared by S.H. Khaseem M.C.A., M.B.A., Lecturer in Computer Science,KVSRDC, 9010797739 17


return 0;
}

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>

const int MAX = 3;

int main () {

int var[] = {10, 100, 200};


int i, *ptr;

/* let us have array address in pointer */


ptr = &var[MAX-1];

for ( i = MAX; i > 0; i--) {

printf("Address of var[%d] = %x\n", i-1, ptr );


printf("Value of var[%d] = %d\n", i-1, *ptr );

/* move to the previous location */


ptr--;
}

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

Prepared by S.H. Khaseem M.C.A., M.B.A., Lecturer in Computer Science,KVSRDC, 9010797739 18


Value of var[0] = 10

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

15. Write a program to create Book (ISBN,Title, Author, Price, Pages,

Prepared by S.H. Khaseem M.C.A., M.B.A., Lecturer in Computer Science,KVSRDC, 9010797739 19


Publisher)structure and 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.
e. Displaying book details.
#include<stdio.h>
struct book
{
char title[35],author[35],publisher[30];
int isbn,pages,price;
}b,b1;
FILE *f1;
void search(void);
void update(void);
void delete1(void);
void add(void);
void display(void);
void main()
{
char ch='y',opt;
f1=fopen("book1.txt","w");
clrscr();
do
{
printf("a. Add book details \n");
printf("b. Search a book details for a given ISBN and
display book details,if available\n");
printf("c. Update a book details using ISBN \n");
printf("d. Delete book details for a given ISBN and display
list of remaining Books \n");
printf("e. disply \n");
printf("Enter your choice a/b/c/d/e press enter key to exit
\n");
flushall();
scanf("%c",&opt);

Prepared by S.H. Khaseem M.C.A., M.B.A., Lecturer in Computer Science,KVSRDC, 9010797739 20


switch(opt)
{
case 'a': add();
break;

case 'b': search();


break;

case 'c': update();


break;

case 'd': delete1();


break;

case 'e': display();


break;
default: printf("Exit from record operation \n");
break;
}
printf("Do you want to continue y/n \n");
scanf("%c",&ch);
}while(ch!='n');
}
void add(void)
{
f1=fopen("book1.txt","r+");
fseek(f1,0L,SEEK_END);
printf("Enter the ISBN Number,title,author, price, pages &
publisher \n");
scanf("%d",&b.isbn);
flushall();
gets(b.title);
gets(b.author);
flushall();
scanf("%d",&b.price);
scanf("%d",&b.pages);
flushall();

Prepared by S.H. Khaseem M.C.A., M.B.A., Lecturer in Computer Science,KVSRDC, 9010797739 21


gets(b.publisher);
fwrite(&b,sizeof(b),1,f1);
fclose(f1);
}

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)

Prepared by S.H. Khaseem M.C.A., M.B.A., Lecturer in Computer Science,KVSRDC, 9010797739 22


{
printf("File not found");
exit(1);
}
rewind(f1);
while(fread(&b,sizeof(b),1,f1)==1)
{
if(b.isbn==isbn1)
{
pos=0;
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);
}
}
if(pos==-1)
printf("File Not found \n");
fclose(f1);
}

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);
}

Prepared by S.H. Khaseem M.C.A., M.B.A., Lecturer in Computer Science,KVSRDC, 9010797739 23


rewind(f1);
while(fread(&b,sizeof(b),1,f1)==1)
{
if(b.isbn==isbn1)
{
pos=0;
printf("Enter the ISBN Number,title,author, price, pages &
publisher \n");
scanf("%d",&b.isbn);
flushall();
gets(b.title);
gets(b.author);
flushall();
scanf("%d",&b.price);
scanf("%d",&b.pages);
flushall();
gets(b.publisher);
fwrite(&b,sizeof(b),1,f2);
}
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);
}
void delete1(void)
{

Prepared by S.H. Khaseem M.C.A., M.B.A., Lecturer in Computer Science,KVSRDC, 9010797739 24


FILE *f2=fopen("book2.txt","w+");
int isbn1,pos=-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);
}

Prepared by S.H. Khaseem M.C.A., M.B.A., Lecturer in Computer Science,KVSRDC, 9010797739 25

You might also like