Posts

Showing posts from August, 2017

C Program to Find The Largest Number in an Array

This program makes the concept of array more clear. As arrays are continuous memory locations, we can access them easily by incrementing the number (variable) in square bracket. This method is used in this program. Here using loop each and every element is comparing it with the other elements. //program #include <stdio .h>   int main ( ) { int array [ 50 ] , size , i , largest ; printf ( " \n Enter the size of the array: " ) ; scanf ( "%d" , & size ) ; printf ( " \n Enter %d elements of the array: " , size ) ; for ( i = 0 ; i < size ; i ++ ) scanf ( "%d" , & array [ i ] ) ; largest = array [ 0 ] ; for ( i = 1 ; i < size ; i ++ ) { if ( largest < array [ i ] ) largest = array [ i ] ; } printf ( " \n largest element present in the given array is : %d" , largest ) ; return 0 ; }

C Program to Calculate The Average of Array Elements

It is as simple as the previous program in which we calculated the sum of array elements. //Program #include <stdio.h> void 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;     printf("Average = %.2f", average);     return 0; }

C Program to Calculate Sum of Array Elements

Array is a mile stone of C Program which we may use almost everywhere. Here is a basic program to understand array, memory locations and structure of array etc. What is an array? An array is a group of continuous memory locations which we can access by the address. An array of a particular datatype can only store values of that data type(often it convert and use the given data). Array initialization The syntax of array initialization in C is : datatype array_name[size]; Algorithm Here the array named 'array' of type 'int' is used to store different numbers and adding them by separately accessing the array elements using for loop. //Program //Program #include <stdio.h< voide 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).
C Program to Print an Integer (Entered by the User) //Program written by Nimiya Joseph #include <stdio.h> int main() {     int number;     // printf() dislpays the formatted output      printf("Enter an integer: ");            // scanf() reads the formatted input and stores them     scanf("%d", &number);            // printf() displays the formatted output     printf("You entered: %d", number);     return 0; }
A simple C program to display "Hello, World!" on the screen. Since, it's a very simple program, it is often used to illustrate the syntax of a programming language. Program to Display "Hello, World!" //Program written by Nimiya Joseph #include   <stdio.h> int  main () { printf ( "Hello, World!" );  // printf() displays the string inside quotation return   0 ; }

C Program to Disply Armstrong Number Series

What is an Armstrong number? An Armstrong number is a number that is the sum of its own digits each raised to the power of the number of digits. This definition depends on the base b of the number system used, e.g., b  = 10 for the decimal system or b  = 2 for the binary system. (Read more about Armstrong number) For generating Armstrong number series we use the following algorithm. Algorithm A positive integer is called an Armstrong number of order n if abcd... = a n + b n + c n + d n + ...   In case of 3 digit Armstrong numbers the sum of cubes of the digits of the number will be equal to the actual number. Thus we can use a conditional statement to split a number, multiply, add and check with original number.   #include < stdio .h > #include < math .h > int main() { int low, high, i, temp1, temp2, remainder, n = 0, result = 0; printf("Enter two numbers(intervals): "); scanf("%d %d", &low, &high); printf("

C Program to Display Prime Number series

This is a simple program. Here the technique used is to generate prime numbers from one lower limit to one upper limit using loop. For more details refer our previous program : C Program to Check Whether a Number is Prime or Not. //Program #include < stdio.h > int main() {     int low, high, i, flag;     printf("Enter two numbers(intervals): ");     scanf("%d %d", &low, &high);     printf("Prime numbers between %d and %d are: ", low, high);     while (low < high)     {         flag = 0;         for(i = 2; i <= low/2; ++i)         {             if(low % i == 0)             {                 flag = 1;                 break;             }         }         if (flag == 0)             printf("%d ", low);         ++low;     }     return 0; }

C Program to Check Whether a Number is Prime or Not

What is an Armstrong number? An Armstrong number is a number that is the sum of its own digits each raised to the power of the number of digits. This definition depends on the base b of the number system used, e.g., b  = 10 for the decimal system or b  = 2 for the binary system. (Read more about Armstrong number) For generating Armstrong number series we use the following algorithm. Algorithm A positive integer is called an Armstrong number of order n if abcd... = a n + b n + c n + d n + ...   In case of 3 digit Armstrong numbers the sum of cubes of the digits of the number will be equal to the actual number. Thus we can use a conditional statement to split a number, multiply, add and check with original number.   #include < stdio .h > #include < math .h > int main() { int low, high, i, temp1, temp2, remainder, n = 0, result = 0; printf("Enter two numbers(intervals): "); scanf("%d %d", &low, &high); printf("

C Program to check whether a number is palindrome or not

In this program we use if else if ladder mechanism. If an integer is equal to the reverse of it, then it is a palindrome.  Algorithm   In this program we divide the given number by 10 and take the reminder(This could be done by using % operator).  Arrange the reminders in order to get the reverse of original number. Check whether original number is equal to  the reverse of it.   //Program   #include int main () { int n , reversedInteger = 0 , remainder , originalInteger ; printf ( "Enter an integer: " ); scanf ( "%d" , & n ); originalInteger = n ; // reversed integer is stored in variable while ( n != 0 ) { remainder = n % 10 ; reversedInteger = reversedInteger * 10 + remainder ; n /= 10 ; } // palindrome if orignalInteger and reversedInteger are equal if ( originalInteger == reversedInteger ) printf ( "%d is a palindrome." ,

C Program to find the power of a number

It is easy to find the power of any number in calculator. For doing the same in a C program needs  an algorithm.    Algorithm   Identify the base number -> Identify the exponent -> find power. This can be illustrated with an example of 2 3 .  Here Base is '2'; exponent is '3' and the power is 2*2*2 = 8.   #include int main () { int base , exponent ; long result = 1 ; printf ( "Enter a base number: " ); scanf ( "%d" , & base ); printf ( "Enter an exponent: " ); scanf ( "%d" , & exponent ); while ( exponent != 0 ) { result *= base ; -- exponent ; } printf ( "Answer = %ld" , result ); return 0 ; }