0% found this document useful (0 votes)
1 views

KCS101.201 Programming

The document contains programming exercises and solutions for B.Tech. students at Goel Institute of Technology & Management, focusing on problem-solving in C. It includes programs for matrix operations, recursion, searching, sorting, string manipulation, Fibonacci series, and file handling. Each section provides a question followed by a code implementation and explanations of concepts such as recursion and searching.

Uploaded by

piyush123sriva
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

KCS101.201 Programming

The document contains programming exercises and solutions for B.Tech. students at Goel Institute of Technology & Management, focusing on problem-solving in C. It includes programs for matrix operations, recursion, searching, sorting, string manipulation, Fibonacci series, and file handling. Each section provides a question followed by a code implementation and explanations of concepts such as recursion and searching.

Uploaded by

piyush123sriva
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Goel Institute of Technology & Management, Lucknow

B.Tech., Ist Year


Programming for Problem Solving (KCS-101/201)

Ques. Write a program to add two matrices?


Ans. // program to add two matrices
# include <stdio.h>
# include <conio.h>
void main()
{ int a[10][10],b[10][10],c[10][10];
int i, j, row,col;
clrscr();
printf(‘’Enter rows and & cols of matrix’’);
scanf (‘’ %d%d’’,&row,&col);

printf(‘’Enter elements of first matrix’’);


for (i=0;i<row;++i)
for (j=0;j<col;++j)
scanf (‘’%d’’,&a[i][j]);

printf(‘’Enter elements of second matrix’’);


for (i=0;i<row;++i)
for (j=0;j<col;++j)
scanf (‘’%d’’,&b[i][j]);

// add two matrices // multiply two matrices


for (i=0;i<row;++i) for (i=0;i<row;++i)
for (j=0;j<col;++j) for (j=0;j<col;++j)
c[i][j]=a[i][j]+b[i][j]; { c[i][j]=0;
for (k=0;k<row;++k)
printf (‘’sum of two matrix is\n’’); c[i][j]=c[i][j]+a[i][k]*b[k][j];
for (i=0;i<row;++i) }
{ for (j=0;j<col;++j)
printf (‘’%d\t”,c[i][j]);
printf(‘’\n’’);
}
getch();
}

Ques. What is Recursion? Write recursive program to find factorial of n?


Ans. Recursion is the process, in which a function calls itself repeatedly until some specified condition has
been satisfied.
There are two types of recursion
1) Direct recursion
2) Indirect recursion
Following are the rules or principle of recursion
1) Base Case
1|Page
Goel Institute of Technology & Management, Lucknow
B.Tech., Ist Year
Programming for Problem Solving (KCS-101/201)

2) Making progress
3) Design Rule
4) Compound interest rule

Program to find factorial using recursion


# include <stdio.h>
int fact(int n)
{ if (n<=1)
return 1;
else
return fact(n-1)*n;
}

void main()
{ int n;
scanf(‘’%d’’, &n);
printf (‘’factorial= %d’’, fact(n));
}

Ques- what is searching? Write a program to search an element in an array?


Ans- Searching is the process of finding the location of an element in a data structure. There are two types of
searching:
i) Linear Search
ii) Binary Search
Program
# include <stdio.h>
void main()
{ int a[100], n, i, key;
printf (‘’enter nos. of element in array’’);
scanf (‘’%d’’, &n);
printf (‘’enter array element’’);
for (i=0;i<n;++i)
scanf (‘’%d”,&a[i]);
printf (‘’enter key you went to search’’);
scanf (‘’%d”,&key);
for (i=0;i<n;++i)
if (key==a[i]) break;

if (key==a[i])
printf (“element is found at loc %d”,i+1);
else
printf(“element not found”);
}
2|Page
Goel Institute of Technology & Management, Lucknow
B.Tech., Ist Year
Programming for Problem Solving (KCS-101/201)

Ques- what is sorting? Write a program to sort an array of nos?


Ans- Sorting is the process of arranging elements of a data structure in a given order i.e. ascending or
descending.
Program (Selection Sort)
#include <stdio.h>
void main()
{ int a[100], n, i, j, min, loc, temp;
printf(“Enter number of elements”);
scanf(“%d“,&n);

for(i=0;i<n;++i) //read elements


scanf(“%d“,&a[i]);

for(i=0;i<=n-1;i++)
{ min=a[i];
loc=i;
for (j=i+1;j<=n-1;j++)
{ if (a[j]<min)
{ min=a[j];
loc=j;
}
}
if (loc!=i)
{ temp= a[i];
a[i]=a[loc];
a[loc]=temp;
}
}
printf (“sorted array is\n”);
for(i=0; i<n; ++i)
printf (“%d\n”,a[i]);
}

Ques- String Manipulation


1) int strlen (char s[]) // to find length of string
{ int i;
for(i=0; s[i]!=’\0’; ++i)
;
return i;
}

