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

Programming in C Question Bank Solution 1-1

The document contains various programming exercises and algorithms related to computer programming fundamentals, including flowcharts, algorithms for finding the largest number, matrix multiplication, and complex number operations. It also includes programming tasks for calculating interest, summing squares, and manipulating strings. Additionally, it discusses identifiers, error correction in code, and the use of pointers and structures in C programming.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Programming in C Question Bank Solution 1-1

The document contains various programming exercises and algorithms related to computer programming fundamentals, including flowcharts, algorithms for finding the largest number, matrix multiplication, and complex number operations. It also includes programming tasks for calculating interest, summing squares, and manipulating strings. Additionally, it discusses identifiers, error correction in code, and the use of pointers and structures in C programming.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

Subject: Fundamental of Computer Programming Year: 2065

1. Draw the flow chart for finding largest of three numbers and write an algorithm and
explain it.
Ans:
Algorithm
Step 1: Start
Step 2: Declare variables a, b, c
Step 3: Read values of a, b, c
Step 4: if a > b
If a > c
Print a is the greatest number
Else
Print c is the greatest number
Else
If b > c
Print b is the greatest number
Else
Print c is the greatest number
Step 5: Stop

start

Declare variables a,b,c

Read a,b,c

Is
a>b
?

Is Is
b>c a>c
? ?

a l
Print c Print c Print c

e p
T N
End
S I
C
Source: www.csitnepal.com
2. Find the value of “a” in each of the following statements.
int i=3 , j=4, k=8;
float a =4.5, b = 6.5, c = 3.5;
(a) a = b –i/k+ c/k
(b) a = (b-k)/j + (j+c)/k
(c) a=c-((i+j)/(k+i))*b
(d) a=c –i+j/k+i*b
(e) a=c+j%2+b
(f) a= (b+1)%(c+1)

Ans:
A) a = b-i/k+c/k
= 6.5-3/8+3.5/8
= 6.5-0+0.4375
= 6.9375
B) a = (b-k)/j+(j+c)/k
= (6.5-8)/4+(4+3.5)/8
= -0.375+0.9375
= 0.5625
C) a = c-((i+j)/(k+i))*b
= 3.5-((3+4)/(8+3))*6.5
= 3.5-0*6.5
= 3.5
D) a = c-i+j/k+i*b
= 3.5-3+4/8+3 *6.5
= 3.5-3+0+19.5
= 0.5 + 19.5
= 20
E) a = c+j%2+b
= 3.5+4%2+6.5
= 3.5+0+6.5
= 10
F) a = (b+1)%(c+1)
= (6.5+1)%(3.5+1)
= 7.5%4.5
= Not valid
3. Write a program for the interest charged in installments for following case. A
cassette player costs Rs. 2000. A shopkeeper sells it for Rs. 100 down payment and Rs.
100 for 21 more months. What is the monthly interest Charged?

#include<stdio.h>
a l
#include<conio.h>
e p
void main()
{
T N
S I
C
Source: www.csitnepal.com
float c_price,t_price,t_interest,m_interest; //cost price, total price, total interest,
monthly interest
c_price=2000;
t_price=100+(100*21);//down payment Rs. 100 and Rs. 100 for 21 installments
t_interest=(t_price-c_price)/c_price*100;
m_interest=t_interest/22;//payment is for 22 months
printf("\nThe monthly interest charged is %.2f",m_interest); //result is 0.45% per
month
getch();
}

4. Write a program that uses a “for” loop to compute and prints the sum of a given numbers
of squares.
Ans:

#include<stdio.h>
#include<conio.h>
void main()
{
int i, n, sum=0;
printf(“Enter the number:”);
scanf(“%d”,&n);
for(i=1;i<n;i++)
{
sum=i*i+sum;
}
printf(“\n Sum=%d”,sum);
getch();
}

5. Write a program to obtain the product of the following matrices and explain it;
Ans:

#include<stdio.h>
#include<conio.h>
void main
{
int a[3][3]={{3,5,7},{2,-3,4,{4,5,2}};
int b[3][2]={{7,6},{6,-5},{4,3}};
int i,j,k,c[3][2];
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
c[i][j]=0;
a l
for(k=0;k<3;k++)
e p
N
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}

S I T
C
Source: www.csitnepal.com
}
}
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf(“%d”,c[i][j]);
}
printf(“\n”);
}
getch();
}

6. Write a function to add, subtract, multiply and divide two complex numbers(x+iy) and
(c+id).
Ans:

#include<stdio.h>
#include<conio.h>
void add(int,int,int,int);
void sub(int,int,int,int);
void mul(int,int,int,int);
void div(int,int,int,int);

