Write a C program to print all Unique numbers in an array

 TASK-

Write a C program to print all Unique numbers in an array (the size of array (number of elements in array) should be taken from user)!!

STEPS :

  • Ask user to enter the size of array 
  • store this size in a variable
  • use this variable to declare an array of that size
  • Finally ask for the entries of array 
  • Follow the algorithm explained below to find unique elements in that    array 

CODE:

#include <stdio.h>
int main()
{
	int r,z;
	printf("Enter the number of entries in array: ");//asked for size (1)
	scanf("%d",&r); //stored in a variable (2)
	int array[r]; // declared an array of this size (3)
	printf("Enter entries: \n");
	for (z=0;z<r;z++) // asked values array
	{
		printf("Entry %d: ",z+1);
		scanf("%d",&array[z]);
		printf("\n");
	}
	//Here the logic begins lets start!!!
   int i,j;
   int count = 1; 
   for(i = 0; i < r; i++){
      for(j = 0; j < r; j++){
      	//when two numbers are equal and i , j are not equal which means your are not comparing same index number
         if(array[i] == array[j] && i != j)  
         break;
      }
      //If above condition is not followed j would run till r else j would run less then r!!!
      if(j == r ){ 
         printf("\nunique elements in an array is [%d] : %d \n",count,array[i]);
         ++count;
      }
   }
   getchar();
   return 0;
}

EXPECTED OUTPUT:



NEED WORK PRODUCE WORK

My name is Abdul Rehman I am from Pakistan. I am doing BS in Computer and information sciences. Currently, I am creating these blogs to help students of computer sciences all over the world..

1 Comments

Post a Comment
Previous Post Next Post