Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
56 views14 pages

Assignment: 2 CAP101 "Foundation OF Computer Programming"

This document contains the submission of an assignment for the course CAP101 "Foundation of Computer Programming". It includes 6 questions and their answers related to programming concepts like functions, recursion, lists, matrices, and menu driven programs. Mrs. Richa Malhotra submitted the assignment on behalf of Sakshi Bansal, with their student details provided. The assignment covers a variety of basic programming topics for an introductory computer science course.

Uploaded by

Puneet Bansal
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views14 pages

Assignment: 2 CAP101 "Foundation OF Computer Programming"

This document contains the submission of an assignment for the course CAP101 "Foundation of Computer Programming". It includes 6 questions and their answers related to programming concepts like functions, recursion, lists, matrices, and menu driven programs. Mrs. Richa Malhotra submitted the assignment on behalf of Sakshi Bansal, with their student details provided. The assignment covers a variety of basic programming topics for an introductory computer science course.

Uploaded by

Puneet Bansal
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 14

ASSIGNMENT: 2

CAP101
“FOUNDATION
OF
COMPUTER
PROGRAMMING”

SUBMITTED BY:
SUBMITTED
Mrs. Richa Malhotra SAKSHI BANSAL
E3004
A24
PART: A

Q:-1. Write a function itob(n, s,b) that converts integer ‘n’ into base ‘b’ character
representation into string ‘s’.E.g. Itob (n, s, 16) format ‘n’ as a hexadecimal integer
in‘s’.
Ans:-
#include<stdio.h>
#include<conio.h>
#include<math.h>
Void itob (int n, int s, int b)
{
Int i; s=0;
for (i=0; n! =0; i++)
{
s=s+ ((pow (10, i))*(n %b));
n=n/b;
}
Switch (b)
{
Case 2:
Printf ("\n the binary number is: %d", s);
Break;
Case 8:
Printf ("\n the octal number is: %d", s);
break;
Case 16:
Printf ("\n the hexadecimal number is: %d", s);
break;
}
}
Void main ()
{
int n, i ,s ,b;
clrscr ();
printf ("enter the value for number:");
scanf ("%d", &n);
printf ("enter the value which do you want to convert the no.");
scanf ("%d", &b);
itob (n, s, b);
getch ();
}

Q:-2. Write a program to find the sum of reciprocals of first n natural nos. using
recursion.
Ans: -
#include<stdio.h>
#include<conio.h>
void main ()
{
float reciprocal (float n);
float no, rec;
clrscr ();
printf("Enter the reciprocals: ");
scanf("%f",&no);
rec=reciprocal(no);
printf("\n sum of reciprocals is %f", no, rec);
getch();
}
float reciprocal(float n)
{ static float sum=0;
if(n==1)
{ return 1;
}
sum=sum+(1/ n);
reciprocal(n-1);
return sum;
}
Q:-3. Explain with example how a recursive function works?
Ans:-RECURSIVE FUNCTION :-
When a function calls itself then it is called recursive function. . Some problems are most easily
solved by recursion, usually those in which you act on data and then act in the same way on the
result. All those problems which can be solved by the looping control statements can be
implemented using recursion.

NECESSARY CONDITIONS :-

For the proper functioning recursion should be satisfied the following two conditions:

 There must exist at least one point which calls same function again.
 There must exist at least one another point which stops the further calling.
When these conditions will be followed in the function then this function will eventually end and
produce an answer.
When these conditions will not be followed then will be unexpected or the function will never
end and produce a runtime failure.

EXAMPLE :-

For example write a program to print the factorial of a given number:

#include<stdio.h>

#include<conio.h>

void main();

int no,fact;

int factorial(int n);

clrscr();

printf(“Enter the number to find the factorial:”);

scanf(“%d”,&no);

fact = factorial(no);

printf(“The factorial of the number is = %d”,fact);

getch();