3|Page
Goel Institute of Technology & Management, Lucknow
B.Tech., Ist Year
Programming for Problem Solving (KCS-101/201)

2) void strcpy (char s1[], char s2[]) //copy string s2 in s1


{ int i;
for(i=0; s2[i]!=’\0’; ++i)
s1[i]= s2 [i];
s1[i]=’\0’;
}
3) void strcat (char s1[], char s2[]) // concatenate s1=s1||s2
{ int i, j;
//find length of s1
for(i=0; s1[i]!=’\0’; ++i)
;
for(j=0; s2[j]!=’\0’; ++j, ++i)
s1[i]= s2 [j];
s1[i]=’\0’;
}
4) void strrev (char s[]) // reverse string s
{ int i, j;
char c;
for(i=0; s[i]!=’\0’; ++i)
;
for(j=0, i=i-1; j<i ;++j, --i)
{ c= s[i];
s[i]=s[j];
s[j]=c;
}
}
5) // program to check whether given string is palindrome
#include <stdio.h>
void main()
{ char s[100];
int i, j, flag=1;
gets(s); //read string to check for palindrome
for(i=0; s[i]!=’\0’;++i) //find length of string
;
for (j=0,i=i-1;j<i;++j,--i)
If (s[i]!=s[j])
{ flag=0;
break;
}
if (flag==1)
printf (“string is palindrome”);
else
4|Page
Goel Institute of Technology & Management, Lucknow
B.Tech., Ist Year
Programming for Problem Solving (KCS-101/201)

printf (“string is not palindrome”);


}
Ques- WAP to generate first N terms of Fibonacci series?
#include <stdio.h>
void main()
{ int i, n, fib0=0, fib1=1, fibn;
printf(“enter how many terms you want to generate”);
scanf(“%d”, &n);
for (i=1;i<=n;++i)
{ printf (“%d\t”,fib0);
fibn=fib0+fib1;
fib0=fib1;
fib1=fibn;
}
}

Ques- WAP to find reverse of a no. N=1234 o/p 4321


#include <stdio.h>
void main()
{ int n, rev, temp;
scanf(“%d“, &n);
rev=0;
while (n>0)
{ temp=n%10;
rev= rev*10+temp;
n=n/10;
}
printf (“reverse in=%d”,rev);
}

Ques- WAP to find sum of digit to a single digit N=9999 sum= 9+9+9+9=36 =3+6=9 o/p =9
#include <stdio.h>
void main()
{ int n, sum=0;
scanf (“%d“, &n);
do
{ while (n>0)
{ sum=sum+n%10;
n=n/10;
}
n=sum;
sum=0;
}
5|Page
Goel Institute of Technology & Management, Lucknow
B.Tech., Ist Year
Programming for Problem Solving (KCS-101/201)

while (n>9);
printf(“sum of series=%d”, n);
}

Ques- WAP to pattern printing?


1) *
**
***
****

#include<stdio.h>
void main()
{ int i, j, n;
scanf (“%d”, &n);
for (i=1; i<=n; ++i)
{ for (j=1; j<=i; ++j)
printf (“*”);
printf (“\n”);
}
}

2) 1
12
123
1234
#include <stdio.h>
void main()
{ int i, j, n;
scanf (“%d”, &n);
for (i=1; i<=n; ++i)
{ for (j=1; j<=n-i; ++j)
printf (“ ”);
for (j=1; j<=i; ++j)
printf (“%d”, j%10);
printf(“\n”);
}
}

3) a b c d e
abcd
abc
ab
a
6|Page
Goel Institute of Technology & Management, Lucknow
B.Tech., Ist Year
Programming for Problem Solving (KCS-101/201)

#include <stdio.h>
void main()
{ int i, j, n;
char c;
scanf (“%d”,&n);
for (i=1; i<=n; ++i)
{ c=’a’;
for (j=i;j<=n;++j)
printf (“%c”, c++);
printf (“\n”);
}
}

4) * * * * *
****
***
**
*
#include <stdio.h>
void main()
{ int i, j, n;
scanf (“%d”,&n);
for (i=1;i<=n;++i)
{ for (j=1; j<=i; ++j)
printf (“ “);
for (j=0; j<=n-i; ++j)
printf(“*”);
printf (“ \n”);
}
}

Ques- find sum of series?


1) S= -13+33-53+73………..50 terms

#include<math.h>
#include<stdio.h>
void main()
{ int sum=0, sign=-1, i, j;
j=1;
for (i=1;i<=50;++i)
{ sum= sum+pow(j*sign,3);
sign= sign*-1;
j=j+2;
7|Page
Goel Institute of Technology & Management, Lucknow
B.Tech., Ist Year
Programming for Problem Solving (KCS-101/201)

}
printf (“ sum of series=%d”,sum);
}

2) S=1+1/2+1/3+1/4+………….+1/20
#include <stdio.h>
void main()
{ int i;
float sum= 0.0;
for (i=1;i<=20;++i)
sum= sum+1.0/i;
printf (“sum=%g”,sum);
}

