C Program to Print the Alternate Elements in an Array

#include <stdio.h> int main() { int arr[100], n, i; // Accept number of elements printf(“Enter the number of elements: “); scanf(“%d”, &n); // Accept array elements printf(“Enter %d elements:\n”, n); for (i = 0; i < n; i++) { scanf(“%d”, &arr[i]); } // Print alternate elements (0th, 2nd, 4th, …) printf(“Alternate elements in the array:\n”); […]

Write a program to find GCD of two numbers

The GCD or HCF (Highest Common Factor) of two integers is the greatest integer that divides both the integers with remainder equals to zero. This can be illustrated by Euclid’s remainder Algorithm which states that GCD of two numbers say x and y i.e. GCD(x, y) = x if y is 0 = GCD(y, x%y) […]

Write a program using recursion to find power of a number

We can write, nm = n*nm-1 =n*n*nm-2 =n*n*n*……………m times *nm-m The program which implements the above logic is as follows: #include<stdio.h> int power(int,int); void main() { int n,m,k; printf(“Enter the value of n and m”); scanf(“%d%d”,&n,&m); k=power(n,m); printf(“The value of nm for n=%d and m=%d is %d”,n,m,k); } int power(int x, int y) { if(y==0) […]

Write a program using function to find factorial of a number

#include <stdio.h> // Function to calculate factorial int factorial(int n) { int fact = 1; for(int i = 1; i <= n; i++) { fact *= i; } return fact; } int main() { int num; printf(“Enter a positive integer: “); scanf(“%d”, &num); if(num < 0) { printf(“Factorial is not defined for negative numbers.\n”); } […]

Actual Arguments And Formal Arguments

Actual Arguments These are the real values or variables you pass to a function when calling it. They appear in the function call. int main() { int a = 10, b = 20; add(a, b); // a and b are actual arguments return 0; } Formal Arguments These are the placeholders or parameters defined in […]

C Program to find the reverse of a number

#include <stdio.h> int main() { int num, reversed = 0, remainder; // Input from user printf(“Enter an integer: “); scanf(“%d”, &num); while (num != 0) { remainder = num % 10; // Get the last digit reversed = reversed * 10 + remainder; // Build the reversed number num = num / 10; // Remove […]