Some Imp. Concepts And Basics Of C

  • Uploaded by: Anik
  • 0
  • 0
  • April 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Some Imp. Concepts And Basics Of C as PDF for free.

More details

  • Words: 2,690
  • Pages: 23
#include <stdio.h> void main() { printf("\"It's freezing in here,\" he said coldly."); }

/* Program Characters and numbers */ #include <stdio.h> void main() { char first = 'A'; char second = 65; printf("\n printf("\n printf("\n printf("\n

letter number letter number

looks looks looks looks

like %c", first); like %d", first); like %c", second); like this %d\n", second);

} /* Program Finding the limits */ #include <stdio.h> #include #include void main() { clrscr(); printf("char store values from %d to %d", CHAR_MIN, CHAR_MAX); printf("\nshort store values from %d to %d", SHRT_MIN, SHRT_MAX); printf("\nint store values from %d to %d", INT_MIN, INT_MAX); printf("\nlong store values from %ld to %ld",LONG_MIN, LONG_MAX); printf("\n\n smallest non-zero value of type float is %.3f",FLT_MIN); printf("\n largest value of type float is %.3f", FLT_MAX); printf("\nsmallest non-zero value of type double is %.3f",DBL_MIN); printf("\n largest value of type double is %.3f", DBL_MAX); }

/* Program Finding the size of a type #include <stdio.h>

*/

void main() { clrscr(); printf("\nchar occupy %d bytes", sizeof(char)); printf("\nshort occupy %dbytes",sizeof(short)); printf("\nint occupy %d bytes", sizeof(int)); printf("\nlong occupy %d bytes",sizeof(long)); printf("\n float occupy %d ytes",sizeof(float)); printf("\n double occupy %d bytes", sizeof(double)); getch(); } /* Program Testing letters the easy way */ #include <stdio.h> void main() { char letter =0; clrscr(); printf("Enter an upper case letter:"); scanf(" %c", &letter); if ((letter >= 'A') && (letter <= 'Z')) { letter += 'a'-'A'; printf("You entered an upper-case %c.\n", letter); } else printf("You did not enter an uppercase letter.\n"); getch(); }

/*Example A calculator*/ #include <stdio.h> void main() { float number1 = 0.0; float number2 = 0.0; char operation ; clrscr(); printf("\nEnter the calculation\n"); scanf("%f %c %f", &number1, &operation, &number2); switch(operation) { case '+': printf("= %f\n", number1 + number2); break; case '-': printf("= %f\n", number1 - number2); break; case '*': printf("= %f\n", number1 * number2); break; case '/': if(number2 == 0) printf("\n\n\aDivision by zero error!\n"); else printf("= %f\n", number1 / number2); break; case '%': if((int)number2 == 0) printf("\n\n\aDivision by zero error!\n"); else printf("= %d\n", (int)number1 % (int)number2); break; default: printf("\n\n\aIllegal operation!\n"); } getch(); }

/*A calculator that allows multiple calculations */ #include <stdio.h> void main() { double number1 = 0.0; double number2 = 0.0; char operation ; char answer; start: printf("\nEnter the calculation as number operator number\n"); scanf("%lf %c %lf", &number1, &operation, &number2); /* Code to check the input goes here */ switch(operation) { case '+': printf("= %lf\n", number1 + number2); break; case '-': printf("= %lf\n", number1 - number2); break; case '*': printf("= %lf\n", number1 * number2); break; case '/': if(number2 == 0) printf("\n\n\aDivision by zero error!\n"); else printf("= %lf\n", number1 / number2); break; case '%': if((long)number2 == 0) printf("\n\n\aDivision by zero error!\n"); else printf("= %ld\n", (long)number1 % (long)number2); break; default: printf("\n\n\aIllegal operation!\n"); } printf("\n Do you want to do another calculation? (y or n): "); scanf(" %c", &answer); if(answer == 'y' || answer == 'Y') goto start; }

#include <stdio.h> main() { int digit = 1; clrscr(); while (digit <= 10) { printf ("%d\t", digit ) ; ++digit; } getch(); } #include <stdio.h> main() { int digit = 1; clrscr(); while (digit <= 10) { printf ("%d\t", digit ) ; digit++; } getch(); } #include <stdio.h> main() { int digit =1; clrscr(); while (digit <= 10) printf ( "%d\t", digit++) ; getch(); }

