C Programming.

1. Computer Programming 2. History of Prog.Language 3. Introduction to C 4. Features of C 5. Starting Turbo C 6. C Tokens 7. Basic requirement of C 8. Comments 9. My First Program in C 10. Line Break 11. C Escape Sequence 12. Constants or Literals 13. Variables in C 14. Data Types in C 15. Range of Data Types in C 16. Declare Variables 17. Reserved Key Words 18. printf( ) statement 19. Chained & Embedded state 20. Prog.to print sum of 2 nos 21. scanf( ) stament 22. print sum & average 23. print Area of Rectangle 24. Area & Circumference of Circle 25. Prg.for Simple Calculator 26. Operators in C 27. Ex. Of Operators 28. Swap two numbers 29. Hierarchy of Operators 30. if( ) statement 31. Prg.to check +ve,-ve or zero 32. if..else statement 33. Prg.to check Odd or Even 34. Prg.to print larger of 2 nos 35. Prg.to print largest of 3 nos 36. Prg.to check divisibility 37. Prg.to check print range 38. for. . . loop statement 39. prg.to print series of nos 40. Prg.to check divisibility 41. prg.to print sum of nos 42. Prg.to print Pyramid 43. prg.for Factorial 44. prg.to fill screen 45. prg.for largest/smallest no 46. prg.to print reverse no 47. prg.to add each digit 48. prg.for sum of factorial 49. prg.for fibonacci series 50. prg.to check Prime nos 51. prg. Prime no from 1 to 100 52. prg.Specified Prime nos 53. while( ) statement 54. do. . While statement 55. goto statement 56. break & continue state. 57. switch case statement 58. Array in C 59. Reverse order using array 60. Ascending/Descending order 61. prg.to search no in Array 62. Double Dimension Array 63. String in C 64. String with spaces in C 65. Print string in reverse 66. prg.to count A,E,I,O,U vowel 67. Function( ) in C 68. Function program examples 69. General Purpose Programs 70. Loan & EMI calculation prg. 71. Table print prg. 72. Leap year program 73. lower to UPPER case prg. 74. Frequency Distribution prg. 75. Bank note calculation prg. 76. Simple Interest prg. 77. Compound Interest prg. 78. Simple Depreciation prg. 79. Reducing bal.Depreciation 80. Marksheet prg. 81. Income Tax prg. 82. Time calculator prg. 83. Distance converter prg. 84. Volume Air Calculation 85. Time to fill Water Tank 86. Salary Calculation prg. 87. Total Sale Calculation prg. 88. Male/Female percentage prg. 89. Library Rent prg. 90. Office Expance prg. 91. Total Salary Calculation prg. 92. Profit or Loss prg. 93. Total Profit/Loss prg. 94. Convert Amt to Word. 95. ASCII Table 96. C Operators 97. C & C++ Reserved Keyword 98. C & C++ Pre-Define Function 99. C Interview Part1 100. C Interview Part2 101. C Interview Part3 102. C Interview Part4 103. C Interview Part5 104. C Interview Part6 105. C Interview Part7 106. C Interview Part8 107. C Interview Part9 108. C Interview Part10 109. C Interview Part11
Pr.Pg Next Pg

C program tutorials examples of operators

C Program Arithmetic operators on int variables

Sam Sir

 

// Program name test013.c

// Arithmetic operators on int variables

#include <stdio.h>

void main()

{ int a,b; // declare two variable a and b as integer

a=10;

b=4;

printf (" a + b = %d \n",(a+b));

printf (" a - b = %d \n",(a-b));

printf (" a * b = %d \n",(a*b));

printf (" a / b = %d \n",(a/b));

printf (" a modulus b = %d",(a%b));

}

Output

a + b = 14

a b = 6

a * b = 40

a / b = 2

a modulus b = 2

 

Note:- All though 10/4 should give you 2.5 answer for division operation, but as a and b variable is integer, the result is shown as 2 only.


 

C Program Arithmetic operators on char variables

Sam Sir

 

// Program name test014.c

// Arithmetic operators on char variables

#include <stdio.h>

void main()

{ char V1,V2; // declare two variable V1 and V2 as character

V1='A'; // Assign A to variable V1

V2='S'; // Assign S to variable V2

printf (" V1 = %c V2 = %c \n",V1,V2);

printf (" ASCII Value of V1 = %d V2 = %d \n",V1,V2);

printf (" V1 + V2 = %d \n",(V1+V2));

printf (" V1 V2 = %d \n",(V1-V2));

printf (" V1 * V2 = %d \n",(V1*V2));

printf (" V1 / V2 = %d \n",(V1/V2));

}