void main()
{
Int x,y,c,d,I;
printf(“Enter real and imaginary part1:”);
scanf(“%d%d”,&x,&y);
printf(“Enter real and imaginary part2:”);
scanf(“%d%d”,&c,&d);
add(x,y,c,d);
sub(x,y,c,d);
mul(x,y,c,d);
div(x,y,c,d);
getch();
}
void add(int r1,int ip1, int r2,int ip2)
{
int realsum,imgsum;
realsum=r1+r2;
imgsum=ip1+ip2;
printf(“The sum of complex numbers=%d+i(%d)\n”,realsum,imgsum);
}
void sub(int r1,int ip1, int r2,int ip2)
{
a l
int realsub,imgsub;
realsub=r1-r2;
e p
imgsub=ip1-ip2;

T N
printf(“The subtraction of complex numbers=%d+i(%d)\n”,realsub,imgsub);
}

S I
C
Source: www.csitnepal.com
void mul(int r1,int ip1, int r2,int ip2)
{
int realmul,imgmul;
realmul=r1*r2;
imgmul=ip1*ip2;
imgmul1=r1*ip2;
imgmul2=r2*ip1;
printf(“The multiplication of complex numbers=
%d+i(%d+%d)%d\n”,realmul,imgmul1,imgmul2,imgmul);
}

void add(int r1,int ip1, int r2,int ip2)


{
int realsum,imgsum;
realsum=r1+r2;
imgsum=ip1+ip2;
printf(“The sum of complex numbers=%d+i(%d)\n”,realsum,imgsum);
}

7. Write a program which will read a line and delete from it all occurrences of the
word “that”.
Ans:

#include<stdio.h>
#include<conio.h>
void main()
{
char str[]=”I that love c that programming.”;
int i;
for(i=0;str[i]!=’\0′;i++)
{

if(str[i]==116&&str[i+1]==104&&str[i+2]==97&&str[i+3]==116&&str[i+4]==32)
{
for(;str[i+5]!=’\0′;i++)
str[i]=str[i+5];
str[i]=’\0′;
i=0;
}
}
printf(“%s”,str);
getch();
}

a l
e p
8. What is a pointer and explain its applications? Write a program that uses pointers to copy
an array of double.
Ans:
T N
S I
C
Source: www.csitnepal.com
A pointer is a variable, which contains the address of another variable in memory. We can
have a pointer to any variable type pointers are said to “point to “the variable whose
reference they store.
#include<stdio.h>
#include<conio.h>
double *copy(double a[],int n);
void main()
{
double a[10],*b;
int i;
for(i=0;i<10;i++)
{
scanf("%lf",&a[i]);
}
b=copy(a,10);
for(i=0;i<10;i++)
{
printf("\n%lf",b[i]);
}
getch();
}
double *copy(double a[], int n)
{
double *p;
int i;
for(i=0;i<10;i++)
{
p[i]=a[i];
}
return p;
}

9. Define a structure of employee having data members name, address, age, and salary. Take
data for n employee in an array dynamically and find the average salary.
Ans:

#include <stdio.h>
#include<conio.h>
#include<alloc.h>
struct employee
{
char name[50];
a l
char add[50];
int age;
e p
int salary;

T N
};

S I
C
Source: www.csitnepal.com
void main()
{
struct employee *p;
int i, n;
float avg,sum=0;
printf("Enter the number of employee:");
scanf("%d", &n);
p=(struct employee *) calloc (n,sizeof(struct employee));
if(p==NULL)
exit();
for(i=0;i<n;i++)
{
fflush(stdin);
printf("Enter name:");
gets((p+1)->name);
printf("Enter address:");
gets((p+i)->add);
printf("Enter age:");
scanf("%d",&(p+i)->age);
fflush(stdin);
printf("Enter salary:");
scanf("%d",&(p+i)->salary);
}
for(i=0;i<n;i++)
{
sum=sum+(p+i)->salary;
}
avg=sum/n;
printf("Average salary=%f",avg);
getch();
}
10. Given a text file, create another text file deleting the following words “three”,
“bad”, and “time”.
OR
Why do you require graphical function? Explain the basic graphical function with
suitable program.
Ans:

#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char str;
fp=fopen("old.txt","r+");
while(fscanf(fp,"%s",str)!=EOF)
a l
{

e
if(strcmp(str,"three")==0||strcmp(str,"bad")==0||strcmp(str,"time")==0) p
{
printf("");
T N
}
S I
C
Source: www.csitnepal.com
else
printf("%s ",str);
}
fclose(fp);
getch();
}

OR