#include <stdio.h> main() { int digit =1; clrscr(); while (digit <= 10) printf ( "%d\t", ++digit) ; getch(); }

/* calculate the average of n numbers */ #include <stdio.h> main() { int n, count = 1; float x, average, sum = 0; clrscr(); printf("How many numbers? " ) ; scanf ("%d", &n) ; while (count <= n) { printf ( " x = " ) ; scanf("%f", &x); sum += x; ++count; } /* calculate the average and display the answer */ average = sum/n; printf("\nThe average i s %.2f\n", average); getch(); } #include <stdio.h> void main() { int sum = 0; int i = 1; int count = 0; clrscr(); printf("\nEnter the number of integers you want to sum: "); scanf("%d", &count); while(i <= count) sum += i++; printf("Total of the first %d numbers is %d\n", count, sum); printf("average=%.2f",(float)sum/count); getch(); }

#include <stdio.h> main() { int digit = 1; clrscr(); do { printf("%d\t", digit++); }while (digit <= 10); getch(); } #include <stdio.h> main() { int digit = 1; do { printf("%d\t", ++digit); }while (digit <= 10); getch(); } #include <stdio.h> void main( void ) { int counter = 1; clrscr(); do { printf( "%d ", counter ); } while ( ++counter <= 10 ); getch(); }

/* calculate the average of n numbers */ #include <stdio.h> main () { int n, count = 1 ; float x, average, sum = 0; printf ("How many numbers? " ) ; scanf ("%d", &n) ; do { printf ( " x = " ) ; scanf("%f", &x); sum += x; ++count; } while (count <= n); /* calculate the average and display the answer */ average = sum/n; printf ( "\nThe average is %.2f", average) ; getch(); } #include <stdio.h> main() { int digit; clrscr(); for (digit = 0; digit <= 10; ++digit) printf ( "%d " ,digit ); getch(); } #include <stdio.h> main() { int digit = 0; clrscr(); for (; digit <= 10; ) printf ("%d " , digit++ ) ; getch(); }

#include <stdlib.h> void main() { int i; clrscr(); for ( i = 1; i <= 20; i++ ) { printf( "%d ", 1 + ( rand() % 6 ) ); if ( i % 5 == 0 ) printf( "\n" ); } getch(); } /*Randomizing die-rolling program */ #include <stdlib.h> #include <stdio.h> void main() { int i; unsigned seed; clrscr(); printf( "Enter seed: " ); scanf( "%u", &seed ); srand( seed ); for ( i = 1; i <= 20; i++ ) { printf( "%d ", 1 + ( rand() % 6 ) ); if ( i % 5 == 0 ) printf( "\n" ); } getch(); }

/* Roll a six-sided die 6000 times */ #include <stdio.h> #include <stdlib.h> void main() { int face, roll, frequency1 = 0, frequency2 = 0, frequency3 = 0, frequency4 = 0, frequency5 = 0, frequency6 = 0; for ( roll = 1; roll <= 6000; roll++ ) { face = 1 + rand() % 6; switch ( face ) { case 1: ++frequency1; break; case 2: ++frequency2; break; case 3: ++frequency3; break; case 4: ++frequency4; break; case 5: ++frequency5; break; case 6: ++frequency6; break; } } printf( printf( printf( printf( printf( printf( printf( }

"%s%13s\n", "Face", "Frequency" ); " 1%13d\n", frequency1 ); " 2%13d\n", frequency2 ); " 3%13d\n", frequency3 ); " 4%13d\n", frequency4 ); " 5%13d\n", frequency5 ); " 6%13d\n", frequency6 );

/* Program Reversing the digits */ #include <stdio.h> void main() { int number = 0; int rnum = 0; int temp = 0; printf("\nEnter a positive integer: "); scanf("%d", &number); temp = number; do { rnum = 10*rnum + temp % 10; temp = temp/10; } while (temp); printf ("\nThe number %d reversed is

%d \n", number, rnum );

}

/* What does this program print? */ #include <stdio.h> void main( void ) { int count = 1; while ( count <= 10 ) { printf( "%s\n", count % 2 ? "****" : "++++++++" ); count++; } } /* Initializing an array with a initializer list */ #include <stdio.h> void main( void ) { int n[ 10 ] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 }; int i; /* counter */ printf( "%s%13s\n", "Element", "Value" ); for ( i = 0; i < 10; i++ ) { printf( "%7d%13d\n", i, n[ i ] ); }

