Write a program using recursion to find the summation of numbers from 1 to n
Learn how to write a recursive program to calculate the summation of numbers from 1 to n in this easy-to-follow guide.
Learn how to write a recursive program to calculate the summation of numbers from 1 to n in this easy-to-follow guide.
#include <stdio.h>// Function to calculate the power of a number using recursionint power(int x, int y){if (y == 0) // Base case: any number raised to the power of 0 is 1{return 1;}else // Recursive case: multiply x by power of (x, y-1){return (x * power(x, y – 1));}}int main(){int n, m, k;// Ask the […]
The Greatest Common Divisor (GCD) or Highest Common Factor (HCF) of two numbers is the largest number that divides both numbers exactly, without leaving any remainder. The formula used in Euclid’s Algorithm for finding the GCD is based on the idea that: GCD(x, y) = x if y = 0 Otherwise, GCD(x, y) = GCD(y, […]
The Fibonacci series is a sequence where each term is the sum of the two preceding ones, starting from 1 and 1. The sequence looks like this: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, … In this program, we aim to generate and print the Fibonacci series up to a specified […]
#include <stdio.h> // Include the standard I/O header file for using printf and other I/O functionsvoid main() {int i; // Declare an integer variable ‘i’ for loop indexingint array[4] = {10, 20, 30, 40}; // Initialize an array of 4 integers with values 10, 20, 30, and 40// First loop: Increment each element of the […]
#include <stdio.h> // Include the standard input-output libraryint main() // Change void main() to int main(){int array[10]; // Declare an array of size 10int i; // Declare the loop variableprintf(“Enter the elements of the array:n”);// Loop to read 10 integers from user inputfor (i = 0; i < 10; i++) {scanf(“%d”, &array[i]); // Store each […]
#include <stdio.h>// Function to perform bubble sortvoid bubbleSort(int arr[], int n) {for (int i = 0; i < n – 1; i++) {for (int j = 0; j < n – i – 1; j++) {if (arr[j] > arr[j + 1]) {// Swap arr[j] and arr[j + 1]int temp = arr[j];arr[j] = arr[j + 1];arr[j […]
#include <stdio.h>void main() {float a[2][2], b[2][2], c[2][2]; // Declare three 2×2 matricesint i, j;printf(“Enter the elements of the 1st matrix:n”);// Reading the first matrixfor(i = 0; i < 2; i++)for(j = 0; j < 2; j++) {scanf(“%f”, &a[i][j]);}printf(“Enter the elements of the 2nd matrix:n”);// Reading the second matrixfor(i = 0; i < 2; i++)for(j = […]
#include <stdio.h>#define MAX 10 // Maximum size of the matricesvoid multiplyMatrices(int first[MAX][MAX], int second[MAX][MAX], int result[MAX][MAX], int rowFirst, int columnFirst, int rowSecond, int columnSecond);int main() {int first[MAX][MAX], second[MAX][MAX], result[MAX][MAX];int rowFirst, columnFirst, rowSecond, columnSecond;// Input for the first matrixprintf(“Enter rows and columns for first matrix: “);scanf(“%d %d”, &rowFirst, &columnFirst);printf(“Enter elements of the first matrix:n”);for (int i […]
#include <stdio.h>float average(float a[]); // Function prototypeint main() {float avg, c[] = {23.4, 55, 22.6, 3, 40.5, 18}; // Array of agesavg = average(c); // Pass the array to the average functionprintf(“Average age = %.2f”, avg); // Print the averagereturn 0;}float average(float a[]) {int i;float avg, sum = 0.0; // Initialize sum to 0.0for (i […]