Clang Excercises
Odd or Even
This is a simple program to illustrate the coding flow. Since all even numbers are divisible by 2
Algorithm:
READ input into number IF MOD( number , 2 ) IS EQUAL TO ZERO THEN number IS even IF MOD( number , 2 ) IS NOT EQUAL TO ZERO THEN number IS odd.
Code Listing:
int main() { int n; printf ("Enter a number: "); scanf ("%d",&n); if (n%2 ==0 ) { printf ("The number is Even"); } else { printf ("The number is Odd"); } getch(); return 0; }
Temperature Conversion from Farenheit to Celcius :
T(in F) = (9/5) T (in C) + 32
int main() { float T_C,T_F; printf ("Enter temperature in celsius: "); scanf("%f",&T_C); T_F=(1.8*T_C)+32; printf("\n\nTemperature in farenheit: %.2f",f); return 0; }
Factorial of a Number :
Factorial of a number, n = n! = n.(n-1).(n-2).(n-3)…3.2.1 This method is iterative(looping) method. Another method called recursive method is shown in exponential series evaluation.
int main() { int number,fac_num; printf ("Enter a number: "); scanf("%d",&number); int i; for( i = 1 ;i <= number ; i++ ){ fac_num*=i; } printf("Factorial: %d",fac_num); return 0; }
Number Reversal :
If u give input = 123 , the output will be 321 123 = 1( 100)+2(10)+3(1) 321 = 3( 100)+2(10)+1(1)
int main() { int digit,number,reversed; printf ("Enter a number: "); scanf ("%d",&number); reversed = 0; while(number>0) { digit = number % 10; reversed = ( reversed * 10 ) + digit; number /= 10; } printf ("The reverse of the number : %d ,reversed); return 0; }
Evaluation of Exponential Series ( up-to 50 terms) :
ex = ∑n = 0∞ {xn \over n!} = 1 + x + {x2 \over 2!} + {x3 \over 3!} + {x4 \over 4!} + ⋯.
//function to calculate factorial (recursive ) float fac(float i) { if( i == 1) return( 1 ); else return( i * fac (i-1) ); } int main() { float x, exp_x, numer , denom ; float n; printf ("\n Enter the value of x: ") ; scanf ("%f",&x) ; for(n = 0 ; n <= 50 ; i++ ){ numer = pow (x,n); denom = fac ( n ); exp_x = exp_x+numer/denom; } printf ("\n e^%f=%e",x,exp_x); return 0; }
Binary to Decimal Conversion
For conversion from any number system to decimal system the formula is
Decimal Value = $Dn( nth-Weight ) . . . . . D1( 1st-Weight ) + D0(0th-Weight).$
For 4 digit binary number.
Decimal Value = $D3 ( 8 ) + D2 ( 4 ) + D1 ( 2 ) + D0 ( 1 ).$
int main() { int decimal,binary; printf ("Enter the binary code:"); scanf ( "%d",&binary); int temp, i; decimal = 0; while(binary > 0){ temp = binary % 10 ; decimal += temp * pow(2,i); binray /= 10; i++; } printf("\n\nEquivalent decimal value: %d",decimal); return 0; }
Searching an element in an Array :
int main() { int *array; //array int n; //array-length int x; //number to be searched printf ( "Enter the number of elements: " ); scanf ("%d" , &n); array = (int*)malloc( n * sizeof(int)); for(int i=0 ; i < n ; i++ ){ scanf ("%d",&array[i]); } printf( "Enter the number to be searched: "); scanf( "%d" , &x); for(int i=0 ; i<n ; i++){ if( x == array[i]){ printf ("The number is found in the list at place %d",i+1); break; } } return 0; }
Sorting the elements in an arrray :
int main() { int *array; int n; int i,j; printf ( "Enter the number of elements: " ); scanf ("%d" , &n); array = (int*)malloc( n * sizeof(int)); for( i=0 ; i<n ; i++ ){ scanf("%d",&array[i]); } int temp; for( i=0 ; i<n ; i++){ for( j=i ; j<n ; j++){ if(a[i]>a[j]){ temp = array[i]; array[i] = array[j]; array[j] = temp; } } } for( i=0 ; i<3 ; i++){ printf(" %d",array[i]); } return 0; }
Binary Search:
int main() { int *array; int n; int i,j, printf("Enter the number of elements: \n"); scanf("%d",&n); array = (int*)malloc(n*sizeof(int)); printf("Enter the elements one by one:\n"); for(i=0; i<n ;i++){ scanf("%d",&array[i]); } printf("Given array elements\n"); for (i=0; i <n ; i++){ printf("%d\n",array[i]); } /* Bubble sorting begins */ int temp; for(i=0; i< n ; i++){ for(j=0; j< (n-i-1) ; j++){ if(array[j] > array[j+1]){ temp = array[j]; array[j] = array[j+1]; array[j+1] = temp; } } } printf("Sorted array is...\n"); for(i=0; i<n ; i++ ){ printf("%d\n",array[i]); } printf("Enter the element to be searched\n"); scanf("%d", &keynum); /* Binary searching begins */ int keynum; int low,mid,high; low=1; high=n; do { mid= (low + high) / 2; if ( keynum < array[mid] ) high = mid - 1; else if ( keynum > array[mid]) low = mid + 1; } while( keynum!=array[mid] && low <= high); if( keynum == array[mid] ) printf("SUCCESSFUL SEARCH\n"); else printf("Search is FAILED\n"); return 0 ; }
Variance :
#include<math.h> //for sqrt(); int main() { float elements[10],mean,SD,variance,sum; int i,n; printf("Enter the number of elements: "); scanf("%d",&n); printf ("Enter the entries: \n"); for( i=0 , sum = 0; i<n ; i++){ scanf ("%d",&elements[i]); sum += elements[i]; } mean=(float)(sum)/(float)(n); float numerator; for( i=0 , numerator = 0 ; i<n ; i++ ){ numerator+=(float)((mean-elements[i])*(mean-elements[i])); variance = float(numerator / n); SD=sqrt(variance); } printf("\nMean: %.2f\nVariance: %.2f\nStandard Deviation: .2f\n",mean,variance,SD); return 0; }
Matrix Transpose
int main() { int matrix[5][5].n; int temp; int i,j; printf("Enter the no. of rows (or) columns: "); scanf("%d",&r); printf("\n\nEnter the entries: \n"); for ( i=0 ; i<r ; i++) for( j=0 ; j<r ; j++) scanf ( "%d",&a[i][j] ); for( i=0 ; i<r ; i++) for( j=i+1 ; j<r ; j++){ temp=a[i][j]; a[i][j]=a[j][i]; a[j][i]=temp; } printf ("\n\n"); } for(i=0;i<r;i++){ for(j=0;j<r;j++){ printf( " %d",a[i][j]); } printf ("\n"); } return 0; }
Matrix Multiplication
int main() { int a[5][5],b[5][5],c[5][5]; int i,j,k; int r1,r2,c1,c2; printf("Enter the number of rows and columns of matrices A and B:"); scanf("%d%d%d%d",&r1,&c1,&r2,&c2); if( c1 == r2 ) { printf("Enter the entries for matrix A:\n"); for( i=0 ; i<r1 ; i++) for( j=0 ; j<c1 ; j++) scanf("%d",&a[i][j]); printf("\nEnter the entries for matrix B:\n"); for( i=0 ; i<r2 ; i++) for( j=0 ; j<c2 ; j++) scanf("%d",&b[i][j]); //matrix multiplication for( i=0 ; i<r1 ; i++){ for( j=0 ; j<c2 ; j++){ c[i][j] = 0; for( k=0 ; k<r2 ; k++ ) c[i][j] += a[i][k] * b[k][j] ; } } printf("\n\nThe product matrix: \n"); for( i=0 ; i<r1 ; i++){ for( j=0 ; j<c2 ; j++) printf(" %d",c[i][j]); printf("\n"); } }else printf("\n\nThe matrices cannot be multiplied......"); return 0; }