Graphical function in c is used to implement various graphical symbols and shapes. There are
various types of graphics function used. Such as
a. Line(x1,y1,x2,y2)

This function is to draw line from (x1,y1) to (x2,y2)

b. Circle(x,y,r)

This function is to draw circle of radius r and center (x,y)

c. arc(int x, int y, int stangle, int endangle, int radius);

arc function is used to draw an arc with center (x,y) and stangle specifies starting
angle, endangle specifies the end angle and last parameter specifies the radius of
the arc. arc function can also be used to draw a circle but for that starting angle and
end angle should be 0 and 360 respectively.

a l
e p
T N
S I
C
Source: www.csitnepal.com
Subject: Fundamental of Computer Programming Year: 2066

1. Why flow chart is required? Explain different symbols used in the flow chart and
explain with suitable example.
Ans:
A flowchart is required because of following reasons:
a. It is a better way of communicating the logic of a system to all
concerned .
b. Problem can be analyzed in more effective way.
c. It acts as a guide or blueprint during the system analysis and program
development phase.
d. It helps in debugging process.

2. Determine which of the following are valid identifiers? If invalid, explain


why?
(a) record1
(c) file_3
(e) #tax
(g) goto
(i) name-and-address
(k) void
Ans:
record1
→This is valid identifier.
file_3
→This is valid identifier.
#tax
→Invalid identifier because identifier must begin with a letter and
special character like # is not permitted in identifiers.
goto
→Invalid identifier because keywords cannot be used as an identifier .
Name-and-address
→Invalid identifier because hyphen or dash is not permitted.
void
→Invalid identifier because keywords cannot be used as an identifier.
3. Find the error in the given program and explain it. Write the correct
program.

#include<stdio.h>
Include<math.h>
main
{
Float p,r,n;
a l
printf(“Please enter a value for the principal(P):”);
e p
scanf(%f,&p):
printf(“Please enter a value for the interest rate(R));
T N
scanf(“%f’,&r);

S I
C
Source: www.csitnepal.com
printf(“Please enter a value for the number o f years(n):”);
scanf(“%f,n);
f = p *pow +r/100),n);
printf(\n The final value(F) is: %.2f \n”,f);
Ans:
#include<stdio.h>
#include<math.h>
void main()
{
float p,r,n,f;
printf(“Please enter a value for the Principal(P)”);
scanf(“%f”,&p);
printf(“Please enter a value for the Interest Rate(R)”);
scanf(“%f”,&r);
printf(“Please enter a value for the number of years(n)”);
scanf(“%f”,&n);
f=pow((p+r/100),n);
printf(“\n The final value is: %f \n”,f);
}

4. Write a program that uses a “while” loop to c ompute and prints the sum
of a given numbers of squares.For example, if 4 is input, then the program
will print 30,which
is equal to 1 2 +2 2 +3 2 +4 2 .
Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
int i=0,n,sum=0;
printf (“Enter the number:”);
scanf(“%d”,&n);
while (i<n)
{
sum=i*i+sum;
i++;
}
printf(“\n Sum=%d”,sum);
getch();
}
5. Write a program to enter two 3x3 matrices and calculate the
product of given matrices.
a l
Ans:
e p
#include<stdio.h>
#include<conio.h>
T N
void main()

S I
C
Source: www.csitnepal.com
{
int a[3][3],b[3][3],c[3][3];
int i,jk;
printf(“Enter matrix A: \n”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“Enter matrix B: \n”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”,&b[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
C[i][j]=0;
for(k=0;k<3;k++)
{
C[i][j]+=a[i][k]*b[k][j];
}
}
}
printf(“\n Product of two matrix: \n”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(“%d”,c[i][j]);
}
printf(“\n”);
}
getch();
}
6. Explain the use of two dimensional arrays. Illustrate it with
suitable program and explain it.
a l
Ans:
e p
A two-dimensional array is arrays which are arranged in two
N
directions: horizontal and vertical. The data items arranged in horizontal
T
S I
C
Source: www.csitnepal.com
direction are referred as rows and the data items arranged in vertical
direction are referred as co lumns.

#incldue<stdio.h>
#incldue<conio.h>
void main()
{
int A[4][4];
printf(“Enter any 2 2 matrix\n”);
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
scanf(“%d”,&A[i][j]);
}
}
printf(“Enterd matrix is \n”);
for(i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
printf(“%3d”,A[i][j]);
}
}
getch();
}