Output

V1 = A V2 = S

ASCII Value of V1 = 65 V2 = 83

V1 + V2 = 148

V1 V2 = -18

V1 * V2 = 5395

V1 / V2 = 0

 

  • If you observe program, we have declared two variables V1 and V2 as Character.

  • V1 = 'A' statement will assign A character to variable V1.

  • Same way in next statement V2 = 'S' statement will assign S character to variable V2.

  • Note When we use %c format specifiers on character variable, it will show its content of variable.

  • When we use %d format specifiers on character variable, it will show its ASCII value.

  • In first printf statement it will print character content of V1 and V2 using %c format specifiers for character.

  • While in next printf statement, it will print ASCII (American Standard Code for Information Interchange) for V1 and V2. For your reference ASCII Value of Capital letter A is 65, for Capital B it is 66, same way for Capital S it is 83. ASCII Value of lowercase letter a is 97, for lowercase b it is 98 and so on.

  • So when we perform arithmetic operation like plus, minus, multiply or division on Character variable, operations are performed on its ASCII value.

 

  • Assume i is integer variable with value 10, f is float variable with value 7.5, c is character variable with value 'A'

 

C Program Arithmetic operators on mixed variables

Sam Sir

 

// Program name test015.c

// Arithmetic operators on mixed variables

#include <stdio.h>

void main()

{ int i = 10; // declare and assign 10 to integer varaiable i

char c = 'A'; // declare and assign A to character variable c

float f = 7.5; // declare and assign 7.5 to float varaiable f

printf(" i + f = %f \n", i+f);

printf(" i + i = %d \n", i+i);

printf(" i + c = %d \n", i+c);

printf("( i + c) - (2 * f /5) = %f \n", ( i + c ) - (2*f/5) );

}

Output

i +f = 17.500000

i + I = 20

i + c = 75

( i + c) - ( 2 * f / 5) = 72.000000

 

  • If you observe variable i is integer, variable c is character and variable 5 is float type.

  • In first printf statement integer + float variable, will give answer as 10 + 7.5 to float 17.5.

  • In second printf statement integer + integer variable, will give answer as 10 + 10 as integer 20.

  • In third printf statement, integer + character variable, will give answer as 10 + ASCII value of c variable, that is 65 +10 as integer 75.

  • Same way last printf statement, first it will add integer 10+ ASCII value of C i.e. 65 to integer 75. Then it will multiply 2 to float 7.5, will generate float 15, float 15 divide by 5 gives, float 3. Then from answer of first bracket 75 integer, 3 float is subtracted, giving final answer as float 72.00000.


 

C Program for Unary pre and post increment and decrement

Sam Sir

 

// Program name test016.c

// Unary pre and post increment and decrement

#include <stdio.h>

void main()

{ int v1,v2,v3,v4;

v1=v2=5;

v3=v4=10;

printf("v1 = %d \n",v1);

printf("v1 = %d \n",v1++); // post increment

printf("v1 = %d \n\n",v1);

 

printf("v2 = %d \n",v2);

printf("v2 = %d \n",++v2); // pre increment

printf("v2 = %d \n\n",v2);

 

printf("v3 = %d \n",v3);

printf("v3 = %d \n",v3--); // post decrement

printf("v3 = %d \n\n",v3);

 

printf("v4 = %d \n",v4);

printf("v4 = %d \n",--v4); // pre decrement

printf("v4 = %d \n",v4);

}

Output

v1 = 5

v1 = 5

v1 = 6

 

v2 = 5

v2 = 6

v2 = 6

 

v3 = 10

v3 = 10

v3 = 9

 

v4 = 10

v4 = 9

v4 = 9

 

  • If you observe first printf statement print value of v1 as 5, then in next statement it will first print value of v1 i.e 5 and then it use post increment after execution of command by 1, so for third printf statement value will be printed as 6.

  • Same way for v2 variable, first printf statement, it will print value of v2 as 5, but in next printf statement, before printing value, we have used pre increment (before exeucition), so it will print it as 6. For third printf statement value remains 6 only.

  • If you observe first printf statement print value of v3 as 10, then in next statement it will first print value of v1 i.e 10 and then it use post decrement after execution of command by 1, so for third printf statement value will be printed as 9.

  • Same way for v4 variable, first printf statement, it will print value of v4 as 10, but in next printf statement, before printing value, we have used pre decrement (before exeucition), so it will print it as 9. For third printf statement value remains 9 only.

Pr.Pg border                                              Next Pg