## Posts

Showing posts from September, 2010

### C Program 2 – Electricity Bill calculation

This program is to display the electricity bill calculation based on the number of units consumed every monthInput : the number of units – variable name – unitOutput – Amount of rupee – variable name – amountLogic: UnitsRupees1-50 units0.75/unit51-1000.85/unit101-2001.50/unit201-3002.20/unit>3003.00/unitIn all the cases, there will be service charge of Rs.20 will be collected.#include <stdio.h>
#include <conio.h>
int main()
{
float amount=0,units;;
printf("Enter the number of units");
scanf("%f", &units);
if(units <=50)
{
amount = units * 0.75;
}
else if(units >50 && units <=100)
{
amount=0.75 * 50 + 0.85*(units-50);
}
else if(units >100 && units <200)
{
amount=(0.75*50) + (0.85*50 )+ (1.5 *(units-100));
}
else if(un…

### C Program 1 – To find the reversal of a given number

Assume the number 1234, after reversal the number becomes 4321. Input: One number, Ex 1234, declared to numOutput : One number, Ex 4321, rev is the variableLogic: separate each digit using modulo operator from the right and bring that number to frontdivide the given number by 10, so the right most digit will be discarded.run till the number becomes <=0output the result#include <stdio.h>
#include <conio.h>
int main()
{
int num,rev=0;
printf("enter the number to be reversed");
scanf("%d", &num);
while(num>0) //till the number is positive, perform the process
{
rev=rev*10 + (num%10);
//separate each digit and bring it to the first
num=num/10;
}
printf("The reversal is %d ",rev);
getch();
return 0;
}

### Decision Making and Looping

C supports Decision Making and Looping (Interations)In decision Making, if, if-else, if- else if – else, switch – case is used.if(condition){//These statements are executed only if the condition is true or 1}if – elseif(condition){//These statements are executed only if the condition is true or 1}else{//these statement are executed only if the condition is false or 0}(in the above example, there is no else part means that for some situation, there may not be a need for else part)if – else if – else if(condition 1){//These statements are executed if the condition 1 is true or 1}else if (condition 2){//These statements are executed if the condition 2 is true or 1}else{//These statements are executed neither condition 1 nor condition 2 are true // if all the conditions fails, then this statement will execute}Switch Case switch (expression){case 1: statement 1;break;case 2: statement 2;break;case 3: statement 3;break;default: statement d;break;}//Statement n;The switch case works similar …

### Data types

Declaration of variable names does two thingsit tells the compiler what the variable name is specifies what type of data the variable will holdSo it is mandatory that each variable should belong to a particular data type. C supports different data types like integer, character, float, double, etc.Data type Size on a 16 bit machineRangechar1 byte or 8 bits-128 to 127signed char1 byte-128 to 127unsigned char1 byte0 to 255short int or short1 byte-128 to 127int2 bytes-32768 to 32767unsigned int2 bytes0 to 65535long int or long4 bytes-2,147,483,648 to 2,147,483,647unsigned long int4 bytes0 to 4,294,967,295float4 bytes3.4E-38 to 3.4 E+38double8 bytes1.7E-308 to 1.7E+308long double10 bytes3.4E-4932 to 1.1E+4932Example of declaring variablesint a=20,b=20,c;int a=10;int b=9;int c=20;float c=3.5;char a =’A’;long int al or long a;short int b or short b;double c;long double c;

### Tokens in C

Tokens are classified as KeywordsIdentifiersOperatorsConstantsSpecial SymbolsKeywordsKeywords are reserved by the compilers and keywords will not be redefined (means the keywords are not been declared as variable names, etc). There are standard 32 keywords in C compiler, and some compilers uses more keywords based on their specification. Here is the list of 32 keywordsautobreakcasecharconstcontinuedefaultdodoubleelseenumexternfloatforgotoifintlongregisterreturnshortsignedsizeofstaticstructswitchtypedefunionunsignedvoidvolatilewhileIdentifiersIdentifiers are variable names declared inside the program. They use alphabets, number, symbols like _ and \$They must begin with  alphabet or _ They are case sensitive i.e Var_name is different from var_nameSome valid identifiers arejohnValuer_siex1x2ChennaiSum1abcdinvalid identifiers123%(super)25th1stab-dsum of totalOperatorsoperators are further classified into different categories likeArithmetic operators +  - for addition   1+3 = 4-   for subt…