Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find the Union and Intersection of the two sorted arrays #68

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions Armstrong number.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <stdio.h>
#include<math.h>

int check_armstrong(int);

int main()
{
int num_1,num_2;
printf("Enter first number : ");
scanf("%d",&num_1);
printf("Enter second number : ");
scanf("%d",&num_2);
printf("\nThe armstrong numbers in the interval %d to %d are as follows: \n",num_1,num_2);
for(int i=num_1;i<=num_2;i++)
{
if(check_armstrong(i)==1)
{ //checking whether the num is armstrong or
printf("%d\n",i); //if it is then printing its value.
}
}

}

int check_armstrong(int p)
{
int temp,flag;
temp=p;
flag=p;
int rem=0,total=0,count=0;
while(p) //this while loop is for counting the total number of digits in the number
{
rem=p%10;
p=p/10;
count++;
}
rem=0;
while(temp) //this while loop is for calculating the sum of digits raised to the total
{ //count of digits in the number as power
rem=temp%10;
total+=pow(rem,count);
temp=temp/10;
}
if(total==flag)
{
return 1;
}
else
{
return 0;
}
return 0;
}
88 changes: 88 additions & 0 deletions Operation on arrays.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
//Find the Union and Intersection of two sorted arrays.
#include <stdio.h>

void operations_on_array(int arr1[],int arr2[],int m,int n);

int main()
{
printf("Implementing union and intersection of two sorted arrays: \n");
int m,n;
printf("Enter the size of first array : ");
scanf("%d",&m);
printf("Enter the elememts of first array (in sorted manner): \n");
int arr1[m];
for(int i=0;i<m;i++)
{
scanf("%d",&arr1[i]);
}
printf("Enter the size of second array : ");
scanf("%d",&n);
printf("Enter the elememts of second array (in sorted manner): \n");
int arr2[n];
for(int i=0;i<n;i++)
{
scanf("%d",&arr2[i]);
}

printf("The arrays are as follows: \n");
printf("Array 1: \n");
for(int i=0;i<m;i++)
{
printf("%d ",arr1[i]);
}
printf("\n");
printf("Array 2: \n");
for(int i=0;i<n;i++)
{
printf("%d ",arr2[i]);
}

operations_on_array(arr1,arr2,m,n);

return 0;
}

void operations_on_array(int arr1[],int arr2[],int m,int n){
printf("\nIntersection : ");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(arr1[i]==arr2[j])
{
printf("%d ",arr1[i]);
}
}
}
printf("\nUnion : ");
int i=0,j=0;
while(i<m,j<n)
{
if(arr1[i]<arr2[j])
{
printf("%d ",arr1[i]);
i++;
}
else if(arr1[i]>arr2[j])
{
printf("%d ",arr2[j]);
j++;
}
else if(arr1[i]==arr2[j])
{
printf("%d ",arr1[i]);
i++;
j++;
}
}
while(i<m)
{
printf("%d ",arr1[i]);
i++;
}
while(j<n)
{
printf("%d ",arr2[j]);
j++;
}
}