Test2 Pci

You might also like

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

Q1.

State the use of array with example


Ans:-
Use:-
i. Arrays are used to store multiple values in a single variable, instead of declaring
separate variables for each value.
ii. Elements of the array are stored at contiguous memory locations so we can easily
perform searching and sorting operation.
Declaration of array:- data_type arrayname[size];
Example:- int myNumbers[] = {25, 50, 75, 100};

Q2. State two string function with example


i. Strlen() function
 Role:- this function is used to return length of the string.
 Header file:- we need to include <string.h> header file in our program
 Syntax:- variable_name=strlen(string_name);
 Example:- a=strlen(name);
ii. Strcpy() function:-
 Role:- this function is used to copy the contents of source string to destination
string.
 Header file:- we need to include <string.h> header file in our program
 Syntax:- strcpy(target_string,source_string);
 Example:- strcpy(str2,str1);
 Above example is used to copy the contents of str1 to str2.

Q3. Give the advantages of pointer


 It save memory space
 Execution speed of pointer is faster
 It increase efficiency .
 Used to allocate memory dynamically

Q4. Write a program to explain recursion


“ A function to call itself is called as recursion”
 Recursive functions are very useful to solve many mathematical problems, such as
calculating the factorial of a number
Example:-

 #include<stdio.h>
 int fact(int n);
 void main()
 {
 int a,fa;
 printf("Enter a number: ");
 scanf("%d",&a);
 fa=fact(a);
 printf("Factorial is= %d",fa);
 getch;
 }

 int fact(int n)
 {
 if (n==1)
 return n;
 else
 return n*fact(n-1);;
 }

Q5. Explain structure with example

 “Structure is a collection of dissimilar data type”


Or
Structure is a collection of multiple element with different data type”
 With the help of Struct keyword we can create a structure.
syntax:
struct structure_name
{
Data_type variable1;
Data_type variable2;
Data_type variable n;
}structure_variable;
Example:
Struct student
{
Int rn;
Char name[20];
Float per;
}s;
6. explain call by value and call by reference

Sr.no parameter Call by value Call by ref


1 Def When we can pass a value When we can pass
or variable as a parameter address of a variable as
then it is called as call by a parameter then it is
value called as call by
reference method
2. Operation It perform operation on Actual variable by
duplicate variable using pointer
3 Value Not modify Yes it modify
modify?
4. Variable Simple variable Pointer variable
5 Example Void add(int a,int b); Void add(int *a,int *b);

Q7. Write a syntax of pow() with example and use of math.h header file

 The pow() function computes the power of a number.


 The pow() function takes two arguments (base value and power value) and, returns the power
raised to the base number.
 Syntax:- double pow(double x, double y); x raised to power y.
 Example:- pow(2,3); it gives 8

Math.h:-

 The math.h header defines various mathematical functions and one macro.
 All the functions available in this library take double as an argument and
return double as the result.
 Function:- pow(), sqrt() etc

Q8. State the syntax to declare pointer variable with example

Pointer :-

“pointer is a variable which hold the address of another variable”

Syntax: data_type *pointer_name;

Ex:- int *p;


Q9. Write a program to read two matrix and print their addition.

#include <stdio.h>

#include <conio.h>
void main()
{
int arr1[2][2],arr2[2][2],arr3[2][2],i,j;
clrscr();
printf("\nENTER VALUES FOR MATRIX 1:\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
scanf("%d",&arr1[i][j]);
}
}
printf("\nENTER VALUES FOR MATRIX B:\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
scanf("%d",&arr2[i][j]);
}
}
Printf(“\n addition of two matrix=\n”);
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
arr3[i][j]= arr1[i][j]+ arr2[i][j];
printf(“%d\t”,arr3[i][j]);
}
Printf(“\n”);
}
getch();

}
10. Compare between array and string
Q11. Write a program to swap two integer number using pointer

#include<stdio.h>
void swap(int *a, int *b);
void main()
{
int x, y;
printf(“enter two number”);
scanf(“%d%d”,&x,&y);
swap(&x,&y);
printf(“after swap a=%d”,x);
printf(“after swap b=%d”,y);
getch();
}
void swap(int *a,int *b)
{
int *c;
*c=*a;
*a=*b;
*b=*c;
}

Q12.write a program to find factorial of a number using recursion

 #include<stdio.h>
 int fact(int n);
 void main()
 {
 int a,fa;
 printf("Enter a number: ");
 scanf("%d",&a);
 fa=fact(a);
 printf("Factorial is= %d",fa);
 getch;
 }

 int fact(int n)
 {
 if (n==1)
 return n;
 else
 return n*fact(n-1);;
 }
Q13. Write a program to perform add, sub,multi, divide operation using function

#include<stdio.h>
#include<conio.h>
void arith(void);
void main()
{
clrscr();
arith();
getch();
}
void arith(void)
{
int a,b,c,d,e;
float f;
printf(“enter two number”);
scanf(“%d%d”,&a,&b);
c=a+b;
d=a-b;
e=a*b;
f=a/b;
printf(“\nadd=%d”,c);
printf(“\nsub=%d”,d);
printf(“\nmulti=%d”,e);
printf(“\ndivide=%f”,f);
}

You might also like