3) S=1/1!+2/2!+3/3!+………….10 terms
#include <stdio.h>
void main()
{ int i, j, fact;
float sum= 0.0;
for(i=1; i<=10; ++i)
{ fact=fact*1;
for (j=1; j<=i; ++j)
fact= fact*j;
sum=sum+1.0*i/fact;
}
printf (“sum of series=%g”, sum);
}

Ques- Flowchart and program to find maximum of three numbers?


#include<stdio.h>
void main()
{ int a, b, c, great;
scanf (“%d%d%d”,&a,&b,&c);
if (a>b)
great=a;
else
great=b;

if (great<c)
great=c;

printf (“%d”, great);


8|Page
Goel Institute of Technology & Management, Lucknow
B.Tech., Ist Year
Programming for Problem Solving (KCS-101/201)

Flowchart- Start

Input A, B, C

If A<B
Yes No

Great= A Great= B

If
great<c

No Yes

Great= C

Print great

Stop

Ques- WAP to find transpose of matrix?


#include <stdio.h>
#include <conio.h>
void main()
{ int a[10][10],b[10][10];
int i, j, row, col;
clrscr();
scanf (“%d %d”, &row, &col);
printf(“enter elements of matrix”);
for (i=0; i<row; ++i)

9|Page
Goel Institute of Technology & Management, Lucknow
B.Tech., Ist Year
Programming for Problem Solving (KCS-101/201)

for (j=0; j<col; ++j)


scanf (“%d”, &a[i][j]);

// find transpose
for (i=0; i<row; ++i)
for (j=0; j<col; ++j)
b[j][i]=a[i][j];
//print transpose
for (i=0; i<row; ++i)
{ for (j=0; j<col; ++j)
printf (“%d\t”, b[i][j]);
printf (“\n”);
}
getch();
}

Ques- How do we open and close a data file? Show with example?
Ans- Following steps must be followed to open a data file
Step1- Include header file <stdio.h>
Step2- Establish a buffer area for data file
FILE * ptvar;
Step3- Open data file using library function fopen
ptvar = fopen(fille-name, file-type)

Where file name and file type is strings.


File name is name of file you want to open while file type is mode (read, write or append) for
which you want to open file.
 If file is open, it is copied in buffer area and starting address is stored in variable ptvar.
 In case of error, ptvar is assigned null value.
Step4- Close the data file using library function fclose.
fclose (ptvar);

Example-
#include <stdio.h>
void main()
{ FILE *ptvar;
ptvar= fopen(“abc.dat”,”w+”);
If (ptvar==NULL)
printf (“error- cannot open file”);
else
{ //perform operation on file

10 | P a g e
Goel Institute of Technology & Management, Lucknow
B.Tech., Ist Year
Programming for Problem Solving (KCS-101/201)

fclose (ptvar);
}
}

Ques- WAP to copy contents of one file to another?


#include <stdio.h>
void main()
{ FILE *f1,*f2;
char c;
f1= fopen(“a.dat”,”r”);
if (f1==NULL)
printf (“error: file not found”);
else
{ f2= fopen (“copy.dat”,”w”);
while (!feof(f1))
{ c= getc(f1); //copy one char from a.dat
putc(c,f2); //write that char in copy .dat
}
fclose (f1);
fclose (f2);
}
}

Ques- WAP to append data at end of file?


#include <stdio.h>
void main()
{ FILE *f1;
f1= fopen(“a.dat”,”a”);
if (f1==NULL)
printf (“error: file not found”);
else
{ fprintf (f1, “Hello World”); //print hello world at end of file
fclose(f1);
}
}

11 | P a g e
Goel Institute of Technology & Management, Lucknow
B.Tech., Ist Year
Programming for Problem Solving (KCS-101/201)

Ques- WAP to read numbers from one file and print squares in another file?
#include <stdio.h>
void main()
{ FILE *f1, *f2;
int n;
f1= fopen(“nos.dat”, ”r”);
if (f1==NULL)
printf (“error: file not found”);
else
{ f2= fopen (“squares.dat”, ”w”);
while (!feof(f1))
{ fscanf (f1,”%d”,&n);
fprintf (f2,”%d\n”,n*n);
}
fclose(f1);
fclose(f2);
}
}

Ques- write a program to read N numbers from keyboard, store even numbers in file even.dat and odd
numbers in file odd.dat?
#include <stdio.h>
void main()
{ FILE *f1,*f2;
int n, i, x;

f1= fopen(“even.dat”,”w”);
f2= fopen(“odd.dat”,”w”);
scanf (“%d”, &n);
for (i=1; i<=n; ++i)
{ scanf (“%d”,&x);
if (x%2==0)
fprintf (f1, ”%d\n”, x);
else
fprint (f2, ”%d\n”, x);
}
fclose (f1);
fclose (f2);
}

12 | P a g e

You might also like