Slip16
Q1. Write a C program to calculate xy without using standard library function.
[15 Marks]
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,i,r=1,t;
printf("Enter a number:");
scanf("%d",&x);
printf("Enter the power:");
scanf("%d",&y);
for(i=1;i<=y;i++)
{
t=x;
r=r*t;
}
printf("Result:%d",r);
getch();
}
#include<conio.h>
void main()
{
int x,y,i,r=1,t;
printf("Enter a number:");
scanf("%d",&x);
printf("Enter the power:");
scanf("%d",&y);
for(i=1;i<=y;i++)
{
t=x;
r=r*t;
}
printf("Result:%d",r);
getch();
}
Q2. Write a program to display union and intersection of two 1D array. [25 Marks]
#include<stdio.h>
int printIntersection(int arr1[], int arr2[], int m, int n)
{
int i = 0, j = 0;
while (i < m && j < n)
{
if (arr1[i] < arr2[j])
i++;
else if (arr2[j] < arr1[i])
j++;
else /* if arr1[i] == arr2[j] */
{
printf(" %d ", arr2[j++]);
i++;
}
}
}
int main()
{
int arr1[] = {1, 2, 4, 5, 6};
int arr2[] = {2, 3, 5, 7};
int m = sizeof(arr1)/sizeof(arr1[0]);
int n = sizeof(arr2)/sizeof(arr2[0]);
printIntersection(arr1, arr2, m, n);
getchar();
return 0;
}
No comments:
Post a Comment