Posts

Showing posts from September, 2015

C program to find maximum element in array

C program to find maximum element in array #include <stdio.h>   int main ( ) { int array [ 100 ] , maximum , size , c , location = 1 ;   printf ( "Enter the number of elements in array \n " ) ; scanf ( "%d" , & size ) ;   printf ( "Enter %d integers \n " , size ) ;   for ( c = 0 ; c < size ; c ++ ) scanf ( "%d" , & array [ c ] ) ;   maximum = array [ 0 ] ;   for ( c = 1 ; c < size ; c ++ ) { if ( array [ c ] > maximum ) { maximum = array [ c ] ; location = c + 1 ; } }   printf ( "Maximum element is present at location %d and it's value is %d. \n " , location , maximum ) ; return 0 ;   } ==================================================================== C programming code to find maximum using function #include <stdio.h>   int find_maximum ( int [ ] , int ) ;   int main ( ) { int ...

C program to print Pascal triangle

     C program to print Pascal triangle    1 1 1 1 2 1 1 3 3 1 #include <stdio.h>   long factorial ( int ) ;   int main ( ) { int i , n , c ;   printf ( "Enter the number of rows you wish to see in pascal triangle \n " ) ; scanf ( "%d" ,& n ) ;   for ( i = 0 ; i < n ; i ++ ) { for ( c = 0 ; c <= ( n - i - 2 ) ; c ++ ) printf ( " " ) ;   for ( c = 0 ; c <= i ; c ++ ) printf ( "%ld " , factorial ( i ) / ( factorial ( c ) * factorial ( i - c ) ) ) ;   printf ( " \n " ) ; }   return 0 ; }   long factorial ( int n ) { int c ; long result = 1 ;   for ( c = 1 ; c <= n ; c ++ ) result = result * c ;   return result ; }

C program to print Floyd's triangle

C program to print Floyd's triangle 1 2 3 4 5 6 7 8 9 10       #include <stdio.h>   int main ( ) { int n , i , c , a = 1 ;   printf ( "Enter the number of rows of Floyd's triangle to print \n " ) ; scanf ( "%d" , & n ) ;   for ( i = 1 ; i <= n ; i ++ ) { for ( c = 1 ; c <= i ; c ++ ) { printf ( "%d " , a ) ; a ++; } printf ( " \n " ) ; }   return 0 ; } C program to print Floyd's triangle using recursion #include <stdio.h>   void print_floyd ( int ) ;   int main ( ) { int n , i , c , a = 1 ;   printf ( "Input number of rows of Floyd's triangle to print \n " ) ; scanf ( "%d" , & n ) ;   print_floyd ( n ) ;   return 0 ; }   void print_floyd ( int n ) { static int row = 1 , c = 1 ; int d ;   if ( n <= 0 ) return ;   for ( d =...

C program to generate and print armstrong numbers

C program to generate and print armstrong numbers C program to generate Armstrong numbers. In our program user will input two integers and we will print all Armstrong numbers between two integers. Using a for loop we will check numbers in the desired range. In our loop we call our function check_armstrong which returns 1 if number is Armstrong and 0 otherwise. If you are not familiar with Armstrong numbers see  #include <stdio.h>   int check_armstrong ( int ) ; int power ( int , int ) ;   int main ( ) { int c , a , b ;   printf ( "Input two integers \n " ) ; scanf ( "%d%d" , & a , & b ) ;   for ( c = a ; c <= b ; c ++ ) { if ( check_armstrong ( c ) == 1 ) printf ( "%d \n " , c ) ; }   return 0 ; }   int check_armstrong ( int n ) { long long sum = 0 , temp ; int remainder , digits = 0 ;   temp = n ;   while ( temp != 0 ) ...

