Skip to main content

Posts

Showing posts with the label C

New things learned from C

getchar() - probably i don't know that we can get char using this function also you can use this to capture newline character                 char sample;         sample=getchar(); using scanf statement to get input in different way input: 12-02-2017 scanf(" %d-%d-%d ",&day,&month,&year); you can round digit using normal decimal notation float a=109.7589; printf("%.2f",a); o/p: 109.76 // the last number is increamented by one printing the " using printf using printf(" \" ");

Pattern program

i/p: 3 o/p :          1                 232               45654 c program # include < stdio.h > int main ( ) { int i , m , j , temp = 0 , n ; scanf ( " %d " , & n ) ; for ( i = 1 ; i < = n ; i + + ) { //to print space printf ( " %*s " , ( n - i ) , " " ) ; //to print increment order for ( j = 1 ; j < = i ; j + + ) { printf ( " %d " , + + temp ) ; } //to print decrement order for ( j = 1 ; j < i ; j + + ) printf ( " %d " , temp - j ) ; printf ( " \n " ) ; } return 0 ; }

sleep sort program in c

# include < stdlib.h > # include < unistd.h > # include < sys/types.h > # include < sys/wait.h > // This program works well in linux // or use gcc compiler int main ( int c , char * * v ) { /*fork is used to create a child process        and make the process to sleep for given        milli seconds and atlast the process        with minimum sleep get printed using        this we can sort the array             Note: This is fun program               cannot be used in productive environment        Time complexity is high for big numbers */ while ( - - c > 1 & & ! fork ( ) ) ; sleep ( c = atoi ( v [ c ] ) ) ; wa...

Dynamic allocation of variable using malloc

int main ( ) { int * h = malloc ( sizeof ( int ) * 26 ) ; // using malloc to allocate array of integers for ( int h_i = 0 ; h_i < 26 ; h_i + + ) { scanf ( " %d " , & h [ h_i ] ) ; } char * word = ( char * ) malloc ( 512000 * sizeof ( char ) ) ; // using malloc to allocate string scanf ( " %s " , word ) ; return 0 ; }

Simple C program

i/p  :  getting n terms o/p :  see below example eg: i/p : 1 2 3 4 5 6 7 8 o/p: 1 3 5 7 2 4 6 8 #include<stdio.h> int main(){ int i,a[30],n,odd=0,even; scanf( " %d " ,&n); //slight neat trick to find where even number start even = (n %2 ==0)?n /2:n/ 2+1; for (i=0;i<n;i++){ //loop to seperate the odd index and even index if (i %2 ==0){ scanf( " %d " ,&a[odd++]); } else { scanf( " %d " ,&a[even++]); } } for (i=0;i<n;i++)//neat mission accomplished victory printf ( " %d " ,a[i]); return 0; }

CSI coding contest program one

Read all salary of employee which is stored in input.txt file and sum it and store the "sum of all salary" in output.txt file input.txt file Vineel Phatak, 520, NOBODY Ajay Joshi, 250, Vineel Phatak Abhishek Chauhan, 120, Ajay Joshi Jayesh Godse, 500, NOBODY Vijaya Mundada, 60, Abhishek Chauhan Shital Tuteja, 45, Jayesh Godse Rajan Gawli, 700, Vineel Phatak Zeba Khan, 300, Jayesh Godse Chaitali Sood, 100, Zeba Khan Sheila Rodrigues, 35, Vineel Phatak output 2630 solution # include < stdio.h > # include < ctype.h > # include < string.h > FILE * fp ; static int sal_sum = 0 ; void sum ( char a [ ] ) { int tmp = atoi ( a ) ;//to convert string to integer => atoi sal_sum + = tmp ; } int main ( ) { char str [ 100 ] ; clrscr ( ) ; fp = fopen ( " input.txt " , " r " ) ; while ( ! feof ( fp ) ) { fscanf ( fp , " %s " , str ) ; if ( isdigit ( str [ 0 ] ) ) {//check if the string ...

C program for bracket matching

# include < stdio.h > int main ( ) { char a [ 24 ] , stack [ 24 ] ; int n , i , top = 0 , cnt = 0 ; scanf ( " %s " , a ) ; n = strlen ( a ) ; if ( n % 2 = = 0 ) { for ( i = 0 ; i < n ; i + + ) { switch ( a [ i ] ) { case '(' : case '{' : case '[' : case '<' : stack [ top + + ] = a [ i ] ; break ; case ')' : if ( stack [ - - top ] = = '(' ) { cnt + + ; } break ; case '}' : if ( stack [ - - top ] = = '{' ) cnt + + ; break ; case ']' : if ( stack [ - - top ] = = '[' ) cnt + + ; break ; case '>' : if ( stack [ - - top ] = = '<' ) cnt + + ; break ; default : break ; } } //printf("%s %d %d",stack,cnt,n); if ( n / 2 = = cnt ) printf ( " valid " ) ; else printf ( " invalid " ) ; } else printf ( " invalid " ) ; return 200 ; } if u have ...

C program - sort Big than small

i/p : 1 2 7 8 o/p : 8 1 7 2 # include < stdio.h > void sort ( int * a , int n ) { //bubble sort //i/p : *a - array is passed as reference && n -no of terms int temp , i , j ; for ( i = 0 ; i < n ; i + + ) { for ( j = 0 ; j < n ; j + + ) { if ( a [ i ] < a [ j ] ) { temp = a [ j ] ; a [ j ] = a [ i ] ; a [ i ] = temp ; } } } } int main ( ) { int a [ 10 ] , n , i , cnt_var , l = 0 , m ; // l -> to points the first element in array printf ( " Enter the n terms : " ) ; scanf ( " %d " , & n ) ; printf ( " Enter the array elements : " ) ; for ( i = 0 ; i < n ; i + + ) scanf ( " %d " , & a [ i ] ) ; //call sort function to sort the array (! array is passed as reference ) sort ( & a , n ) ; //cnt_var -> control variable of the loop cnt_var = 0 ; //m -> to points the last position in array m = n - 1 ; //logic is for every even num...