Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
31 views13 pages

Programs

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

Programs

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

) to rotate the elements of an array to if (n <= 0) {

cout << "Invalid array size." << endl


the right (clockwise) by one position.
return 1;
#include <iostream>
}
using namespace std;
int arr[n];
void rotateArrayClockwise(int arr[], int n) {
cout << "Enter the elements of the array: ";
if (n <= 1) return; // No need to rotate if the array has 0 or 1 element
for (int i = 0; i < n; i++) {
int lastElement = arr[n - 1];
cin >> arr[i];
// Shift elements to the right
}
for (int i = n - 1; i > 0; i--) {
cout << "Original array: ";
arr[i] = arr[i - 1];
printArray(arr, n);
}
rotateArrayClockwise(arr, n);
// Set the first element to the last element
cout << "Array after rotating clockwise by one position: ";
arr[0] = lastElement;
printArray(arr, n);
}
return 0;
void printArray(int arr[], int n) {
}
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
} program to calculate the sum of the cubes of the digits of a given
integer
cout << endl;
#include <iostream>
}
#include <cmath> // For pow function
int main() {
using namespace std;
int n;
int sumOfCubes(int num) {
cout << "Enter the number of elements in the array: ";
int sum = 0;
cin >> n;
num = abs(num); // Handle negative numbers by taking absolute value printf("Enter the coordinates of the first point (x1 y1): ");
while (num > 0) { scanf("%f %f", &x1, &y1);
int digit = num % 10; // Get the last digit // Input coordinates of the second point
sum += pow(digit, 3); // Add the cube of the digit to the sum printf("Enter the coordinates of the second point (x2 y2): ");
num /= 10; // Remove the last digit scanf("%f %f", &x2, &y2);
} // Calculate the Euclidean distance
return sum; distance = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
} // Output the result
int main() { printf("The Euclidean distance between the points is: %.2f\n", distance);
int num; return 0;
cout << "Enter an integer: "; }
cin >> num; program to search for a given integer in an array and determine its
frequency:
int result = sumOfCubes(num); #include <stdio.h>
int main() {
int n, searchValue, count = 0;
cout << "The sum of the cubes of the digits of " << num << " is: " << result << // Input array size
endl; printf("Enter the number of elements in the array: ");
scanf("%d", &n);
return 0;
if (n <= 0) {
} printf("Invalid array size.\n");
return 1;
program to calculate the Euclidean distance between two }
points (x1,y1)(x1,y1) and (x2,y2)(x2,y2): int arr[n];
#include <stdio.h> // Input array elements
printf("Enter the elements of the array: ");
#include <math.h> // For sqrt and pow functions for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
int main() {
}
float x1, y1, x2, y2, distance // Input the value to search for
printf("Enter the integer to search for: ");
// Input coordinates of the first point scanf("%d", &searchValue);
// Search for the integer and calculate its frequency printf("The given sides do not form a valid triangle.\n");
for (int i = 0; i < n; i++) { }
if (arr[i] == searchValue) { return 0;
count++; }program to find the largest and second-largest numbers in a
} set of nn integers:
} #include <stdio.h>
// Output the result #include <limits.h> // For INT_MIN
if (count > 0) {
printf("The integer %d appears %d times in the array.\n", int main() {
searchValue, count); int n, num;
} else { int largest = INT_MIN, secondLargest = INT_MIN;
printf("The integer %d does not appear in the array.\n", // Input the number of integers
searchValue); printf("Enter the number of integers (n): ");
} scanf("%d", &n);
return 0; if (n < 2) {
} printf("At least two integers are required.\n");
to determine if three given sides can form a triangle and return 1;
classify the type of triangle: }
#include <stdio.h> // Input integers and find the largest and second-largest
int main() { printf("Enter %d integers:\n", n);
float side1, side2, side3; for (int i = 0; i < n; i++) {
// Input the three sides scanf("%d", &num);
printf("Enter the lengths of the three sides of the triangle: ");
scanf("%f %f %f", &side1, &side2, &side3); if (num > largest) {
// Check if the sides form a valid triangle secondLargest = largest; // Update second largest
if (side1 + side2 > side3 && side1 + side3 > side2 && side2 largest = num; // Update largest
+ side3 > side1) { } else if (num > secondLargest && num < largest) {
printf("The given sides form a valid triangle.\n"); secondLargest = num; // Update second largest
// Check the type of triangle }
if (side1 == side2 && side2 == side3) { }
printf("It is an Equilateral triangle.\n"); // Check if second largest exists
} else if (side1 == side2 || side1 == side3 || side2 == if (secondLargest == INT_MIN) {
side3) { printf("Second largest number does not exist (all numbers
printf("It is an Isosceles triangle.\n"); are equal).\n");
} else { } else {
printf("It is a Scalene triangle.\n"); printf("The largest number is: %d\n", largest);
} printf("The second largest number is: %d\n",
} else { secondLargest);
} default:
return 0; printf("Invalid choice! Please select a valid option.\
} n");
for a menu-driven calculator that performs addition, subtraction, }
multiplication, or division based on the user's choice } while (choice != 5);
#include <stdio.h> return 0;
void add(); }
void subtract(); void add() {
void multiply(); float a, b;
void divide(); printf("\nEnter two numbers to add: ");
int main() { scanf("%f %f", &a, &b);
int choice; printf("Result: %.2f\n", a + b);
do { }
// Displaying the menu void subtract() {
printf("\n--- Menu Driven Calculator ---\n"); float a, b;
printf("1. Addition\n"); printf("\nEnter two numbers to subtract: ");
printf("2. Subtraction\n"); scanf("%f %f", &a, &b);
printf("3. Multiplication\n"); printf("Result: %.2f\n", a - b);
printf("4. Division\n"); }
printf("5. Exit\n"); void multiply() {
printf("Enter your choice (1-5): "); float a, b;
scanf("%d", &choice); printf("\nEnter two numbers to multiply: ");
switch (choice) { scanf("%f %f", &a, &b);
case 1: printf("Result: %.2f\n", a * b);
add(); }
break; void divide() {
case 2: float a, b;
subtract(); printf("\nEnter two numbers to divide: ");
break; scanf("%f %f", &a, &b);
case 3: if (b != 0) {
multiply(); printf("Result: %.2f\n", a / b);
break; } else {
case 4: printf("Error: Division by zero is not allowed.\n");
divide(); }
break; }
case 5: rogram to find the largest and second-largest numbers in a set
printf("Exiting the program. Goodbye!\n"); of nn integers:
break; #include <stdio.h>
#include <limits.h> printf("Enter the x-coordinate: ");
int main() { scanf("%f", &x);
int n, i, num; printf("Enter the y-coordinate: ");
int largest = INT_MIN, secondLargest = INT_MIN; scanf("%f", &y);
// Input the number of elements // Determine the quadrant
printf("Enter the number of elements: "); if (x > 0 && y > 0) {
scanf("%d", &n); printf("The point (%.2f, %.2f) is in Quadrant I.\n", x, y);
if (n < 2) { } else if (x < 0 && y > 0) {
printf("Error: At least two elements are required.\n"); printf("The point (%.2f, %.2f) is in Quadrant II.\n", x, y);
return 1; } else if (x < 0 && y < 0) {
} printf("The point (%.2f, %.2f) is in Quadrant III.\n", x, y);
printf("Enter %d integers:\n", n); } else if (x > 0 && y < 0) {
for (i = 0; i < n; i++) { printf("The point (%.2f, %.2f) is in Quadrant IV.\n", x, y);
scanf("%d", &num); } else if (x == 0 && y == 0) {
// Update largest and secondLargest printf("The point (%.2f, %.2f) lies at the origin.\n", x, y);
if (num > largest) { } else if (x == 0) {
secondLargest = largest; printf("The point (%.2f, %.2f) lies on the y-axis.\n", x, y);
largest = num; } else if (y == 0) {
} else if (num > secondLargest && num != largest) { printf("The point (%.2f, %.2f) lies on the x-axis.\n", x, y);
secondLargest = num; }
} return 0;
} }
// Check if secondLargest was updated
if (secondLargest == INT_MIN) { program to calculate the mean, variance, and standard
printf("All numbers are the same, no second largest deviation for a set of nn integers:
value.\n"); #include <stdio.h>
} else { #include <math.h>
printf("Largest number: %d\n", largest); int main() {
printf("Second largest number: %d\n", secondLargest); int n, i;
} float sum = 0, mean, variance = 0, std_dev;
return 0; // Input the number of elements
} printf("Enter the number of elements: ");
program that accepts the xx and yy coordinates of a point and scanf("%d", &n);
determines the quadrant in which it lies: if (n <= 0) {
#include <stdio.h> printf("Error: Number of elements must be greater than
int main() { zero.\n");
float x, y; return 1;
// Input coordinates }
float numbers[n]; } else if (marks >= 40 && marks < 50) {
// Input the elements printf("Grade: Pass Class\n");
printf("Enter %d numbers:\n", n); } else {
for (i = 0; i < n; i++) { printf("Grade: Fail\n");
scanf("%f", &numbers[i]); }
sum += numbers[i]; // Compute the sum return 0;
} }
// Calculate the mean C program to find the median of a set of nn integers:
mean = sum / n; #include <stdio.h>
// Calculate the variance #include <stdlib.h>
for (i = 0; i < n; i++) { int compare(const void *a, const void *b) {
variance += pow(numbers[i] - mean, 2); return (*(int *)a - *(int *)b); // Comparison function for
} sorting
variance /= n; }
// Calculate the standard deviation float findMedian(int arr[], int n) {
std_dev = sqrt(variance); // Sort the array
// Display the results qsort(arr, n, sizeof(int), compare);
printf("Mean: %.2f\n", mean); // If the number of elements is odd, return the middle
printf("Variance: %.2f\n", variance); element
printf("Standard Deviation: %.2f\n", std_dev); if (n % 2 != 0) {
return 0; return arr[n / 2];
} }
program that accepts the percentage marks obtained by a // If the number of elements is even, return the average of
student and displays the corresponding grade the two middle elements
#include <stdio.h> else {
int main() { return (arr[(n - 1) / 2] + arr[n / 2]) / 2.0;
float marks; }
// Input the percentage marks }
printf("Enter the percentage marks obtained by the student: int main() {
"); int n;
scanf("%f", &marks); // Input the number of elements
// Determine the grade based on marks printf("Enter the number of elements: ");
if (marks > 70) { scanf("%d", &n);
printf("Grade: Distinction\n"); if (n <= 0) {
} else if (marks >= 60 && marks <= 70) { printf("Error: Number of elements must be greater than
printf("Grade: First Class\n"); zero.\n");
} else if (marks >= 50 && marks < 60) { return 1;
printf("Grade: Second Class\n"); }
int arr[n]; #include <stdio.h>
// Input the elements int main() {
printf("Enter %d integers:\n", n); int n;
for (int i = 0; i < n; i++) { // Input the number of elements
scanf("%d", &arr[i]); printf("Enter the number of elements: ");
} scanf("%d", &n);
// Find and print the median int arr[n];
float median = findMedian(arr, n); // Input the elements of the array
printf("The median is: %.2f\n", median); printf("Enter %d integers:\n", n);
return 0; for (int i = 0; i < n; i++) {
} scanf("%d", &arr[i]);
program to display a list of all 3-digit prime numbers in }
descending orde // Reverse the array
#include <stdio.h> int start = 0, end = n - 1, temp;
#include <stdbool.h> while (start < end) {
// Function to check if a number is prime // Swap the elements at start and end
bool isPrime(int num) { temp = arr[start];
if (num <= 1) { arr[start] = arr[end];
return false; arr[end] = temp;
} // Move towards the center
for (int i = 2; i * i <= num; i++) { start++;
if (num % i == 0) { end--;
return false; }
} } // Output the reversed array
return true;
printf("Reversed array:\n");
}
for (int i = 0; i < n; i++) {
int main() {
printf("%d ", arr[i]);
printf("List of 3-digit prime numbers in descending order:\n");
}
// Loop to check for prime numbers in the range of 100 to 999
for (int i = 999; i >= 100; i--) { printf("\n");
if (isPrime(i)) { return 0;
printf("%d ", i); }
} program that accepts the coordinates of the start and end
} points of a line and calculates the inclination angle in degrees:
printf("\n"); #include <stdio.h>
return 0; #include <math.h>
} int main() {
program to reverse the elements of an array of nn integers: float x1, y1, x2, y2;
float slope, angle_radians, angle_degrees; // Input the number of elements
// Input coordinates of the start and end points printf("Enter the number of elements: ");
printf("Enter the coordinates of the start point (x1, y1): "); scanf("%d", &n);
scanf("%f %f", &x1, &y1); int arr[n];
printf("Enter the coordinates of the end point (x2, y2): "); // Input the elements of the array
scanf("%f %f", &x2, &y2); printf("Enter %d integers:\n", n);
// Check if the line is vertical (x1 == x2) to avoid division by zero for (int i = 0; i < n; i++) {
if (x1 == x2) { scanf("%d", &arr[i]);
printf("The line is vertical. The inclination angle is 90 degrees.\n"); }
} else { // Sort the array in descending order
// Calculate the slope of the line bubbleSort(arr, n);
slope = (y2 - y1) / (x2 - x1); // Output the sorted array
// Calculate the inclination angle in radians printf("Array in descending order:\n");
angle_radians = atan(slope); for (int i = 0; i < n; i++) {
// Convert the angle from radians to degrees printf("%d ", arr[i]);
angle_degrees = angle_radians * (180 / M_PI); }
// Display the inclination angle in degrees printf("\n");
printf("The inclination angle of the line is: %.2f degrees\n", return 0;
angle_degrees); } }
return 0; program that accepts a single character and determines
} whether it is a vowel, consonant, digit, or special
program that accepts a set of nn integers and arranges them character:
in descending order using the bubble sort algorithm #include <stdio.h>
#include <stdio.h> #include <ctype.h> // for the isdigit() and isalpha() functions
void bubbleSort(int arr[], int n) { int main() {
int temp; char ch;
for (int i = 0; i < n - 1; i++) { // Input a single character
for (int j = 0; j < n - i - 1; j++) { printf("Enter a character: ");
// Swap elements if they are in the wrong order (descending) scanf("%c", &ch);
if (arr[j] < arr[j + 1]) { // Check if the character is a vowel or consonant
temp = arr[j]; if (ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z') {
arr[j] = arr[j + 1]; // Convert character to lowercase for easy comparison
arr[j + 1] = temp; ch = tolower(ch);
} } }} // Check for vowels
int main() { if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
int n; printf("The character is a Vowel.\n");
} else { return 0;
printf("The character is a Consonant.\n"); }
} } program that accepts an integer number and determines
// Check if the character is a digit whether it is a 1-digit, 2-digit, 3-digit, 4-digit number, or a
else if (ch >= '0' && ch <= '9') { number with more than 4 digits:
printf("The character is a Digit.\n"); #include <stdio.h>
} int main() {
// Otherwise, it's a special character int num;
else { // Input the integer number
printf("The character is a Special Character.\n"); printf("Enter an integer: ");
} scanf("%d", &num);
return 0; // Find the number of digits
} if (num >= 0 && num <= 9) {
program that calculates the kthkth term of the Fibonacci series printf("The number is a 1-digit number.\n");
using a recursive function and displays the first nn terms of the } else if (num >= 10 && num <= 99) {
Fibonacci series: printf("The number is a 2-digit number.\n");
#include <stdio.h> } else if (num >= 100 && num <= 999) {
// Recursive function to calculate the kth Fibonacci number printf("The number is a 3-digit number.\n");
int fibonacci(int k) {
} else if (num >= 1000 && num <= 9999) {
if (k <= 1) {
printf("The number is a 4-digit number.\n");
return k; // Base cases: F(0) = 0, F(1) = 1
} else if (num >= 10000) {
}
printf("The number has more than 4 digits.\n");
return fibonacci(k - 1) + fibonacci(k - 2); // Recursive step
} else if (num <= -10 && num >= -99) {
}
printf("The number is a 2-digit negative number.\n");
int main() {
int n; } else if (num <= -100 && num >= -999) {
// Input the number of terms to display printf("The number is a 3-digit negative number.\n");
printf("Enter the number of terms: "); } else if (num <= -1000 && num >= -9999) {
scanf("%d", &n); printf("The number is a 4-digit negative number.\n");
} else {
printf("The first %d terms of the Fibonacci series are:\n", n); printf("The number has more than 4 digits.\n");
// Display the first n Fibonacci terms using the recursive function }
for (int i = 0; i < n; i++) { return 0;
printf("%d ", fibonacci(i)); }
} program that uses a recursive function to calculate the factorial of an integer number and then calculates
printf("\n"); the combination nCrnCr using the formula:
nCr=n!r!(n−r)!nCr=r!(n−r)!n! For LT consumers, the MSEB calculates electricity bill by
considering following per unit charges: Units Per unit charge
#include <stdio.h> 0-100 3.36 101-300 7.34 301-500 10.37 500 and above
// Recursive function to calculate factorial 11.86 Accept the units consumed by the user and calculate
long long factorial(int n) { electricity bill. Example: If units consumed are 320 then bill
if (n == 0 || n == 1) { amount will be Rs. 2011.40
return 1; // Base case: factorial of 0 or 1 is 1
} #include <stdio.h>
return n * factorial(n - 1); // Recursive case int main() {
} float units, bill = 0.0;
// Function to calculate nCr // Input the number of units consumed
long long nCr(int n, int r) { printf("Enter the number of units consumed: ");
if (r > n) { scanf("%f", &units);
return 0; // Invalid case: r cannot be greater than n // Calculate the electricity bill based on the unit ranges
} if (units <= 100) {
// nCr = n! / (r! * (n - r)!) bill = units * 3.36;
return factorial(n) / (factorial(r) * factorial(n - r)); } else if (units <= 300) {
} bill = 100 * 3.36 + (units - 100) * 7.34;
int main() { } else if (units <= 500) {
int n, r; bill = 100 * 3.36 + 200 * 7.34 + (units - 300) * 10.37;
// Input values for n and r } else {
printf("Enter the value of n: "); bill = 100 * 3.36 + 200 * 7.34 + 200 * 10.37 + (units - 500) * 11.86;
scanf("%d", &n); }
printf("Enter the value of r: "); // Output the total bill
scanf("%d", &r); printf("The electricity bill for %.2f units is Rs. %.2f\n", units, bill);
return 0;
// Calculate nCr }
long long result = nCr(n, r); program that calculates the sum of the first nn natural numbers using a
recursive function
// Output the result
#include <stdio.h>
printf("The value of %dC%d is: %lld\n", n, r, result); // Recursive function to calculate the sum of first n natural numbers
return 0; int sumOfNaturalNumbers(int n) {
} if (n == 1) {
program that accepts the number of units consumed by the return 1; // Base case: the sum of first 1 natural number is 1
user and calculates the electricity bill based on the given per- }
unit charges: return n + sumOfNaturalNumbers(n - 1); // Recursive case
}
int main() { scanf("%d", &score);
int n; // Update the highest score if the current score is higher
// Input the value of n
if (score > highest_score) {
printf("Enter a positive integer n: ");
scanf("%d", &n); highest_score = score;
// Check if the input is positive } }
if (n < 1) { // Output the highest score
printf("Please enter a positive integer.\n"); printf("The highest score is: %d\n", highest_score);
} else { return 0;
// Calculate the sum using the recursive function
int sum = sumOfNaturalNumbers(n);
}
// Output the result program that calculates xyxy (x raised to the power y) using a
printf("The sum of the first %d natural numbers is: %d\n", n, sum); recursive function:
} #include <stdio.h>
return 0; // Recursive function to calculate x raised to the power y
} long long power(int x, int y) {
program that prompts the user (a cricket player) to enter the if (y == 0) {
number of times he has batted in One Day International return 1; // Base case: x^0 is 1 for any x
(ODI) matches, then asks him to input the runs scored in }
each inning, and finally displays his highest score: return x * power(x, y - 1); // Recursive case
#include <stdio.h> }
int main() {
int main() { int x, y;
int n, i, score, highest_score; // Input values for x and y
// Input the number of innings played printf("Enter the base (x): ");
printf("Enter the number of innings played: "); scanf("%d", &x);
scanf("%d", &n); printf("Enter the exponent (y): ");
// Check if the number of innings is valid scanf("%d", &y);
if (n <= 0) { // Check if the exponent is negative
printf("Please enter a valid number of innings.\n"); if (y < 0) {
return 1; // Exit the program if invalid input is entered printf("Exponent cannot be negative in this implementation.\n");
} return 1;
// Initialize highest score to a very low value }
highest_score = -1; // Calculate the power using the recursive function
// Input the scores for each inning and determine the highest score long long result = power(x, y);
for (i = 1; i <= n; i++) { // Output the result
printf("Enter the runs scored in inning %d: ", i); printf("%d raised to the power %d is: %lld\n", x, y, result);
return 0;
} return num == sumOfCubes(num); // Compare the original number with the
program that generates nn lines of the pattern where each line sum of the cubes of its digits
contains numbers from 1 up to the line number: }
#include <stdio.h> int main() {
int main() { int n;
int n, i, j; // Input the 3-digit number
// Input the number of lines printf("Enter a 3-digit number: ");
printf("Enter the number of lines (n): "); scanf("%d", &n);
scanf("%d", &n); // Check if the number is an Armstrong number
// Generate the pattern if (isArmstrong(n)) {
for (i = 1; i <= n; i++) { printf("%d is an Armstrong number.\n", n);
for (j = 1; j <= i; j++) { } else {
printf("%d ", j); // Print numbers from 1 to i printf("%d is not an Armstrong number.\n", n);
} }
printf("\n"); // Move to the next line after printing one row return 0;
} }
return 0; program that defines a function to check if a number is prime
} and uses it to display all 4-digit prime numbers.
program that defines a function to calculate the sum of the #include <stdio.h>
cubes of the digits of a 3-digit number and uses it to check if #include <stdbool.h>
the given number is an Armstrong number. // Function to check if a number is prime
#include <stdio.h> bool isPrime(int num) {
// Function to calculate the sum of cubes of digits of a 3-digit number if (num <= 1) {
int sumOfCubes(int num) { return false; // 0 and 1 are not prime numbers
int sum = 0, digit; }
// Extract digits and calculate the sum of their cubes // Check divisibility from 2 to sqrt(num)
while (num != 0) { for (int i = 2; i * i <= num; i++) {
digit = num % 10; // Extract the last digit if (num % i == 0) {
sum += digit * digit * digit; // Add the cube of the digit to the sum return false; // If num is divisible by i, it is not prime
num /= 10; // Remove the last digit } }
} return true; // num is prime
return sum; // Return the sum of cubes }
} int main() {
// Function to check if the number is an Armstrong number // Display all 4-digit prime numbers
int isArmstrong(int num) { printf("4-digit prime numbers are:\n");
for (int i = 1000; i < 10000; i++) {
if (isPrime(i)) {
printf("%d ", i); // Print the prime number
} }
return 0;
}

You might also like