7. Write and test the following power () function that returns x raised to
the power n,
where n can be any integer:
double power(double x, int p)
Ans:
#include <stdio.h>
double power(double x, int p);
void main()
{
double x = 0.0;
int p = 0;
for(x = 2.0 ; x<= 5.0; x ++)
{
for(p = 0 ; p<5 ;p++)

printf("%.2lf raised to the power %d = %.2lf\n", x,p, power(x,p));


a l
}
e p
}
double power(double x, int p) {
T N
if(p == 0)

S I
C
Source: www.csitnepal.com
return 1.0;
else
return x * power( x , p - 1 );
}
9. Why structure is required? Make a program using structure of booklist
having data
member’s title, author, and cost. Enter four data and calculate total cost.
Ans:
A structure is required because it provides a means of grouping
variables under a single name for easier handling and identification.
#include<stdio.h>
#include<conio.h>
struct book
{
char title[50];
char author[50];
int cost;
};
void main()
{
struct book P;
int i,sum=0;
for(i=0; i<4;i++)
{
printf(“Enter book title \n”);
gets(P[i].title);
printf(“Enter Author name: \n”);
gets(P[i].author);
printf(“Enter cost of book: \n”);
scanf(“%d”,&P[i].cost);
sum=sum+P[i].cost;
}
printf(“Total cost of books:”,%d”sum);
getch();
}
10. Create a text file and enter “to write a good program is very
time consuming job. “Create another text which contains reverse of above
text.
Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
a l
FILE *fp,*f;
e p
int i;
T N
char str[]= "To write a good prog ram is very time consuming";

fp=fopen("old.txt","w");

S I
C
Source: www.csitnepal.com
fputs(str,fp);
f=fopen("new.txt","w");
for(i=strlen(str);0<i;i --)
fputc(str[i],f);
fclose(fp);
fclose(f);
}
OR
C graphical function is used to draw different shapes, display text in different fonts,
change colors and many more. Using functions of graphics.h we can make graphics
programs, animations, projects and games. We can draw circles, lines, rectangles, bars and
many other geometrical figures.
Example:

#include<stdio.h>
#include<graphics.h>
#include<conio.h>

Void main()
{
int gd = DETECT, gm;

initgraph(&gd, &gm, "C:\\TC\\BGI");


line(100, 100, 200, 200);
arc(100, 100, 0, 135, 50);
circle(100,100,50);
getch();
closegraph();
}

a l
e p
T N
S I
C
Source: www.csitnepal.com
Subject: Fundamental of Computer Programming
2067

1. Draw the flowchart for the solution of a quadratic equation and write algorithm and explain
it.
Ans:

Algorithm:

Step 1: Input a, b, c
Step 2: d = sqrt (b2 – 4 x a x c
Step 3: x1 = (–b + d) / (2 x a)
Step 4: x2 = (–b – d) / (2 x a)
Step 5: Print x1 and x2

Flowchart:

2. Find the value of “a” in each of the following statements.


Int i=2,j=5,k=7;
Float a=1.5,b=2.5,c=3.5;
(a) a=c-i/j+c+k
(b) a=(c-i)/k+(j+b)/k
a l
(c) a=b*b-((ai+j)/c)
e p
(d) a=b-k+j/k+i*c
(e) a=c+k%2+b
T N
(f) a=(b+4)%(c+2)
S I
C
Source: www.csitnepal.com
Same as 2065 question no2.

3. A machine is purchased which will produce earning of Rs. 20000 per year while it lasts. The
machine costs Rs. 120000 and will have a salvage value of Rs. 20000 when it is condemned. If
the 12 percent per annum can be earned on alternative investmenst what should be the
minimum life of the machine to make it a more attractive investment compared to
alternative investements?
Ans:
Do yourself

4. Write a program to transpose the following matrix.

Do yourself

5. Write a program that uses a do…while loop to compute and prints the sum of squares given
n numbers.
Ans:
#include < stdio.h >
void main()
{
Int n;
int sum=0;
printf(“upto which number?:”);
scanf(“%d”,&n);
do
{
sum+=(n*n);
n--;
}
while(n>0);
printf(“sum=%d”,sum);
getch();
}

6. Write a function to multiply two nxn matrices.


Ans:
a l
#include<stdio.h>
int main() e p
{
T N
int i,j,l;
S I
C
Source: www.csitnepal.com
int matrix[3][3],matri[3][3],matr[3][3]={{0,0,0},{0,0,0},{0,0,0}};
printf("Enter 1st matrix: \n");
for (i=0;i<3;++i)
{
printf("\nEnter #%d row: ",(i+1));
for (j=0;j<3;++j)
scanf_s("%d",&matrix[i][j]);
}
printf("\n\n");
printf("Enter 2nd matrix: \n");
for (i=0;i<3;++i)
{
printf("\nEnter #%d row: ",(i+1));
for (j=0;j<3;++j)
scanf_s("%d",&matri[i][j]);
}
for (j=0;j<3;++j)
{
for (i=0;i<3;++i)
{
for (l=0;l<3;++l)
matr[i][j] = matr[i][j] + (matrix[i][l])*(matri[l][j]);
}
}
printf("The resultant matrix is:\n\n");
for (i=0;i<3;++i)
{
for (j=0;j<3;++j)
printf("%4d",matr[i][j]);
printf("\n\n");
}
return( 0 );
}

7. Write a program to count the number of words in a sentence.


Ans:
#include<stdio.h>
#include<string.h>
a l
void main()
ep
{

T N
int count,i,len;

S I
C
Source: www.csitnepal.com
char str[100];
printf("enter the sentence");
gets(str);
len=strlen(str);
for(i=0;i<=len;i++)
{
while(str)
if(str[i]==' ') count++;
}
printf("the number of words are :\t%d",count+1);
}

8. Why pointer is called jewel of C language? Write a program that uses pointers to copy an
array of integer.
Ans:

In c a pointer is a variable that points to or references a memory location in which data is


stored. Each memory cell in the computer has an address that can be used to access that
location so a pointer variable points to a memory location we can access and change the
contents of this memory location via the pointer.

OR
Explain the importance of pointer. Write a function that is passed an array of n pointers to
floats and returns a newly created array that contains those n float values.
Ans:

pointers have a number of useful applications.

Firstly, a number of things you do all the time use pointers, even if you are not aware. A struct
is a pointer. An array is a pointer. A string is a pointer. A class is a pointer. There is no way of
representing a number of associated objects without using pointers in some form or another.

Also, pointers are useful for efficiency. If you pass ten thousand ints to a function, you will have
a substantial time penalty, unless you pass a pointer to an array of ten thousand ints.

a l
9. Define a structure of student having data member’s name, address, marks in C language, and

e
marks in information system. Take data for n students in an array dynamically and find the p
total marks obtained.

T N
S I
C
Source: www.csitnepal.com
Do yourself
10. Some text file is given; create another text file replacing the following words “Ram” to
“Hari”, “Sita” to “Gita”, and “Govinda” to “Shiva”.
Ans:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

main()
{
FILE *ptr,*ptr1;
int i=0,j,n[10],k=0,l;
char line[150],word1[30],word[30];
printf("* This Is A Program To Replace The Words 'Ram' to 'Hari', 'Sita' to 'Gita', & 'Govinda'
to 'Shiva' . *\n\n");
printf("Enter Your String : ");
gets(line);
l=strlen(line);
for(i=0;i<l;i++)
{
if(line[i]==' ')
k++;
}

//printf("\nEnter a Word To Delete : ");


//gets(word1);
ptr=fopen("original.txt","w");
//fprintf(ptr,"%s","This Is The Original Strings With Out Replacement :\n\t");
for(i=0;i<l;i++)
{
fprintf(ptr,"%c",line[i]);
}
fclose(ptr);
printf("\n\nThe New String After Deletion : \n\n\t");
ptr=fopen("original.txt","r");
ptr1=fopen("replace.txt","w");
a l
e p
while(!feof(ptr))

T N
{

S I
C
Source: www.csitnepal.com
fscanf(ptr,"%s",&word);
if(strcmp(word,"Ram")==NULL)
{
printf("%s ","Hari");
fprintf(ptr1,"%s ","Hari");
}
else if(strcmp(word,"Sita")==NULL)
{
printf("%s ","Gita");
fprintf(ptr1,"%s ","Gita");
}

else if(strcmp(word,"Govinda")==NULL)
{
printf("%s ","Shiva");
fprintf(ptr1,"%s ","Shiva");
}
else
{
fprintf(ptr1,"%s ",word);
printf("%s ",word);
}
}
fclose(ptr);

getch();
}

OR

What are the uses of graphical function? Explain the basic graphical function with suitable
program.

Ans:
The use of graphical function in c programming is to draw the graphics in the c console.
Different shapes can be drawn using graphical function in c.

There are numerous graphics functions available in c. But let us see some to have an
understanding of how and where a pixel is placed in each when each of the graphics
function gets invoked.
a l
putpixel(x, y, color)
e p
T N
S I
C
Source: www.csitnepal.com
->The functionality of this function is it put a pixel or in other words a dot at position x, y
given in inputted argument

Line(x1, y1, x2, y2)

->The functionality of this function is to draw a line from (x1,y1) to (x2,y2).

getpixel(x, y)

->This function when invoked gets the color of the pixel specified. The color got will be the
integer value associated with that color and hence the function gets an integer value as
return value.

#include<graphics.h>

void main()

{int gd= DETECT, gm;

initgraph(&gd,&gm,"c:\\tc\\bgi");

circle(200,100,10);

setcolor(WHITE);

getch();

closegraph()

a l
e p
T N
S I
C
Source: www.csitnepal.com
Subject: Fundamental of Computer Programming 2068
1 Write an algorithm and flowchart to determine whether a given integer is odd or
even and explain it.

Ans:

Algorithm

Step 1: Start

Step 2: Read a

Step 3: Find modules of a by 2 (r = a % 2)

Step 4: If r= 0

Print a is even

Else

Print a is odd

Step 5: stop

Flowchat

start

Read a

R = a % 2;

If No
Print a is odd
a l
R=0
e p
T N
S I
C1
C2
C
Source: www.csitnepal.com
C1

yes

Print a is even

start C2

First of all, we have to read an integer a , then find the modules of a by 2 and if the value of
modules is 0 then a is even else a is odd.

2. How can you declare the variable in C ? Explain with example.

Ans:

Variable declaration in C:

Every variable used in the program should be declared to the compiler. The
decleration use two things

a. Tells the compiler the variable name


b. Specifies what type of data the variables will hold

The general format of any declaration

Data type v1 , v2 , v3 , …….. , vn ;

Where v1,v2,v3,…….,vn are variables name variables are separated by commas. The
decleration statement must end with a semi colon

Example :

Int sum;

Int number , salary;

a l
Double average , mean;

e p
2 Write a program to find the factorial of a given integer.

T N
Ans:

S I
C
Source: www.csitnepal.com
Program to find factorial

#include<stdio.h>

Int main()

Int i = 1 , f = 1, num ;

Printf (“ enter a number :”)

Scanf(“ %d”,&num)

While (i <= num)

F = f*i;

i++;

Printf (“ factorial of %d is %d”,num,f);

Return 0;

3 Explain switch statement with example.

Ans:

Switch statement

Switch statement allows a program to select one statement for


execution out of a set of alternative. During the execution of the switch statement only one of
the possible statement will be executed the remaining statement uses the simple and straight
forward approach avoiding multiple uses that was in if….else statement. General format of
switch statement is

Switch (expression)

a l
Case case1;

e p
Case case2;

T N
………………..

S I
C
Source: www.csitnepal.com
Case default

Example:

#include<stdio.h>

Void main()

Int num1,num2,result;

Char operator;

Printf(“enter two number”);

Scanf(“%d%d”,&num1,&num2);

Printf(“enter an operator”);

Scanf(“%c”,&operator);

Switch (operator)

Case ‘ + ’ :

Result = num1 + num2

Break;

Case ‘ - ‘ :

Result = num1 – num2

Break;

Case ‘ * ’ :

Result = num1 * num2

Break;

Default:
a l
Printf (“unknown operator”);
ep
Result = 0
T N
Break;
S I
C
Source: www.csitnepal.com
}

Printf (“%d”, result)

4 Write a program to find the largest and smallest among the given element in an
array.

Ans:

#include<sdtio.h>

#include<conio.h>

Void main()

Clrscr();

Int a[10];

Int i , big , small;

Printf ( “enter 10 elements in array:” );

For ( i=0 ;I <10 ; i++ )

Scanf(“ %d ” , a[i] );

Big = a[0];

For( i=0 ; i<10 ; i++ )

Printf( big < a[i] )

Big a[i];

Printf(“ largest element = %d ” , big );

a l
Small = a[0];
e p
For ( i=0 ; i<10 ; i++ )

T N
{
S I
C
Source: www.csitnepal.com
If ( small > a[i] )

Small = a[i];

Printf (“ \n smallest element = %d ” , small);

5 Explain the user-define function and its types with example.

Ans:

User-define function:

The functions which are created by user for program are known as user
defined functions. These type of functions are not defined in C- library and are created by the
user himself. The types of user-defined functions are:

a. Functions with no arguments and no return value.


b. Functions with no arguments and a return value.
c. Functions with arguments and return value.
d. Functions with arguments and no return value.

Example:

Int fact (int);

Void main()

Int n, fac;

Clrscr ();

Printf(“ enter the value of n ”);

Scanf(“ %d”, &n);

Fac = fact(n);

Printf(“ factorial of %d is %d “, n, fac);

Getch ();
a l
}
e p
Int fact (int n)
T N
{
S I
C
Source: www.csitnepal.com
If (n==1)

Return(1);

Else

Return fact (n-1) * n;

6 Write a program to accept two number and sort them with using pointer.

Ans:

#include

Void main ()

Int x, y, *a, *b, temp;

Printf (“ enter the value of x and y \n ”);

Scanf (“ %d %d” , &x, &y );

Printf (“ before swapping \n n = %d \n y = %d \n “ ,x, y);

a = &x;

b = &y;

temp = *b;

*b = *a;

*a = temp;

Printf (“ after swapping \n x = %d \n y = %d \n”, x, y);

7 Explain the passing structure to function with example.

Ans:

Passing structure to function:


a l
e p
In case of structures having numeras structure elements passing these

N
individual elements would be hard task. In such cases we may pass whole structure to a

T
function as shown below:

S I
#include<stdio.h>
C
Source: www.csitnepal.com
Structure employee

Int id;

Char name[25];

Float salary;

};

Void main ()

Static struct employee emp1 = { 12, “nitesh” ,7500};

Display(emp1);

Display (emp1);

Struct employee emp1

Printf (“ %d %s %f “, emp1, id, emp1.name, emp1.salary);

8 Write a program to accept any number and print the sum of that single digit
through recursive function.

Ans:

#include <stdio.h>

#include<conio.h>

int sum(int);

void main() {

a l
int n;
e p
printf("Enter the single digit number");scanf("%d",&n);

T N
printf("Sum = %d\n", sum(n));
S I
C
Source: www.csitnepal.com
getch();

int sum(int n)

if (n <= 1)

return 1;

else

return (n + sum(n-1));

9 Explain the pointer to structure with example.

Ans:

Pointer to structure:

We know the name of array stands for the address of its zero th element the
same concept applies for name of arrays of structures. Suppose item is an array variable of
struct type , consider the following

Struct products

Char name[30];

Int manufac ;

Float net;

Item[2], ptr;

This statement declares item as array of two element, each type struct product and ptr as a
pointer data object of type struct products.

The assignment ptr = item;


a l
Would assign the address of zeroth element to product [0],
e
its member can bep
accessed by using the following notation.

T N
Ptr = name

S I
C
Source: www.csitnepal.com
Ptr = manufac

Ptr = net

OR

Write a short notes on:

a. Dynamic memory allocation


b. Opening and closing file
a) Dymanic memory allocation:
C provides the function calloc( ) and malloc( ) in the standard library and
their function prototypes are in <stdlib.h> . The name calloc stands for contigeous
allocation and the name malloc to dynamically create space for arrays, structures and
unions. The function calloc takes to arguments where as the function malloc takes
only are argument

b) Opening and closing file:

Before a program can write to a file or read from file. The program must
open it , opening a file established a link between the program and the operating system, this
provides the operating system, the name of the file and the mode in which the file is to be
opened. The process of establishing a connection between the program and file is called
opening the file.

Same is the case of closing file.

a l
e p
T N
S I
C
Source: www.csitnepal.com
2069

1 Write an algorithm and flowchart to find out whether a given integer is zero, positive,
negative and explain it.
Ans:

Start

Input a;

If If
Print A is Positive
A<0 B=0
No No p

yes yes

Print A is Negative Print A is Zero


Yes Yes
p p

End

Explain yourself
a l
e p
2 What are the basic four data types use in C programming? What are its size and range?
Explain.
T N
Ans:
S I
C
Source: www.csitnepal.com
The four types of datatypes used in C programming are:

Data type Size Range

1. Int 2 byte -32768 to 32767


2. Char 1 byte -128 to 127
3. Float 4 byte 3.4e - 38 to 3.4e + 38
4. Double 8 byte 1.7e – 308 to 1.7e + 308

Int : They are the basic integer valued data types. They take simple numbers containg no decimal
points in them.

Float : They are the longer range data types that are also capable of taking decimal point values.

Char : They are the character storing data types who stores a character within a name specified.

Double : Doubles are the data types who stores a longer range of number.

3 Explain the “if…else” statement with example.

Ans:

If …..else :-

If …..else statement are the loopong statement that checks the condition and
proceeds forward the logics. The if statements is run only if the condition in if is TRUE Otherwise
the else statement is run.

Example :

#include<stdio.h>

Void main()

Int a, b;

Printf(“ Enter two no’s ;“);

Scanf ( “ %d %d “ , &a , &b);

If (a<b)

a l
Printf ( “a is larger”);

e p
}

