Skip to main content

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 month Input : the number of units – variable name – unit Output – Amount of rupee – variable name – amount Logic: Units Rupees 1-50 units 0.75/unit 51-100 0.85/unit 101-200 1.50/unit 201-300 2.20/unit >300 3.00/unit In 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)

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 num Output : One number, Ex 4321, rev is the variable Logic: separate each digit using modulo operator from the right and bring that number to front divide the given number by 10, so the right most digit will be discarded. run till the number becomes <=0 output 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 – else if(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

Data types

Declaration of variable names does two things it tells the compiler what the variable name is specifies what type of data the variable will hold So 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 machine Range char 1 byte or 8 bits -128 to 127 signed char 1 byte -128 to 127 unsigned char 1 byte 0 to 255 short int or short 1 byte -128 to 127 int 2 bytes -32768 to 32767 unsigned int 2 bytes 0 to 65535 long int or long 4 bytes -2,147,483,648 to 2,147,483,647 unsigned long int 4 bytes 0 to 4,294,967,295 float 4 bytes 3

Tokens in C

Tokens are classified as Keywords Identifiers Operators Constants Special Symbols Keywords Keywords 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 keywords auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while   Identifiers Identifiers are variable names decl

printf() and scanf() Functions

Printf() is a function to print strings to the display unit and scanf() is to scan the input through the keyboard the f in the printf and scanf are called as format. That is, printf() and scanf() will print and scan in some particular format, it is the duty of the developer to provide that formats. For example printf(“a=%d, b=%d”, a,b); //This function will print a=10, b=20 if the values of a and b are 10 and 20 scanf(“%f”, &a); //this will accept a float number as the specifier is %f which is for float, See the Ampersand symbol which to tell the compiler to store the variable into the address. Here is the list of format codes or format specifiers scanf() format codes Code Meaning %c read a single character %d read a decimal integer %f read a floating point value %e read a floating point value (even in exponential format)

Sample C Program

/* This program is written by T S Pradeep Kumar on 28th Sep 2010 This is to display Hello World to the display unit */ #include <stdio.h> //including the standard IO functions like printf(), scanf() int main() { printf(“Hello World \n”); return 0; } stdio.h is the library which contains the printf(), scanf() and other standard IO functions, so before we use any function we need to include in our C program. Most of the compilers will take stdio.h automatically, even if we dont include in our program int main() {} is the main function printf(“Hello World\n”); printf() is the function which is printing to the display unit and it is displaying Hello World. Whatever is there inside the double quotes will displayed as it is in the monitor except the format specifiers or format codes (%d, %f, %c) return 0; is the final statement which returns the integer value 0 (this is just to make the compiler happy) Here in the above program, y

Introduction to C Programming

Features of C Programming C is a small language having lesser number of keywords than Java or pascal. C is a native language of Unix, Linux. Not only that many of the windows packages, database programs, graphics libraries are written in C program C is portable – it provides standard set of libraries that work on the same way with all machines C is modular, as it supports functions to divide the program in to sub program C is efficient on most machines, because certain constructs are machine dependant. Structure of a C program Comments the comments are usually ignored by the compiler so this part informs about the author of the program what is the usefulness of this program updation or creation of the program Example: //  - Single line comment /* ----- multi line comments    ….*/ Preprocessor Directives before the compiler runs, the preprocessing takes place. the preprocessors are prefix with a sy

Xgraph [dialog.o] error

If you are installing ns2 allinone package in fedora or ubuntu or any other linux, all the packages will be installed successfully. Occasionally the recent releases of linux flavors failed to install xgraph. Problem: The possible error you may get is [dialog.o] error, Solution: Open the file dialog.c which is available under the folder xgraph-12.1/ (this is in my case) Find out the function getline() (will be at two places), usually line number 780 and 891 or 889, replace getline() function to newgetline() (at the both the places) save the file and at the prompt give ./configure give the command make If there are no errors, xgraph will work now..