Armstrong number c program

 Armstrong number c program Armstrong number c program: c programming code to check whether a number is Armstrong or not. Armstrong number is a number which is equal to sum of digits raise to the power total number of digits in the number. Some Armstrong numbers are: 0, 1, 2, 3, 153, 370, 407, 1634, 8208 etc. Read more about Armstrong numbers at Wikipedia . We will consider base 10 numbers in our program. Algorithm to check Armstrong is: First we calculate number of digits in our program and then compute sum of individual digits raise to the power number of digits. If this sum equals input number then number is Armstrong otherwise not an Armstrong number. Examples: 7 = 7^1 371 = 3^3 + 7^3 + 1^3 (27 + 343 +1) 8208 = 8^4 + 2^4 +0^4 + 8^4 (4096 + 16 + 0 + 4096). 1741725 = 1^7 + 7^7 + 4^7 + 1^7 + 7^7 + 2^7 +5^7 (1 + 823543 + 16384 + 1 + 823543 +128 + 78125) . #include <stdio.h>   int power ( int , int ) ;   int main ( ) { int n , sum...

Prime number program in c language

Prime number program in c language #include<stdio.h>   int main ( ) { int n , i = 3 , count , c ;   printf ( "Enter the number of prime numbers required \n " ) ; scanf ( "%d" ,& n ) ;   if ( n >= 1 ) { printf ( "First %d prime numbers are : \n " , n ) ; printf ( "2 \n " ) ; }   for ( count = 2 ; count <= n ; ) { for ( c = 2 ; c <= i - 1 ; c ++ ) { if ( i % c == 0 ) break ; } if ( c == i ) { printf ( "%d \n " , i ) ; count ++; } i ++; }   return 0 ; } =================================================== C program for prime number or not #include<stdio.h>   main ( ) { int n , c = 2 ;   printf ( "Enter a number to check if it is prime \n " ) ; scanf ( "%d" ,& n ) ;   for ( ...

C program to Calculate the Power of a Number Using Recursion

C program to Calculate the Power of a Number Using Recursion /* Source Code to calculate power using recursive function */ #include <stdio.h> int power ( int n1 , int n2 ); int main () { int base , exp ; printf ( "Enter base number: " ); scanf ( "%d" ,& base ); printf ( "Enter power number(positive integer): " ); scanf ( "%d" ,& exp ); printf ( "%d^%d = %d" , base , exp , power ( base , exp )); return 0 ; } int power ( int base , int exp ) { if ( exp != 1 ) return ( base * power ( base , exp - 1 )); }

C program to print diamond pattern

 C program to print diamond pattern * *** ***** *** * #include <stdio.h>   int main ( ) { int n , c , k , space = 1 ;   printf ( "Enter number of rows \n " ) ; scanf ( "%d" , & n ) ;   space = n - 1 ;   for ( k = 1 ; k <= n ; k ++ ) { for ( c = 1 ; c <= space ; c ++ ) printf ( " " ) ;   space --;   for ( c = 1 ; c <= 2 * k - 1 ; c ++ ) printf ( "*" ) ;   printf ( " \n " ) ; }   space = 1 ;   for ( k = 1 ; k <= n - 1 ; k ++ ) { for ( c = 1 ; c <= space ; c ++ ) printf ( " " ) ;   space ++;   for ( c = 1 ; c <= 2 * ( n - k ) - 1 ; c ++ ) printf ( "*" ) ;   printf ( " \n " ) ; }   return 0 ; } ====================================================================================== C program to print diamond using recursio...

C program to print patterns of numbers and stars

C program to print patterns of numbers and stars  1. * *** ***** ******* ********* #include <stdio.h>   int main ( ) { int row , c , n , temp ;   printf ( "Enter the number of rows in pyramid of stars you wish to see " ) ; scanf ( "%d" ,& n ) ;   temp = n ;   for ( row = 1 ; row <= n ; row ++ ) { for ( c = 1 ; c < temp ; c ++ ) printf ( " " ) ;   temp --;   for ( c = 1 ; c <= 2 * row - 1 ; c ++ ) printf ( "*" ) ;   printf ( " \n " ) ; }   return 0 ; } ===================================================================== 2. * ** *** **** ***** #include <stdio.h>   int main ( ) { int n , c , k ;   printf ( "Enter number of rows \n " ) ; scanf ( "%d" ,& n ) ;   for ( c = 1 ; c <= n ; c ++ ) { f...