T N
else

S I
{
C
Source: www.csitnepal.com
Printf(“b is larger”);

4 Differentiate between break and exit statement with example.

Ans:

Break Exit
1. break is used to exit from the loop 1. exit is used to exit from the program
2. break() is not a function, it is part of the 2. exit() is a function
language syntax.
3.break is a keyword 3. exit is a non-returning function that returns
that exits the current construct like loops the control to the operating system
4. break is a control flow statement of the 4. exit(), instead, is a function that says that the
language. It says that next statement to be program must end and control must be given
executed is the one at the end of the loop (or at back to the operating system
the end of the switch statement)

5 Explain the multidimensional array with example. Write a program to convert a


lowercase character string to uppercase?

Ans:

Multidimensional array :

Often there is a need to store and manipulate two dimensional data structure such
as matrices and tables. Here the array has two subscripts one subscript denotes the row and the
other denotes the column. The declaration of two dimensional array as follows.

Data_type array_name[row size] [column size];

Int m [20] [10];

Example:

Int table [2] [3] ={0,0,0,1,1,1}

0 0 0

1 1 1

a l
Program to convert lowercase string to uppercase :

e p
#include<stdio.h>

T N
#include<string.h>

S I
#include<conio.h>
C
Source: www.csitnepal.com
Void main()

Char str [25];

Int len ;

Clrscr ();

Printf (“ enter the string \n: ””);

Gets(str);

Strupr(str)

Printf (“ \n your string in uppercase %s ”, str);

6 Explain the library functions with example.

Ans:

The C language is accompanied by a number of standard library functions for various useful tasks.
In particular all input and output operation and all math operations are implemented by library
functions. Header file is required to use library functions.

Types of library functions:

a. Built in library functions


b. User defined library functions

Example:
#include <stdio.h>
Void main()

{
Display();

}
Void display()

Printf(“my name is ram bahadur kanshakar”);

}
a l
e p
Output: my name is ram bahadur kanshakar

