1. Write a program in C to count the total number of duplicate elements in an array.
Test Data :
Input the number of elements to be stored in the array :3
Input 3 elements in the array :
element - 0 : 5
element - 1 : 1
element - 2 : 1
Expected Output :
Total number of duplicate elements found in the array is : 1
CHATGPT/WEB RELATED MINE
#include <stdio.h> #include <stdio.h>
#define MAX_SIZE 100 // Maximum array #include <stdlib.h>
size
#define max_size 3
int main()
{
int arr[MAX_SIZE]; int main ()
int i, j, size, count = 0;
{
int array[max_size];
/* Input size of array */
printf("Enter size of the array : ");
scanf("%d", &size); int d, s, size, count = 0;
/* Input elements in array */
printf("Hi! Please enter the size of your array: ");
printf("Enter elements in array : ");
for(i=0; i<size; i++)
{ scanf("%d", &size);
scanf("%d", &arr[i]);
}
/* printf("\nEnter the three elements in your array: ");
* Find all duplicate elements in
array for (d = 0; d < size; d++)
*/
for(i=0; i<size; i++) {
{ scanf("%d", &array[d]);
for(j=i+1; j<size; j++)
{ }
/* If duplicate found then
increment count by 1 */
if(arr[i] == arr[j]) for (d = 0; d < size; d++)
{
count++; {
break;
} for (s = d + 1; s < size; s++)
} {
}
printf("\nTotal number of duplicate if (array[d] == array[s])
elements found in array = %d", count);
{
return 0; count++;
}
break;
}
}
}
printf("\nThe total number of duplicate elements
found in the array is: %d", count);
return 0;
}
2. Write a program in C to print all unique elements in an array.
Test Data :
Print all unique elements of an array:
------------------------------------------
Input the number of elements to be stored in the array: 4
Input 4 elements in the array :
element - 0 : 3
element - 1 : 2
element - 2 : 2
element - 3 : 5
Expected Output :
The unique elements found in the array are:
35
CHATGPT/WEB RELATED MINE
#include <stdio.h>
int main()
{
int arr1[100], n,ctr=0;
int i, j, k;
printf("\n\nPrint all unique
elements of an array:\n");
printf("------------------------
------------------\n");
printf("Input the number of
elements to be stored in the array: ");
scanf("%d",&n);
printf("Input %d elements in the
array :\n",n);
for(i=0;i<n;i++)
{
printf("element - %d :
",i);
scanf("%d",&arr1[i]);
}
printf("\nThe unique elements found
in the array are: \n");
for(i=0; i<n; i++)
{
ctr=0;
for(j=0,k=n; j<k+1; j++)
{
/*Increment the counter
when the seaarch value is duplicate.*/
if (i!=j)
{
if(arr1[i]==arr1[j])
{ctr++; }
}
}
if(ctr==0)
{
printf("%d ",arr1[i]);
}
}
printf("\n\n");
}
3. Write a program in C to merge two arrays of the same size sorted in descending
order.
Test Data :
Input the number of elements to be stored in the first array :3
Input 3 elements in the array :
element - 0 : 1
element - 1 : 2
element - 2 : 3
Input the number of elements to be stored in the second array :3
Input 3 elements in the array :
element - 0 : 1
element - 1 : 2
element - 2 : 3
Expected Output :
The merged array in decending order is :
332211
CHATGPT/WEB RELATED MINE
#include <stdio.h>
void main()
{
int arr1[100], arr2[100], arr3[200];
int s1, s2, s3;
int i, j, k;
printf("\n\nMerge two arrays of same
size sorted in decending order.\n");
printf("----------------------------
--------------------------------\n");
printf("Input the number of elements
to be stored in the first array :");
scanf("%d",&s1);
printf("Input %d elements in the
array :\n",s1);
for(i=0;i<s1;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
}
printf("Input the number of elements
to be stored in the second array :");
scanf("%d",&s2);
printf("Input %d elements in the
array :\n",s2);
for(i=0;i<s2;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr2[i]);
}
/* size of merged array is size of
first array and size of second array */
s3 = s1 + s2;
/*----------------- insert in the third
array------------------------------------*/
for(i=0;i<s1; i++)
{
arr3[i] = arr1[i];
}
for(j=0;j<s2; j++)
{
arr3[i] = arr2[j];
i++;
}
/*----------------- sort the array in
decending order ---------------------------
*/
for(i=0;i<s3; i++)
{
for(k=0;k<s3-1;k++)
{
if(arr3[k]<=arr3[k+1])
{j=arr3[k+1];
arr3[k+1]=arr3[k];
arr3[k]=j;}
}
}
/*--------------- Prints the merged array -
-----------------------------------*/
printf("\nThe merged array in
decending order is :\n");
for(i=0; i<s3; i++)
{
printf("%d ", arr3[i]);
}
printf("\n\n");
}
4. Write a program in C to count the frequency of each element of an array.
Test Data :
Input the number of elements to be stored in the array :3
Input 3 elements in the array :
element - 0 : 25
element - 1 : 12
element - 2 : 43
Expected Output :
The frequency of all elements of an array :
25 occurs 1 times
12 occurs 1 times
43 occurs 1 times
CHATGPT/WEB RELATED MINE
#include <stdio.h>
void main()
{
int arr1[100], fr1[100];
int n, i, j, ctr;
printf("\n\nCount frequency of each
element of an array:\n");
printf("----------------------------------
--------------\n");
printf("Input the number of elements to be
stored in the array :");
scanf("%d",&n);
printf("Input %d elements in the array
:\n",n);
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
fr1[i] = -1;
}
for(i=0; i<n; i++)
{
ctr = 1;
for(j=i+1; j<n; j++)
{
if(arr1[i]==arr1[j])
{
ctr++;
fr1[j] = 0;
}
}
if(fr1[i]!=0)
{
fr1[i] = ctr;
}
}
printf("\nThe frequency of all elements of
array : \n");
for(i=0; i<n; i++)
{
if(fr1[i]!=0)
{
printf("%d occurs %d times\n",
arr1[i], fr1[i]);
}
}
}
5. Write a program in C to find the maximum and minimum elements in an array.
Test Data :
Input the number of elements to be stored in the array :3
Input 3 elements in the array :
element - 0 : 45
element - 1 : 25
element - 2 : 21
Expected Output :
Maximum element is : 45
Minimum element is : 21
CHATGPT/WEB RELATED MINE
#include <stdio.h>
void main()
{
int arr1[100];
int i, mx, mn, n;
printf("\n\nFind maximum and minimum
element in an array :\n");
printf("-----------------------------
---------------------\n");
printf("Input the number of elements
to be stored in the array :");
scanf("%d",&n);
printf("Input %d elements in the
array :\n",n);
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
}
mx = arr1[0];
mn = arr1[0];
for(i=1; i<n; i++)
{
if(arr1[i]>mx)
{
mx = arr1[i];
}
if(arr1[i]<mn)
{
mn = arr1[i];
}
}
printf("Maximum element is : %d\n", mx);
printf("Minimum element is : %d\n\n",
mn);
}
6. Write a program in C to separate odd and even integers into separate arrays.
Test Data :
Input the number of elements to be stored in the array :5
Input 5 elements in the array :
element - 0 : 25
element - 1 : 47
element - 2 : 42
element - 3 : 56
element - 4 : 32
Expected Output :
The Even elements are :
42 56 32
The Odd elements are :
25 47
CHATGPT/WEB RELATED MINE
#include <stdio.h>
void main()
{
int arr1[10], arr2[10], arr3[10];
int i,j=0,k=0,n;
printf("\n\nSeparate odd and
even integers in separate arrays:\n");
printf("------------------------
------------------------------\n");
printf("Input the number of
elements to be stored in the array :");
scanf("%d",&n);
printf("Input %d elements in the
array :\n",n);
for(i=0;i<n;i++)
{
printf("element - %d :
",i);
scanf("%d",&arr1[i]);
}
for(i=0;i<n;i++)
{ if (arr1[i]%2 == 0)
{arr2[j] = arr1[i];
j++;}
else
{arr3[k] = arr1[i];
k++;}}
printf("\nThe Even elements are :
\n");
for(i=0;i<j;i++)
{
printf("%d ",arr2[i]);
}
printf("\nThe Odd elements are
:\n");
for(i=0;i<k;i++)
{
printf("%d ", arr3[i]);
}
printf("\n\n");
}
7. Write a program in C to find the second largest element in an array.
Test Data :
Input the size of array : 5
Input 5 elements in the array :
element - 0 : 2
element - 1 : 9
element - 2 : 1
element - 3 : 4
element - 4 : 6
Expected Output :
The Second largest element in the array is : 6
CHATGPT/WEB RELATED MINE
#include <stdio.h>
void main(){
int arr1[50],n,i,j=0,lrg,lrg2nd;
printf("\n\nFind the second
largest element in an array :\n");
printf("------------------------
-------------------------\n");
printf("Input the size of array
: ");
scanf("%d", &n);
/* Stored values into the array*/
printf("Input %d elements in the
array :\n",n);
for(i=0;i<n;i++)
{
printf("element - %d :
",i);
scanf("%d",&arr1[i]);
}
/* find location of the largest element
in the array */
// lrg=arr1[0];
lrg=0;
for(i=0;i<n;i++)
{
if(lrg<arr1[i])
{
lrg=arr1[i];
j = i;
}
}
/* ignore the largest element and find
the 2nd largest element in the array *
lrg2nd=0;
for(i=0;i<n;i++)
{
if(i==j)
{
i++; /* ignoring the largest
element */
i--;
}
else
{
if(lrg2nd<arr1[i])
{
lrg2nd=arr1[i];
}
}
}
printf("The Second largest element in
the array is : %d \n\n", lrg2nd);
}
8. Write a program in C to convert a decimal number to a binary number using the
function.
Test Data :
Input any decimal number : 65
Expected Output :
The Binary value is : 1000001
CHATGPT/WEB RELATED MINE
#include <stdio.h>
int main(){
int decimalNumber = 65;
int binaryNumber[32];
int i = 0;
while (decimalNumber > 0) {
binaryNumber[i] = decimalNumber % 2;
decimalNumber = decimalNumber / 2;
i++;
}
printf("Binary representation: ");
for (int j = i - 1; j >= 0; j--) {
printf("%d", binaryNumber[j]);
}
printf("\n");
return 0;
}
9. Write a program in C to print all perfect numbers in a given range using the function.
Test Data :
Input lowest search limit of perfect numbers : 1
Input lowest search limit of perfect numbers : 100
Expected Output :
The perfect numbers between 1 to 100 are :
6 28
CHATGPT/WEB RELATED MINE
#include <stdio.h>
/* Function declarations */
int checkPerfect(int n1);
void PerfectNumbers(int stLimit, int
enLimit);
int main()
{
int stLimit, enLimit;
printf("\n\n Function : perfect numbers
in a given range :\n");
printf("--------------------------------
------------------\n");
printf(" Input lowest search limit of
perfect numbers : ");
scanf("%d", &stLimit);
printf(" Input highest search limit of
perfect numbers : ");
scanf("%d", &enLimit);
printf("\n The perfect numbers between %d
to %d are : \n", stLimit, enLimit);
PerfectNumbers(stLimit, enLimit);
printf("\n\n");
return 0;
}
// Checks whether the given number is perfect
or not.
int checkPerfect(int n1)
{
int i, sum;
sum = 0;
for(i=1; i<n1; i++)
{
if(n1 % i == 0)
{
sum += i;
}
}
// If sum of proper positive divisors equals
to given number
// then the number is perfect number
if(sum == n1)
return 1;
else
return 0;
}
void PerfectNumbers(int stLimit, int enLimit)
{
/* print perfect numbers from start to
end */
while(stLimit <= enLimit)
{
if(checkPerfect(stLimit))
{
printf(" %d ", stLimit);
}
stLimit++;
}
}
10. Write a program in C to make such a pattern like a right angle triangle with a number
which will repeat a number in a row.
The pattern like :
1
22
333
4444
CHATGPT/WEB RELATED MINE
#include <stdio.h>
int main(){
int row = 4, num = 1, x = 1
for(int i = 0; i < row; i++){
for(int j = 1; j <= num; j++){
printf("%d ", x);
}
x++;
num++;
printf("\n");
}
return 0;
}
11. Write a program in C to make such a pattern like a right angle triangle with the
number increased by 1.
The pattern like :
1
2 3
4 5 6
7 8 9 10
CHATGPT/WEB RELATED MINE
#include <stdio.h>
int main(){
int row = 4, num = 1, x = 1;
for(int i = 0; i < row; i++){
for(int j = 1; j <= num; j++){
printf("%d ", x);
x++;
}
num++;
printf("\n");
}
return 0;
}
12. Write a program in C to make a pyramid pattern with numbers increased
by 1.
1
2 3
4 5 6
7 8 9 10
CHATGPT/WEB RELATED MINE
#include <stdio.h>
int main(){
int row = 4, num = 1, x = 1;
for(int i = 1; i <= row; i++){
for(int k = 0; k < row - i; k++){
printf(" ");
}
for(int j = 1; j <= num; j++){
printf("%d ", x);
x++;
}
num++;
printf("\n");
}
return 0;
}
13. Write a program in C to print Floyd's Triangle.
1
01
101
0101
10101
CHATGPT/WEB RELATED MINE
#include <stdio.h>
int main(){
int x = 1,num = 1, row=5, col=1;
for(int i = 0; i <= row; i++){
for (int j = 1; j <= x; j++){
int k = col%2;
printf("%d ", k);
col++;
}
col = i;
x++;
num++;
printf("\n");
}
return 0;
}
14. Write a program in C to find the number and sum of all integers between 100 and
200 which are divisible by 9.
Expected Output :
Numbers between 100 and 200, divisible by 9 :
108 117 126 135 144 153 162 171 180 189 198
The sum : 1683
CHATGPT/WEB RELATED MINE
#include <stdio.h>
int main(){
for(int i = 100; i <= 200; i++){
if(i%9 == 0){
printf("%d ", i);
}
}
return 0;
}
15. Write a C program to convert a binary number into a decimal number without using
array, function and while loop.
Test Data :
Input a binary number :1010101
Expected Output :
The Binary Number : 1010101
The equivalent Decimal Number : 85
CHATGPT/WEB RELATED MINE
#include <stdio.h>
int main(){
int binary = 10110011;
int decimal = 0;
int base = 1;
int digit;
A:
if(binary > 0){
int lastDigit = binary % 10;
binary /= 10;
decimal += lastDigit * base;
base *= 2;
goto A;
}
printf("%d", decimal);
return 0;
}
16. Write a program in C to convert a decimal number into octal without using an array.
Test Data :
Enter a number to convert : 79
Expected Output :
The Octal of 79 is 117.
CHATGPT/WEB RELATED MINE
#include <stdio.h>
int main(){
int octal = 0, i = 1;
int decimal = 79;
A:
if(decimal > 0){
octal += (decimal%8) * i;
decimal /= 8;
i *= 10;
goto A;
}
printf("Octal number: %d\n", octal);
return 0;
}
17. Write a C program to convert an octal number to a decimal without using an array.
Test Data :
Input an octal number (using digit 0 - 7) :745
Expected Output :
The Octal Number : 745
The equivalent Decimal Number : 485
CHATGPT/WEB RELATED MINE
#include <stdio.h>
int main(){
int decimal = 0, i = 1;
int octal = 745;
int lastdigit;
A:
if(octal > 0){
lastdigit = octal%10;
decimal = decimal + lastdigit*i;
octal = octal/10;
i = i*8;
goto A;
}
printf("Decimal number: %d\n", decimal);
return 0;
}
18. Write a program in C to find the largest element using Dynamic Memory Allocation.
Test Data :
Input total number of elements(1 to 100): 5
Number 1: 5
Number 2: 7
Number 3: 2
Number 4: 9
Number 5: 8
Expected Output :
The Largest element is : 9.00
CHATGPT/WEB RELATED MINE
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,n;
float *element;
printf("\n\n Pointer : Find the largest
element using Dynamic Memory Allocation :\n");
printf("------------------------------------
-------------------------------------\n");
printf(" Input total number of elements(1 to
100): ");
scanf("%d",&n);
element=(float*)calloc(n,sizeof(float)); //
Memory is allocated for 'n' elements
if(element==NULL)
{
printf(" No memory is allocated.");
exit(0);
}
printf("\n");
for(i=0;i<n;++i)
{
printf(" Number %d: ",i+1);
scanf("%f",element+i);
}
for(i=1;i<n;++i)
{
if(*element<*(element+i))
*element=*(element+i);
}
printf(" The Largest element is : %.2f
\n\n",*element);
return 0;
}
19. Write a program in C to sort an array using a pointer.
Test Data :
Input the number of elements to store in the array : 5
Input 5 number of elements in the array :
element - 1 : 25
element - 2 : 45
element - 3 : 89
element - 4 : 15
element - 5 : 82
Expected Output :
The elements in the array after sorting :
element - 1 : 15
element - 2 : 25
element - 3 : 45
element - 4 : 82
element - 5 : 89
CHATGPT/WEB RELATED MINE
#include <stdio.h>
void main()
{
int *a,i,j,tmp,n;
printf("\n\n Pointer : Sort an
array using pointer :\n");
printf("-------------------------
-------------------\n");
printf(" Input the number of
elements to store in the array : ");
scanf("%d",&n);
printf(" Input %d number of elements
in the array : \n",n);
for(i=0;i<n;i++)
{
printf(" element - %d : ",i+1);
scanf("%d",a+i);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if( *(a+i) > *(a+j))
{
tmp = *(a+i);
*(a+i) = *(a+j);
*(a+j) = tmp;
}
}
}
printf("\n The elements in the array
after sorting : \n");
for(i=0;i<n;i++)
{
printf(" element - %d : %d
\n",i+1,*(a+i));
}
printf("\n");
}
20. Write a program in C to compute the sum of all elements in an array using pointers.
Test Data :
Input the number of elements to store in the array (max 10) : 5
Input 5 number of elements in the array :
element - 1 : 2
element - 2 : 3
element - 3 : 4
element - 4 : 5
element - 5 : 6
Expected Output :
The sum of array is : 20
CHATGPT/WEB RELATED MINE
#include <stdio.h>
void main()
{
int arr1[10];
int i,n, sum = 0;
int *pt;
printf("\n\n Pointer : Sum of all
elements in an array :\n");
printf("--------------------------------
----------------\n");
printf(" Input the number of elements to
store in the array (max 10) : ");
scanf("%d",&n);
printf(" Input %d number of elements in
the array : \n",n);
for(i=0;i<n;i++)
{
printf(" element - %d : ",i+1);
scanf("%d",&arr1[i]);
}
pt = arr1; // pt store the base address of
array arr1
for (i = 0; i < n; i++) {
sum = sum + *pt;
pt++;
}
printf(" The sum of array is : %d\n\n",
sum);
}
21. Write a program in C to count the number of vowels and consonants in a string using
a pointer.
Test Data :
Input a string: string
Expected Output :
Number of vowels : 1
Number of constant : 5
CHATGPT/WEB RELATED MINE
#include <stdio.h>
int main()
{
char str[100];
char *p;
int vCount=0,cCount=0;
printf("Enter any string: ");
fgets(str, 100, stdin);
//assign base address of char array to
pointer
p=str;
//'\0' signifies end of the string
while(*p!='\0')
{
if(*p=='A' ||*p=='E' ||*p=='I'
||*p=='O' ||*p=='U'
||*p=='a' ||*p=='e'
||*p=='i' ||*p=='o' ||*p=='u')
vCount++;
else
cCount++;
//increase the pointer, to point
next character
p++;
}
printf("Number of Vowels in String:
%d\n",vCount);
printf("Number of Consonants in String:
%d",cCount);
return 0;
}
23. Write a program in C to print the elements of an array in reverse order using
pointer.
Test Data :
Input the number of elements to store in the array (max 15) : 5
Input 5 number of elements in the array :
element - 1 : 2
element - 2 : 3
element - 3 : 4
element - 4 : 5
element - 5 : 6
Expected Output :
The elements of array in reverse order are :
element - 5 : 6
element - 4 : 5
element - 3 : 4
element - 2 : 3
element - 1 : 2
CHATGPT/WEB RELATED MINE
#include <stdio.h>
void main()
{
int n, i, arr1[15];
int *pt;
printf("\n\n Pointer : Print the elements
of an array in reverse order :\n");
printf("---------------------------------
-------------------------------\n");
printf(" Input the number of elements to
store in the array (max 15) : ");
scanf("%d",&n);
pt = &arr1[0]; // pt stores the address of
base array arr1
printf(" Input %d number of elements in the
array : \n",n);
for(i=0;i<n;i++)
{
printf(" element - %d : ",i+1);
scanf("%d",pt);//accept the address of
the value
pt++;
}
pt = &arr1[n - 1];
printf("\n The elements of array in reverse
order are :");
for (i = n; i > 0; i--)
{
printf("\n element - %d : %d ", i,
*pt);
pt--;
}
printf("\n\n");
}
24. Write a program in C to print a string in reverse using a pointer.
Test Data :
Input a string : w3resource
Expected Output :
Pointer : Print a string in reverse order :
------------------------------------------------
Input a string :w3resource
Reverse of the string is : ecruoser3w
CHATGPT/WEB RELATED MINE
#include <stdio.h>
int main()
{
char str1[50];
char revstr[50];
char *stptr = str1;
char *rvptr = revstr;
int i=-1;
printf("\n\n Pointer : Print a
string in reverse order :\n");
printf("-------------------------
-----------------------\n");
printf(" Input a string : ");
scanf("%s",str1);
while(*stptr)
{
stptr++;
i++;
}
while(i>=0)
{
stptr--;
*rvptr = *stptr;
rvptr++;
--i;
}
*rvptr='\0';
printf(" Reverse of the string is :
%s\n\n",revstr);
return 0;
}
25.Write a C program to get the indices of two numbers in a given array of integers.
This will enable you to get the sum of two numbers equal to a specific target.
Expected Output:
Original Array: 4 2 1 5
Target Value: 7
Indices of the two numbers whose sum equal to target value: 7
1 3
CHATGPT/WEB RELATED MINE
#include <stdio.h>
#include <stdlib.h>
struct object {
int val;
int index;
};
static int compare(const void *a, const void *b)
{
return ((struct object *) a)->val - ((struct object
*) b)->val;
}
static int * two_sum(int *nums, int nums_size, int
target)
{
int i, j;
struct object *objes = malloc(nums_size *
sizeof(*objes));
for (i = 0; i < nums_size; i++) {
objes[i].val = nums[i];
objes[i].index = i;
}
qsort(objes, nums_size, sizeof(*objes), compare);
int *results = malloc(2 * sizeof(int));
i = 0;
j = nums_size - 1;
while (i < j) {
int diff = target - objes[i].val;
if (diff > objes[j].val) {
while (++i < j && objes[i].val == objes[i -
1].val) {}
} else if (diff < objes[j].val) {
while (--j > i && objes[j].val == objes[j +
1].val) {}
} else {
results[0] = objes[i].index;
results[1] = objes[j].index;
return results;
}
}
return NULL;
}
int main(void)
{
int nums[] = { 4, 2, 1, 5 };
int ctr = sizeof(nums) / sizeof(*nums);
int target = 7;
int i;
printf("Original Array: ");
for(i=0; i<ctr; i++)
{
printf("%d ", nums[i]);
}
printf("\nTarget Value: %d", target);
int *indexes = two_sum(nums, ctr, target);
if (indexes != NULL) {
printf("\nIndices of the two numbers whose
sum equal to target value: %d", target);
printf("\n%d %d\n", indexes[0], indexes[1]);
}
else
{
printf("Not found or matched\n");
}
return 0;
}