Generate Fibonacci SeriesCheck No. if +/-/0Roots of Quad. EqsCheck Character: Vowel or Consonant?Calculate length of the entered stringPrint Number in Reverse OrderSwap 2 Variables by Pointer AdditionSort an Array Using Quick SortCheck if a no. is an ArmstrongCount Values using Ternary OperatorIllustrate Diff. b/w Iteration & SelectionMatrix Calculation
#include <stdio.h>
int factorial(int n) {
if (n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1);
}
int main() {
int num;
printf("Enter a non-negative integer: ");
scanf("%d", &num);
printf("Factorial of %d = %d\n", num, factorial(num));
return 0;
}
#include <stdio.h>
void generateFibonacci(int n) {
int first = 0, second = 1, next;
printf("Fibonacci Series up to %d terms: ", n);
for (int i = 0; i < n; i++) {
printf("%d, ", first);
next = first + second;
first = second;
second = next;
}
}
int main() {
int terms;
printf("Enter the number of terms for Fibonacci Series: ");
scanf("%d", &terms);
generateFibonacci(terms);
return 0;
}
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0) {
printf("The number %d is positive.\n", num);
} else if (num < 0) {
printf("The number %d is negative.\n", num);
} else {
printf("The number is zero.\n");
}
return 0;
}
#include <stdio.h>
#include <math.h>
int main() {
double a, b, c;
double discriminant, root1, root2;
// Input coefficients a, b, and c
printf("Enter coefficients (a, b, c) of the quadratic equation (ax^2 + bx + c = 0):\n");
scanf("%lf %lf %lf", &a, &b, &c);
// Calculate discriminant
discriminant = b * b - 4 * a * c;
// Check the nature of roots
if (discriminant > 0) {
// Two distinct real roots
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("Roots are real and distinct: %.2lf and %.2lf\n", root1, root2);
} else if (discriminant == 0) {
// One real root (repeated)
root1 = -b / (2 * a);
printf("Roots are real and equal: %.2lf\n", root1);
} else {
// Complex roots
double realPart = -b / (2 * a);
double imaginaryPart = sqrt(-discriminant) / (2 * a);
printf("Roots are complex and imaginary: %.2lf + %.2lfi and %.2lf - %.2lfi\n",
realPart, imaginaryPart, realPart, imaginaryPart);
}
return 0;
}
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
scanf(" %c", &ch);
// Checking if the entered character is an alphabet
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
// Checking if the character is a vowel
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
printf("The character %c is a vowel.\n", ch);
} else {
printf("The character %c is a consonant.\n", ch);
}
} else {
printf("Invalid input. Please enter an alphabet character.\n");
}
return 0;
}
#include <stdio.h>
// Function to calculate the length of a string
int stringLength(const char *str) {
int length = 0;
// Loop until the null character '\0' is encountered
while (str[length] != '\0') {
length++;
}
return length;
}
int main() {
char inputString[100];
printf("Enter a string: ");
scanf("%s", inputString);
// Calculate and display the length of the entered string
printf("Length of the string: %d\n", stringLength(inputString));
return 0;
}
#include <stdio.h>
void printReverseFor(int n) {
printf("Printing in reverse using for loop: ");
for (int i = n; i >= 1; i--) {
printf("%d ", i);
}
printf("\n");
}
void printReverseWhile(int n) {
printf("Printing in reverse using while loop: ");
int i = n;
while (i >= 1) {
printf("%d ", i);
i--;
}
printf("\n");
}
int main() {
int num;
printf("Enter a positive integer: ");
scanf("%d", &num);
printReverseFor(num);
printReverseWhile(num);
return 0;
}
#include <stdio.h>
void swap(int *a, int *b) {
*a = *a + *b;
*b = *a - *b;
*a = *a - *b;
}
int main() {
int num1, num2;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);
swap(&num1, &num2);
printf("After swapping: num1 = %d, num2 = %d\n", num1, num2);
return 0;
}
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return i + 1;
}
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
int main() {
int arr[] = {12, 11, 13, 5, 6, 7};
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
printf("Sorted array: ");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
#include <stdio.h>
#include <math.h>
// Function to check if a number is an Armstrong number
int isArmstrong(int num) {
int originalNum, remainder, result = 0, n = 0;
originalNum = num;
// Counting the number of digits
while (originalNum != 0) {
originalNum /= 10;
++n;
}
originalNum = num;
// Calculating the sum of nth powers of digits
while (originalNum != 0) {
remainder = originalNum % 10;
result += pow(remainder, n);
originalNum /= 10;
}
// Checking if the number is Armstrong
if (result == num)
return 1; // True, the number is Armstrong
else
return 0; // False, the number is not Armstrong
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
// Checking if the entered number is Armstrong
if (isArmstrong(num))
printf("%d is an Armstrong number.\n", num);
else
printf("%d is not an Armstrong number.\n", num);
return 0;
}
#include <stdio.h>
int main() {
int num, positiveCount = 0, negativeCount = 0, zeroCount = 0;
printf("Enter integers (enter 0 to stop):\n");
while (1) {
scanf("%d", &num);
// Check if the entered number is zero, and exit the loop
if (num == 0)
break;
// Use conditional operator to count positive, negative, or zero values
(num > 0) ? positiveCount++ : (num < 0) ? negativeCount++ : zeroCount++;
}
printf("Count of positive numbers: %d\n", positiveCount);
printf("Count of negative numbers: %d\n", negativeCount);
printf("Count of zero values: %d\n", zeroCount);
return 0;
}
#include <stdio.h>
int main() {
// Iteration using a for loop
printf("Iteration using a for loop:\n");
for (int i = 1; i <= 5; i++) {
printf("Iteration %d\n", i);
}
// Iteration using a while loop
printf("\nIteration using a while loop:\n");
int j = 1;
while (j <= 5) {
printf("Iteration %d\n", j);
j++;
}
// Selection using if-else statement
int number;
printf("\nEnter a number: ");
scanf("%d", &number);
if (number > 0) {
printf("The number %d is positive.\n", number);
} else if (number < 0) {
printf("The number %d is negative.\n", number);
} else {
printf("The number is zero.\n");
}
return 0;
}
#include <stdio.h>
// Function to add two matrices
void addMatrices(int firstMatrix[10][10], int secondMatrix[10][10], int result[10][10], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = firstMatrix[i][j] + secondMatrix[i][j];
}
}
}
// Function to display a matrix
void displayMatrix(int matrix[10][10], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
}
int main() {
int rows, cols;
printf("Enter the number of rows and columns for the matrices: ");
scanf("%d %d", &rows, &cols);
int firstMatrix[10][10], secondMatrix[10][10], resultMatrix[10][10];
printf("Enter elements of the first matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &firstMatrix[i][j]);
}
}
printf("Enter elements of the second matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &secondMatrix[i][j]);
}
}
// Adding two matrices
addMatrices(firstMatrix, secondMatrix, resultMatrix, rows, cols);
// Displaying the result matrix
printf("Resultant Matrix:\n");
displayMatrix(resultMatrix, rows, cols);
return 0;
}