T N
S I
7 Write a program to find the sum of all the elements of an array using pointers.
C
Source: www.csitnepal.com
Ans

#include<stdio.h>
#include<conio.h>
Void main()

{
Int sum = 0;
Int * array ;
Int i;
Printf (“enter 10 value:”);
For (i=0;i<10;i++)

scanf(“%d”,*(a+i)

For(int j=0;j<10;j++)

Sum = sum + *(a+j);

Printf (“ sum = %d”,sum);

Getch();

8 Explain the pointer arithmetic with example.

Ans:

Pointer arithmetic :

C allows us to add integer or subtract integer from pointer as well as to subtract one
pointer from the other. We can also compare pointer by using the relational operators.

Example :

#include<stdio.h>

a l
Void main()

e p
{

T N
Int *ptr1, *ptr2;

S I
Int a , b , x , y , z;
C
Source: www.csitnepal.com
A=30;

B=60;

Ptr1 = &a;

Ptr2 = &b;

X=*ptr1 + *ptr2 – 6;

Printf(“value of x =%d”,x)

9 Explain the array of structures and write a program to accept record of 15 persons which
has name, age and address and also display them.

Ans:

Array of structure:

It is possible to define array of structure for example if we are maintain information


of all the students in the college and if 100 students are studying in the college we need to use an
array then single variables. We can define an array of structure as shown in the following example

Structure information

Int id;

Char name [20];

Char address [20];

Int age ;

Student[100];

Program :

#include<stdio.h>

#include<conio.h>

Structure student
a l
{
e p
Char name [20];

T N
Int age;
S I
C
Source: www.csitnepal.com
Char address [25];

S1 [15];

Void main()

Int I , j;

Printf (“enter name , age and address of 15 people “);

For (i=0;i<15;I++)

Scanf(“ %s %d %s” , s1[i].name , s1[i].age , s1[i].address”);

Printf (“ details you entered =”);

For (j=0;j<15;j++)

Printf(“ %s %d %s” , s1[j].name,s1[j].age,s1[j].address);

10 What are the three types of input/output functions which support in C-programming?
Explain with example.

Ans:

The three types of i/o functions which support in C- programming are

a) Basic i/o functions


b) Formatted i/o functions
c) Non – formatted i/o function
a l
EXPLAIN
ep
T N
(OR)
S I
C
Source: www.csitnepal.com
Write short note on:

a) Delimiters
b) Graphics function

Delimiters:

A delimiters is a sequence of one or more characters used to specify the boundry between
separate independent region in plain text or other data strings. An example of delimiters is
the comma character which act as field delimiter in a sequesnce of comma separated
values.

Graphic function:

Graphic functions in C is used to implemented various graphical symbols and shape in C .


There are various types of C – graphic function used , such as

 Put pixel (x , y , color)

It helps to put a dot (pixel) at the position x,y.

 Line (x1 , y1 , x2 , y2)

Its function is to draw line from (x1 , y1) to (x2 , y2)

 Circle (x , y , r) etc.

a l
e p
T N
S I
C
Source: www.csitnepal.com

You might also like