Posts

C Program to Find Largest Element of an Array

This program takes n number of element from user (where, n is specified by user) and stores data in an array. Then, this program displays the largest element of that array using loops. To understand this example, you should have the knowledge of following C programming topics: C Programming for Loop C Programming Arrays Example: Display Largest Element of an array #include <stdio.h> int main () { int i , n ; float arr [ 100 ]; printf ( "Enter total number of elements(1 to 100): " ); scanf ( "%d" , & n ); printf ( "\n" ); // Stores number entered by the user for ( i = 0 ; i < n ; ++ i ) { printf ( "Enter Number %d: " , i + 1 ); scanf ( "%f" , & arr [ i ]); } // Loop to store largest number to arr[0] for ( i = 1 ; i < n ; ++ i ) { // Change < to > if you want to find the smallest e

C Program to Calculate Average Using Arrays

This program takes n number of element from user (where, n is specified by user), stores data in an array and calculates the average of those numbers. To understand this example, you should have the knowledge of following C programming topics: C program to calculate the power using recursion Source Code to Calculate Average Using Arrays #include <stdio.h> int main() {     int n, i;     float num[100], sum = 0.0, average;     printf("Enter the numbers of elements: ");     scanf("%d", &n);     while (n > 100 || n <= 0)     {         printf("Error! number should in range of (1 to 100).\n");         printf("Enter the number again: ");         scanf("%d", &n);     }     for(i = 0; i < n; ++i)     {         printf("%d. Enter number: ", i+1);         scanf("%f", &num[i]);         sum += num[i];     }     average = sum / n;

C program to calculate the power using recursion

Image
This program calculates the power of a number using recursion. To understand this example, you should have the knowledge of following C programming topics: C Program to Find G.C.D Using Recursion C Program to Find Factorial of a Number Using Recursion calculate power using recursion Example: Program to calculate power using recursion #include <stdio.h> int power(int n1, int n2); int main() {     int base, powerRaised, result;     printf("Enter base number: ");     scanf("%d",&base);     printf("Enter power number(positive integer): ");     scanf("%d",&powerRaised);     result = power(base, powerRaised);     printf("%d^%d = %d", base, powerRaised, result);     return 0; } int power(int base, int powerRaised) {     if (powerRaised != 0)         return (base*power(base, powerRaised-1));     else         return 1; } Output Enter base

C Program to Display Prime Numbers Between Intervals Using Function

Example to print all prime numbers between two numbers (entered by the user) by making a user-defined function. To understand this example, you should have the knowledge of following C programming topics: C Programming for Loop C Programming break and continue Statement C Programming Functions C Programming User-defined functions To find all prime numbers between two integers,  checkPrimeNumber()  function is created. This function checks whether a number is prime or not. Read -  Programming language Example: Prime Numbers Between Two Integers #include <stdio.h> int checkPrimeNumber ( int n ); int main () { int n1 , n2 , i , flag ; printf ( "Enter two positive integers: " ); scanf ( "%d %d" , & n1 , & n2 ); printf ( "Prime numbers between %d and %d are: " , n1 , n2 ); for ( i = n1 + 1 ; i < n2 ; ++ i ) { // i is a prime number, flag will be equal to 1

C Program to Check Prime or Armstrong Number Using User-defined Function

Example to check whether an integer is a prime number or an Armstrong or both by creating two separate functions. To understand this example, you should have the knowledge of following C programming topics: C Programming for Loop C Programming while and do...while Loop C Programming break and continue Statement C Programming Functions Types of User-defined Functions in C Programming In this program, two user-defined functions  checkPrimeNumber()  and  checkArmstrongNumber() are created. The  checkPrimeNumber()  returns 1 if the number entered by the user is a prime number. Similarly,  checkArmstrongNumber()  returns 1 if the number entered by the user is an Armstrong number. Read -  Programming language Visit these pages to learn to check whether a number is a prime number or not an Armstrong number or not Example: Check Prime and Armstrong Number #include <stdio.h> #include <math.h> int checkPrimeNumber ( int n ); int checkA

C Program to Check Whether a Number can be Expressed as Sum of Two Prime Numbers

Example to check if an integer (entered by the user) can be expressed as the sum of two prime numbers of all possible combinations with the use of functions.  Read -  Programming language To understand this example, you should have the knowledge of following C programming topics: C if, if...else and Nested if...else Statement C Programming for Loop C Programming Functions C Programming User-defined functions To accomplish this task,  checkPrime()  function is created. The  checkPrime()  returns 1 if the number passed to the function is a prime number.  Example: Integer as a Sum of Two Prime Numbers #include <stdio.h> int checkPrime ( int n ); int main () { int n , i , flag = 0 ; printf ( "Enter a positive integer: " ); scanf ( "%d" , & n ); for ( i = 2 ; i <= n / 2 ; ++ i ) { // condition for i to be a prime number if ( checkPrime ( i ) == 1 ) {

C Program to Find the Sum of Natural Numbers using Recursion

Example to find the sum of natural numbers by using a recursive function. To understand this example, you should have the knowledge of following C programming topics: C Programming User-defined functions C Programming Recursion The positive numbers 1, 2, 3... are known as natural numbers. The program below takes a positive integer from the user and calculates the sum up to the given number. You can find the sum of natural numbers using loop as well. However, you will learn to solve this problem using recursion here. Read -  Programming language Example: Sum of Natural Numbers Using Recursion #include <stdio.h> int addNumbers ( int n ); int main () { int num ; printf ( "Enter a positive integer: " ); scanf ( "%d" , & num ); printf ( "Sum = %d" , addNumbers ( num )); return 0 ; } int addNumbers ( int n ) { if ( n != 0 ) return n + addNumbers ( n - 1 ); el