/* Program Averaging ten numbers - storing the numbers the easy way */ #include <stdio.h> void main() { int numbers[10]; int count = 10; int sum = 0; float average = 0.0f; int i = 0; printf("\nEnter the 10 numbers:\n"); /* Read the ten numbers to be averaged */ for(i = 0; i < count; i ++) { printf("%2d> ",i+1); scanf("%d", &numbers[i]); sum += numbers[i]; } average = (float)sum/count; printf("Average of the ten numbers entered is: %.2f\n", average); } /* Histogram printing program */ #include <stdio.h> #define SIZE 10 void main( void ) { /* use initializer list to initialize array n */ int n[ SIZE ] = { 19, 3, 15, 7, 11, 9, 13, 5, 17, 1 }; int i; /* outer for counter for array elements */ int j; /* inner for counter counts *s in each histogram bar */ printf( "%s%13s%17s\n", "Element", "Value", "Histogram" ); /* for each element of array n, output a bar of the histogram */ for ( i = 0; i < SIZE; i++ ) { printf( "%7d%13d ", i, n[ i ]) ; for ( j = 1; j <= n[ i ]; j++ ) { printf( "%c", '*' ); } printf( "\n" ); }

} /*Creating and using a programmer-defined function */ #include <stdio.h> int square( int y ); void main( void ) { int x; for ( x = 1; x <= 10; x++ ) { printf( "%d ", square( x ) ); } printf( "\n" ); } int square( int y ) { return y * y; }

/* Finding the maximum of three integers */ #include <stdio.h> int maximum( int x, int y, int z ); void main( void ) { int number1; int number2; int number3; printf( "Enter three integers: " ); scanf( "%d%d%d", &number1, &number2, &number3 ); printf( "Maximum is: %d\n", maximum( number1, number2, number3 ) ); } int maximum( int x, int y, int z ) { int max = x; if ( y > max ) { max = y; } if ( z > max ) { max = z; } return max; }

/* A scoping example */ #include <stdio.h> void useLocal( void ); void useStaticLocal( void ); void useGlobal( void ); int x = 1; main( void ) { int x = 5; printf("local x in outer scope of main is %d\n", x ); { int x = 7; printf( "local x in inner scope of main is %d\n", x ); } printf( "local x in outer scope of main is %d\n", x ); useLocal(); useStaticLocal(); useGlobal(); useLocal(); useStaticLocal(); useGlobal(); printf( "\nlocal x in main is %d\n", x ); } void useLocal( void ) { int x = 25; printf( "\nlocal x in useLocal is %d after entering useLocal\n", x ); x++; printf( "local x in useLocal is %d before exiting useLocal\n", x ); }

void useStaticLocal( void ) { /* initialized only first time useStaticLocal is called */ static int x = 50;

printf( "\nlocal static x is %d on entering useStaticLocal\n", x ); x++; printf( "local static x is %d on exiting useStaticLocal\n", x ); } void useGlobal( void ) { printf( "\nglobal x is %d on entering useGlobal\n", x ); x *= 10; printf( "global x is %d on exiting useGlobal\n", x ); }

/*Passing arrays to functions */ #include <stdio.h> #define SIZE 5 void modifyArray( int [], int ); int main() { int a[ SIZE ] = { 0, 1, 2, 3, 4 }, i; for ( i = 0; i < SIZE; i++ ) printf( "%3d", a[ i ] ); printf( "\n" ); modifyArray( a, SIZE ); /* passed call by reference */ printf( "The values of the modified array are:\n" ); for ( i = 0; i < SIZE; i++ ) printf( "%3d", a[ i ] ); return 0; } void modifyArray( int b[], int size ) { int j; for ( j = 0; j <= size - 1; j++ ) b[ j ] *= 2; }

/* Demonstrating the const type qualifier with arrays */ #include <stdio.h> void tryToModifyArray( const int [] ); int main() { int a[] = { 10, 20, 30 }; tryToModifyArray( a ); printf("%d %d %d\n", a[ 0 ], a[ 1 ], a[ 2 ] ); return 0; } void tryToModifyArray( const int b[] ) { b[ 0 ] /= 2; /* error */ b[ 1 ] /= 2; /* error */ b[ 2 ] /= 2; /* error */ }

