[go: up one dir, main page]

0% found this document useful (0 votes)
6 views19 pages

Practical 9

T T

Uploaded by

kracompsci2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views19 pages

Practical 9

T T

Uploaded by

kracompsci2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

PRACTICAL 9

SET A

1. Write a program to accept n numbers in an array and display elements.

#include <stdio.h>

int main() {

int n;

// Prompt user for the number of elements

printf("Enter the number of elements: ");

scanf("%d", &n);

// Check for valid input

if (n <= 0) {

printf("Please enter a positive integer greater than 0.\n");

return 1; // Exit the program with an error code

int array[n]; // Declare an array of size n

// Accept n numbers from the user

printf("Enter %d numbers:\n", n);

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

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

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

// Display the elements of the array

printf("The elements in the array are:\n");

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

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


}

printf("\n");

return 0;

2. Write a program to accept n numbers calculate the range of elements in the

array.

#include <stdio.h>

int main() {

int n;

// Prompt user for the number of elements

printf("Enter the number of elements: ");

scanf("%d", &n);

// Check for valid input

if (n <= 0) {

printf("Please enter a positive integer greater than 0.\n");

return 1; // Exit the program with an error code

int array[n]; // Declare an array of size n

// Accept n numbers from the user

printf("Enter %d numbers:\n", n);

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

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

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

}
// Initialize min and max

int min = array[0];

int max = array[0];

// Calculate the min and max values

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

if (array[i] < min) {

min = array[i];

if (array[i] > max) {

max = array[i];

// Calculate the range

int range = max - min;

// Display the results

printf("The range of the elements in the array is: %d\n", range);

return 0;

3. Write a program to accept n numbers in an array and calculate the average.

#include <stdio.h>

int main() {

int n;

// Prompt user for the number of elements

printf("Enter the number of elements: ");

scanf("%d", &n);
// Check for valid input

if (n <= 0) {

printf("Please enter a positive integer greater than 0.\n");

return 1; // Exit the program with an error code

int array[n]; // Declare an array of size n

int sum = 0; // Variable to store the sum of the elements

// Accept n numbers from the user

printf("Enter %d numbers:\n", n);

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

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

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

sum += array[i]; // Add each element to the sum

// Calculate the average

double average = (double)sum / n;

// Display the results

printf("The average of the elements in the array is: %.2lf\n", average);

return 0;

4. Write a program to accept n numbers in the range of 1 to 25 and count the

frequency of occurrence of each number.

#include <stdio.h>

int main() {
int n;

int frequency[26] = {0}; // Array to store the frequency of numbers 1 to 25

// Prompt user for the number of elements

printf("Enter the number of elements: ");

scanf("%d", &n);

// Check for valid input

if (n <= 0) {

printf("Please enter a positive integer greater than 0.\n");

return 1; // Exit the program with an error code

// Accept n numbers in the range of 1 to 25

printf("Enter %d numbers (each between 1 and 25):\n", n);

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

int number;

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

scanf("%d", &number);

// Check if the number is in the valid range

if (number < 1 || number > 25) {

printf("Please enter a number between 1 and 25.\n");

i--; // Decrement i to repeat this iteration

continue; // Skip the rest of the loop

// Increment the frequency count for the number

frequency[number]++;

}
// Display the frequency of each number

printf("Frequency of each number between 1 and 25:\n");

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

if (frequency[i] > 0) {

printf("Number %d: %d times\n", i, frequency[i]);

return 0;

5.SAME

6. Write a function to accept n numbers and display the array in the reverse

order. Write separate functions to accept and display.

#include <stdio.h>

#define MAX_SIZE 100 // Maximum size of the array

// Function prototypes

void acceptNumbers(int arr[], int n);

void displayReverse(int arr[], int n);

int main() {

int n;

int array[MAX_SIZE]; // Declare an array with a maximum size

// Prompt user for the number of elements

printf("Enter the number of elements (max %d): ", MAX_SIZE);

scanf("%d", &n);

// Check for valid input

if (n <= 0 || n > MAX_SIZE) {


printf("Please enter a positive integer between 1 and %d.\n", MAX_SIZE);

return 1; // Exit the program with an error code

// Accept numbers into the array

acceptNumbers(array, n);

// Display the array in reverse order

printf("The array in reverse order is:\n");

displayReverse(array, n);

return 0;

// Function to accept n numbers

void acceptNumbers(int arr[], int n) {

printf("Enter %d numbers:\n", n);

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

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

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

// Function to display the array in reverse order

void displayReverse(int arr[], int n) {

for (int i = n - 1; i >= 0; i--) {

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

printf("\n"); // New line after displaying the array

}
7. Write a function to accept n numbers and store all prime numbers in

an array called prime.Display this array.

#include <stdio.h>

#include <stdbool.h>

#define MAX_SIZE 100 // Maximum size of the array

// Function prototypes

bool isPrime(int num);

void acceptNumbersAndStorePrimes(int arr[], int n, int prime[], int *primeCount);

void displayPrimes(int prime[], int primeCount);

int main() {

int n;

int array[MAX_SIZE]; // Array to store input numbers

int prime[MAX_SIZE]; // Array to store prime numbers

int primeCount = 0; // Count of prime numbers

// Prompt user for the number of elements

printf("Enter the number of elements (max %d): ", MAX_SIZE);

scanf("%d", &n);

// Check for valid input

if (n <= 0 || n > MAX_SIZE) {

printf("Please enter a positive integer between 1 and %d.\n", MAX_SIZE);

return 1; // Exit the program with an error code

// Accept numbers and store primes

acceptNumbersAndStorePrimes(array, n, prime, &primeCount);


// Display the prime numbers

displayPrimes(prime, primeCount);

return 0;

// Function to check if a number is prime

bool isPrime(int num) {

if (num <= 1) return false;

for (int i = 2; i * i <= num; i++) {

if (num % i == 0) return false;

return true;

// Function to accept numbers and store prime numbers in the prime array

void acceptNumbersAndStorePrimes(int arr[], int n, int prime[], int *primeCount) {

printf("Enter %d numbers:\n", n);

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

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

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

// Check if the number is prime and store it if it is

if (isPrime(arr[i])) {

prime[*primeCount] = arr[i];

(*primeCount)++; // Increment the count of prime numbers

// Function to display the prime numbers

void displayPrimes(int prime[], int primeCount) {


if (primeCount == 0) {

printf("No prime numbers were entered.\n");

} else {

printf("Prime numbers are:\n");

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

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

printf("\n"); // New line after displaying the array

SET B

1. Write a program to accept n numbers and count Even and Odd Elements in an

Array

#include <stdio.h>

#define MAX_SIZE 100 // Maximum size of the array

int main() {

int n;

int array[MAX_SIZE]; // Array to store the input numbers

int evenCount = 0; // Counter for even numbers

int oddCount = 0; // Counter for odd numbers

// Prompt user for the number of elements

printf("Enter the number of elements (max %d): ", MAX_SIZE);

scanf("%d", &n);

// Check for valid input

if (n <= 0 || n > MAX_SIZE) {

printf("Please enter a positive integer between 1 and %d.\n", MAX_SIZE);

return 1; // Exit the program with an error code


}

// Accept numbers into the array

printf("Enter %d numbers:\n", n);

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

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

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

// Count even and odd elements

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

if (array[i] % 2 == 0) {

evenCount++;

} else {

oddCount++;

// Display the results

printf("Total Even Numbers: %d\n", evenCount);

printf("Total Odd Numbers: %d\n", oddCount);

return 0;

2. Write a program to accept n numbers and print Array Elements Present in

Even Position

#include <stdio.h>

#define MAX_SIZE 100 // Maximum size of the array

int main() {
int n;

int array[MAX_SIZE]; // Array to store the input numbers

// Prompt user for the number of elements

printf("Enter the number of elements (max %d): ", MAX_SIZE);

scanf("%d", &n);

// Check for valid input

if (n <= 0 || n > MAX_SIZE) {

printf("Please enter a positive integer between 1 and %d.\n", MAX_SIZE);

return 1; // Exit the program with an error code

// Accept numbers into the array

printf("Enter %d numbers:\n", n);

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

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

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

// Print elements present in even positions (1-based indexing)

printf("Elements present in even positions:\n");

for (int i = 1; i < n; i += 2) { // Start from index 1 for even positions

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

printf("\n"); // New line after printing

return 0;

}
3. Write a program to accept n numbers and print Array Elements Present in Odd

Position

#include <stdio.h>

#define MAX_SIZE 100 // Maximum size of the array

int main() {

int n;

int array[MAX_SIZE]; // Array to store the input numbers

// Prompt user for the number of elements

printf("Enter the number of elements (max %d): ", MAX_SIZE);

scanf("%d", &n);

// Check for valid input

if (n <= 0 || n > MAX_SIZE) {

printf("Please enter a positive integer between 1 and %d.\n", MAX_SIZE);

return 1; // Exit the program with an error code

// Accept numbers into the array

printf("Enter %d numbers:\n", n);

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

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

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

// Print elements present in odd positions (1-based indexing)

printf("Elements present in odd positions:\n");

for (int i = 0; i < n; i += 2) { // Start from index 0 for odd positions

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


}

printf("\n"); // New line after printing

return 0;

4. Write a program to accept n numbers and write a function to print the Sum of

Even and the Product of Odd Digits

#include <stdio.h>

void calculateSumAndProduct(int num, int *evenSum, int *oddProduct);

int main() {

int n;

// Prompt user for the number of elements

printf("Enter the number of elements: ");

scanf("%d", &n);

// Check for valid input

if (n <= 0) {

printf("Please enter a positive integer.\n");

return 1; // Exit the program with an error code

// Variables to store the final results

int totalEvenSum = 0;

int totalOddProduct = 1; // Start with 1 for multiplication

// Accept numbers and calculate sum of even and product of odd digits

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

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

scanf("%d", &number);

int evenSum = 0;

int oddProduct = 1; // Start with 1 for multiplication

// Calculate sum of even and product of odd digits

calculateSumAndProduct(number, &evenSum, &oddProduct);

// Update total results

totalEvenSum += evenSum;

totalOddProduct *= oddProduct; // Multiply with the current product

// Display the results

printf("Total Sum of Even Digits: %d\n", totalEvenSum);

printf("Total Product of Odd Digits: %d\n", totalOddProduct);

return 0;

// Function to calculate sum of even digits and product of odd digits

void calculateSumAndProduct(int num, int *evenSum, int *oddProduct) {

while (num > 0) {

int digit = num % 10; // Get the last digit

if (digit % 2 == 0) { // Even digit

*evenSum += digit;

} else { // Odd digit

*oddProduct *= digit;

num /= 10; // Remove the last digit


}

5. Write a function to accept n numbers and Delete an Element from an Array

and Print a New Array.

#include <stdio.h>

#define MAX_SIZE 100 // Maximum size of the array

void deleteElement(int arr[], int *n, int element);

int main() {

int n;

int array[MAX_SIZE]; // Array to store the input numbers

// Prompt user for the number of elements

printf("Enter the number of elements (max %d): ", MAX_SIZE);

scanf("%d", &n);

// Check for valid input

if (n <= 0 || n > MAX_SIZE) {

printf("Please enter a positive integer between 1 and %d.\n", MAX_SIZE);

return 1; // Exit the program with an error code

// Accept numbers into the array

printf("Enter %d numbers:\n", n);

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

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

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

}
// Get the element to delete

int element;

printf("Enter the element to delete: ");

scanf("%d", &element);

// Delete the element from the array

deleteElement(array, &n, element);

// Print the new array

printf("The new array after deletion is:\n");

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

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

printf("\n"); // New line after printing

return 0;

// Function to delete an element from the array

void deleteElement(int arr[], int *n, int element) {

int found = 0; // To check if the element was found

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

if (arr[i] == element) {

found = 1; // Element found

// Shift elements to the left

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

arr[j] = arr[j + 1];

(*n)--; // Decrease the size of the array

i--; // Adjust index to check the new element at the current position
}

if (!found) {

printf("Element %d not found in the array.\n", element);

6. Write function to accept n numbers and print Array Elements Present in Even

Position and display the sum of elements.

#include <stdio.h>

#define MAX_SIZE 100 // Maximum size of the array

void printEvenPositionElementsAndSum(int arr[], int n);

int main() {

int n;

int array[MAX_SIZE]; // Array to store the input numbers

// Prompt user for the number of elements

printf("Enter the number of elements (max %d): ", MAX_SIZE);

scanf("%d", &n);

// Check for valid input

if (n <= 0 || n > MAX_SIZE) {

printf("Please enter a positive integer between 1 and %d.\n", MAX_SIZE);

return 1; // Exit the program with an error code

// Accept numbers into the array

printf("Enter %d numbers:\n", n);


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

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

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

// Print elements present in even positions and their sum

printEvenPositionElementsAndSum(array, n);

return 0;

// Function to print elements in even positions and their sum

void printEvenPositionElementsAndSum(int arr[], int n) {

int sum = 0;

printf("Elements present in even positions (1-based indexing):\n");

for (int i = 1; i < n; i += 2) { // Start from index 1 for even positions

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

sum += arr[i];

printf("\nSum of elements in even positions: %d\n", sum);

You might also like