int factorial(int n)
{ int f=1;

if(no==1)

return 1;

f=n*factorial(n-1);

return f;

In the statement the function is called as follows:

f=n*factorial(4)// f=24*5

f=n*factorial(3)// f=6*4

f=n*factorial(2)// f=2*3

f=n*factorial(1)//f=1*2

So, in the last the value of f=120, is returned to the Main() function and printed on the screen.

PART B

Q:-4. Write a program to insert an element in an unsorted list (on the specified
location).

Ans:-

#include<stdio.h>

#include<conio.h>

void main()

int i, list[20], t, no, loc;

printf("enter the limit for the list:");


scanf("%d",& t;

if(t<=0 || t >20)

Printf ("\n it should be <20");

exit();

printf("enter the %d elements:", t);

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

{ scanf("%d",&list[i]);

Printf ("enter the number and its location to insert");

scanf("%d%d",&no,&loc);

if(loc<0 || loc>t)

printf("Invalid location.");

exit();

t=t+1;

i=t;

while(i>loc)

list[i]=list[i-1];

i--;

}
list[i]=no;

printf("\nThe list after insertion:\n");

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

{ printf("\t%d",list[i]); }

getch();

Q:-5. Write a program to calculate:

a. Sum of diagonal elements of matrix.


b. Sum of second diagonal elements of matrix.
Ans:-#include<stdio.h>
#include<conio.h>
void main()
{
int matrix[10][10],r,c , i, j,sum=0;
clrscr();
printf("enter the number of rows and coloms");
scanf("%d%d",&r,&c);
if(r<0 || r>10 || c<0 || c>10 || r!=c)
{
printf("\n invalid rows and colomns:");
goto a;
}
printf("\nenter the elements of the matrix:\n");
for(i=1; i<=r; i++)
{
for(j=1; j<=c; j++)
{
scanf("%d",&matrix[i][j]);
}
}
printf("\nthe matrix is :\n");
for(i=1; i<=r; i++)
{
printf("\n");
for(j=1; j<=c; j++)
{
printf("\t%d",matrix[i][j]);
if(i==j)
{
sum = sum + matrix[i][j];
}
}
}
printf("\nthe sum of diagonal elements is = %d",sum);
a:
getch();
}

(b)
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,r,c,mat[10][10],sum=0;
clrscr();
printf("\nenter the rows and colomns for matrix:");
scanf("%d%d",&r,&c);
if(r<=0 || r>10 || c<=0 || c>10 || r!=c)
{
printf("\n Invalid rows and colomns .");
goto a;
}
Printf ("\n enter the elements of the matrix\n");
for(i=1; i<=r; i++)
{
for(j=1; j<=c; j++)
{ scanf("%d",&mat[i][j]); }
}
printf("\nthe matrix is :\n");
for(i=1; i<=r; i++)
{
printf("\n");
for(j=1; j<=c; j++)
{ printf("\t%d",mat[i][j]); }
}
for(i=1; i<=r; i++)
{
for(j=1; j<=c; j++)
{
if((i+j)==(c+1))
{ sum=sum+mat[i][j]; }
}
}
printf("\nthe some of diagnol matrix is : %d", sum);
a:
getch();
}

Q:-6. Write a menu driven program to perform addition, subtraction and


multiplication of two matrices.

Ans:-

#include<stdio.h>

#include<conio.h>

void enter_matrix(int m[10][10],int r,int c)


{

int i , j;

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

for(j=1; j<=c; j++)

scanf("%d",&m[i][j]);

void print_matrix(int m[10][10],int r,int c)

int i , j;

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

printf("\n");

for(j=1; j<=c; j++)

printf("\t%d", m[i][j]);

void main()
{

int i , j, k,s;

int no,mat1[10][10],mat2[10][10],r1,c1,r2,c2; clrscr();

printf("Enter the number of rows and colomns of 1st matrix:");

scanf("%d%d", &r1, &c1);

printf("Enter the number of rows and colomns of 2nd matrix:");

scanf("%d%d", &r2, &c2);

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

enter_matrix(mat1,r1,c1);

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

enter_matrix(mat2,r2,c2);

printf("\n The matrices are :\n");

printf("\n the first matrix=");

print_matrix(mat1,r1,c1);

printf("\n the second matrix is=");

print_matrix(mat2,r2,c2);

printf("\n\n MENU :\n\n");

printf ( "\n 1. Addition.");

printf ("\n 2. Subtraction.");

printf ("\n3. Multiplication");

printf("\n0. Exit\n\n");

do
{

printf("\n enters the no. From menu:\n");

scanf("%d",&no);

switch(no)

case 1:

if( r1!=r2 || c1!=c2 )

printf("\nsum is not possible ");

break;

printf("\n The sum of matrices is :\n");

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

printf("\n");

for(j=1; j<=c2; j++)

s=mat1[i][j]+mat2[i][j];

printf("\t%d", s);

break;
case 2:

if( r1!=r2 || c1!=c2)

printf("\n subtraction is not possible. ");

break;

printf("\n The subtraction of matrices is :\n");

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

printf("\n");

for(j=1; j<=c2; j++)

s=mat1[i][j] - mat2[i][j];

printf("\t%d", s);

}break;

case 3:

if(c2!=r1)

printf("\nmultiplication is not possible r1 should be =


c2.");

break;

}
printf("\n The product of matrices is :\n");

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

printf("\n");

for(j=1; j<=c2; j++)

s=0;

for(k=1; k<=r1; k++)

s=s+(mat1[i][k]*mat2[k][j]);

printf("\t%d",s);

Break;

getch();

You might also like