/* Initializing multidimensional arrays */ #include <stdio.h> void printArray( const int [][ 3 ] ); int main() { int array1[ 2 ][ 3 ] = { { 1, 2, 3 }, { 4, 5, 6 } }, array2[ 2 ][ 3 ] = { 1, 2, 3, 4, 5 }, array3[ 2 ][ 3 ] = { { 1, 2 }, { 4 } }; printf( "Values in array1 by row are:\n" ); printArray( array1 ); printf( "Values in array2 by row are:\n" ); printArray( array2 ); printf( "Values in array3 by row are:\n" ); printArray( array3 ); return 0; } void printArray( const int a[][ 3 ] ) { int i, j; for ( i = 0; i <= 1; i++ ) { for ( j = 0; j <= 2; j++ ) printf( "%d ", a[ i ][ j ] ); printf( "\n" ); } }

/* Recursive factorial function */ #include <stdio.h> long factorial( long number ); void main( void ) { int i; for ( i = 0; i <= 10; i++ ) { printf( "%2d! = %ld\n", i, factorial( i ) ); }

} long factorial( long number ) { if ( number <= 1 ) { return 1; } else { return ( number * factorial( number - 1 ) ); } }

/* Program Displaying a string */ #include <stdio.h> void main() { printf("The character \0 is used to terminate a string"); } /* Program Lengths of strings */ #include <stdio.h> void main() { char str1[40] = "To be or not to be"; int count = 0; while (str1[count] != '\0') count++; printf("\nThe length of the string %s is %d characters.", str1, count); }

/* Program Joining strings */ #include <stdio.h> void main() { char str1[40] = "To be or not to be"; char str2[40] = ",that is the question"; int count1 = 0; int count2 = 0; while (str1[count1] != '\0') count1++; while (str2[count2] != '\0') count2++; /* Check that we have enough space for both strings if(sizeof str1 < count1 + count2 + 1) printf("\length is small"); else { count2 = 0; while(str2[count2] != '\0') str1[count1++] = str2[count2++];

*/

str1[count1] = '\0'; printf("\n%s\n", str1 ); } }

/* Program Joining strings - revitalised */ #include <stdio.h> #include <string.h> #define STR_LENGTH 40 void main() { char str1[STR_LENGTH] = "To be or not to be"; char str2[STR_LENGTH] = ",that is the question"; if(STR_LENGTH

>

strlen(str1)

+

strlen(str2))

*/ printf("\n%s\n", strcat(str1, str2) ); else printf("\n can't combine"); }

/*

Enough

space

?

/* Program Testing characters in a string */ #include <stdio.h> #include void main() { char buffer[80]; int i = 0; int num_letters = 0; int num_digits = 0; printf("\nEnter characters:\n"); gets(buffer);

an

interesting

string

of

less

/* Read a string into buffer

than */

while(buffer[i] != '\0') { if(isalpha(buffer[i])) num_letters++; /* Increment letter count */ if(isdigit(buffer[i])) num_digits++; /* Increment digit count */ i++; } printf("\nYour string contained %d letters and %d digits.\n", num_letters, num_digits); }

80

/* Program Arrays of strings */ #include <stdio.h> void main() { char str[][40] = { "To be or not to be" , ",that is the question" }; int count1 = 0; /* Length of first string */ int count2 = 0; /* Length of second string */ /* find the length of the first string */ while (str[0][count1] != '\0') count1++; /* Find the length of the second string */ while (str[1][count2] != '\0') count2++; /* Check that we have enough space for both strings if (sizeof str[0] < count1 + count2 + 1) printf("\n can't combine"); else

*/

{ /* Copy 2nd string to first */ count2 = 0; while ((str[0][count1++] = str[1][count2++]) != '\0'); printf("\n%s", str[0]); } }

/* Output combined string */

/* Comparing strings #include <stdio.h> #include <string.h>

*/

void main() { char word1[20]; char word2[20]; printf("\nType in the first word:\n1: "); scanf("%s", word1); printf("Type in the second word:\n 2: "); scanf("%s", word2); /* Compare the two words */ if(strcmp(word1,word2) == 0) printf("You have entered identical words"); else printf("%s comes before %s", (strcmp(word1, word2) < 0) ? word1 : word2, (strcmp(word1, word2) < 0) ? word2 : word1); }

Related Documents

Rc- Imp Concepts
October 2019 7
Some Imp Tips.pdf
April 2020 15
Some Imp Tips.pdf
April 2020 15
Some Imp Tips.pdf
October 2019 18

More Documents from ""