0% found this document useful (0 votes)
51 views

Write A Program To Find The Two Repeating Elements in An Array

Uploaded by

armanmund27
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Write A Program To Find The Two Repeating Elements in An Array

Uploaded by

armanmund27
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

1.

write a program to find the two repeating elements in an array

#include<stdio.h>

#include<stdlib.h>

void find2RepetElement(int arr1[], int arr_size) {

int i, j;

printf("The repeating elements are: ");

for(i = 0; i < arr_size; i++) {

for(j = i + 1; j < arr_size; j++) {

if(arr1[i] == arr1[j]) {

printf(" %d ", arr1[i]);

int main() {

int arr1[] = {2, 7, 4, 7, 8, 3, 4};

int ctr = sizeof(arr1) / sizeof(arr1[0]);

int i;

printf("The given array is : ");

for(i = 0; i < ctr; i++) {

printf("%d ", arr1[i]);

printf("\n");

find2RepetElement(arr1, ctr);

return 0;

}
2.WAP to read 10 numbers and find their sum and average

#include <stdio.h>

int main() {

int n = 10; // Number of elements

int numbers[n];

int sum = 0;

float average;

// Input

printf("Enter 10 numbers:\n");

for (int i = 0; i < n; i++) {

printf("Enter number %d: ", i + 1);

scanf("%d", &numbers[i]);

sum += numbers[i];

// Calculate average

average = (float)sum / n;

// Output

printf("\nSum: %d\n", sum);

printf("Average: %.2f\n", average);

return 0;

}
3. input a string and check it is pallindrome or not#include <stdio.h>

#include <string.h>

int main()

char str[] = { "abbba" };

int l = 0;

int h = strlen(str) - 1;

while (h > l) {

if (str[l++] != str[h--]) {

printf("%s is not a palindrome\n", str);

return 0;

printf("%s is a palindrome\n", str);

return 0;

}
4. Find factorial of a number using pointers.

#include <stdio.h>

void findFact(int, int*);

int main() {

int fact;

int num1;

printf("\n\n Pointer : Find the factorial of a given number :\n");

printf("------------------------------------------------------\n");

printf(" Input a number : ");

scanf("%d", &num1);

findFact(num1, &fact);

printf(" The Factorial of %d is : %d \n\n", num1, fact);

return 0;

void findFact(int n, int *f) {

int i;

*f = 1;

for (i = 1; i <= n; i++) {

*f = *f * i;

}
5. Print a right angled triangle with the number increasing by 1.

#include <stdio.h>

int main() {

int i, j, rows, k = 1;

printf("Input number of rows : ");

scanf("%d", &rows);

for (i = 1; i <= rows; i++) {

for (j = 1; j <= i; j++)

printf("%d ", k++);

printf("\n");

return 0;

}
6. find power of a number using recursion

#include <stdio.h>

int power(int n1, int n2);

int main() {

int base, a, result;

printf("Enter base number: ");

scanf("%d", &base);

printf("Enter power number(positive integer): ");

scanf("%d", &a);

result = power(base, a);

printf("%d^%d = %d", base, a, result);

return 0;

int power(int base, int a) {

if (a != 0)

return (base * power(base, a - 1));

else

return 1;

}
7. write a program to reverse the elements in an array

#include <stdio.h>

#include <stdlib.h>

#define n 6

int main(){

int arr[n] = {9, 8, 7, 2, 4, 3};

int temp;

for(int i = 0; i<n/2; i++){

temp = arr[i];

arr[i] = arr[n-i-1];

arr[n-i-1] = temp;

for(int i = 0; i < n; i++){

printf("%d,", arr[i]);

}
8. wap to two add distances in inches and feet using structure

#include <stdio.h>

struct Distance {

int feet

float inch;

} d1, d2, result;

int main() {

// take first distance input

printf("Enter 1st distance\n");

printf("Enter feet: ");

scanf("%d", &d1.feet);

printf("Enter inch: ");

scanf("%f", &d1.inch);

// take second distance input

printf("\nEnter 2nd distance\n");

printf("Enter feet: ");

scanf("%d", &d2.feet);

printf("Enter inch: ");

scanf("%f", &d2.inch);

// adding distances

result.feet = d1.feet + d2.feet;

result.inch = d1.inch + d2.inch;

// convert inches to feet if greater than 12

while (result.inch >= 12.0) {

result.inch = result.inch - 12.0;

++result.feet;

printf("\nSum of distances = %d\'-%.1f\"", result.feet, result.inch);

return 0;

}
9. wap in c to reverse a number

#include <stdio.h>

int main() {

int n, reverse = 0, remainder;

printf("Enter an integer: ");

scanf("%d", &n);

while (n != 0) {

remainder = n % 10;

reverse = reverse * 10 + remainder;

n /= 10;

printf("Reversed number = %d", reverse);

return 0;

}
10. WAP to print the series 1!/1 + 2!/2 + .... + 5!/5 using function

#include <stdio.h>

int fact(int);

void main()

int sum;

sum=fact(1)/1+fact(2)/2+fact(3)/3+fact(4)/4+fact(5)/5;

printf("\n\n Function : find the sum of 1!/1+2!/2+3!/3+4!/4+5!/5 :\n");

printf("----------------------------------------------------------\n");

printf("The sum of the series is : %d\n\n",sum);

int fact(int n)

int num=0,f=1;

while(num<=n-1)

f =f+f*num;

num++;

return f;

}
11. WAP to print a pyramid that increases by 1

#include <stdio.h> // Include the standard input/output header file.

int main() {

int i, j, spc, rows, k, t = 1; // Declare variables 'i' and 'j' for loop counters, 'spc' for spaces, 'rows' for user input, 'k'
for loop counter, 't' for incrementing numbers.

printf("Input number of rows : "); // Print a message to prompt user input.

scanf("%d", &rows); // Read the value of 'rows' from the user.

spc = rows + 4 - 1; // Calculate the initial number of spaces.

for (i = 1; i <= rows; i++) { // Start a loop to generate rows.

for (k = spc; k >= 1; k--) { // Loop to print spaces before the numbers.

printf(" ");

for (j = 1; j <= i; j++) { // Loop to print numbers based on the current row.

printf("%d ", t++);

printf("\n"); // Move to the next line for the next row.

spc--; // Decrement the number of spaces for the next row.

return 0; // Indicate that the program has executed successfully.

}
12. )wap to check if a number is prime.

#include <stdio.h>

int main() {

int n, i, flag = 0;

printf("Enter a positive integer: ");

scanf("%d", &n);

// 0 and 1 are not prime numbers

// change flag to 1 for non-prime number

if (n == 0 || n == 1)

flag = 1;

for (i = 2; i <= n / 2; ++i) {

// if n is divisible by i, then n is not prime

// change flag to 1 for non-prime number

if (n % i == 0) {

flag = 1;

break;

// flag is 0 for prime numbers

if (flag == 0)

printf("%d is a prime number.", n);

else

printf("%d is not a prime number.", n);

return 0;

}
13. wap to check if a number is prime.#include <stdio.h>

#include <string.h>

#include <ctype.h>

char checkCapital(char *);

int main()

char str1[20], singLet;

printf("\n\n Recursion : Find the first capital letter in a string :\n");

printf("------------------------------------------------------------\n");

printf(" Input a string to including one or more capital letters : ");

scanf("%s", str1);

singLet = checkCapital(str1);

if (singLet == 0)

printf(" There is no capital letter in the string : %s.\n", str1);

else

printf(" The first capital letter appears in the string %s is %c.\n\n", str1, singLet); }

return 0;

char checkCapital(char *str2)

static int i = 0;

if (i < strlen(str2))

if (isupper(str2[i]))

return str2[i];
}

else

i = i + 1;

return checkCapital(str2);

else return 0;

14. WAP to find the first capital letter of a string using recursion

#include <stdio.h>

#include <string.h>

#include <ctype.h>

char checkCapital(char *);

int main()

char str1[20], singLet;

printf("\n\n Recursion : Find the first capital letter in a string :\n");

printf("------------------------------------------------------------\n");

printf(" Input a string to including one or more capital letters : ");

scanf("%s", str1);

singLet = checkCapital(str1);

if (singLet == 0)

printf(" There is no capital letter in the string : %s.\n", str1);

else

printf(" The first capital letter appears in the string %s is %c.\n\n", str1, singLet); }
return 0;

char checkCapital(char *str2)

static int i = 0;

if (i < strlen(str2))

if (isupper(str2[i]))

return str2[i];

else

i = i + 1;

return checkCapital(str2);

else return 0;

15. WAP to find sum of the series

1+11+111+1111+11111.....upto n terms

// C program to find the sum of

// the series 1+11+111+1111+....

#include <stdio.h>

// Function for finding summation

int summation(int n)

int sum = 0, j = 1;

for (int i = 1; i <= n; i++) {

sum = sum + j;
// Appending a 1 at the end

j = (j * 10) + 1;

return sum;

// Driver Code

int main()

int n = 5;

printf("%d", summation(n));

return 0;

}
16. WAP for multiplication table of an integer.

#include <stdio.h> // Include the standard input/output header file.

int main() {

int j, n; // Declare variables 'j' for loop counter and 'n' for user input.

printf("Input the number (Table to be calculated) : "); // Print a message to prompt user input.

scanf("%d", &n); // Read the value of 'n' from the user.

printf("\n"); // Print a newline for formatting.

for (j = 1; j <= 10; j++) { // Start a for loop to calculate the table up to 10.

printf("%d X %d = %d \n", n, j, n * j); // Print the multiplication expression and result.

return 0;

17. WAP for swapping of elements by call by reference.

#include <stdio.h>

// Function prototype to swap elements using call by reference

void swapNumbers(int *x, int *y, int *z);

int main() {

int e1, e2, e3;

printf("\n\n Pointer : Swap elements using call by reference :\n");

printf("------------------------------------------------------\n");

// Input three integer values from the user

printf(" Input the value of 1st element : ");

scanf("%d", &e1);

printf(" Input the value of 2nd element : ");

scanf("%d", &e2);

printf(" Input the value of 3rd element : ");


scanf("%d", &e3);

printf("\n The value before swapping are :\n");

printf(" element 1 = %d\n element 2 = %d\n element 3 = %d\n", e1, e2, e3);

// Call the function to swap the values of the three elements

swapNumbers(&e1, &e2, &e3);

printf("\n The value after swapping are :\n");

printf(" element 1 = %d\n element 2 = %d\n element 3 = %d\n\n", e1, e2, e3);

return 0;

// Function definition to swap the values of three integer pointers

void swapNumbers(int *x, int *y, int *z) {

int tmp;

tmp = *y;

*y = *x;

*x = *z;

*z = tmp;

You might also like