Saturday 31 August 2013

C Programming for beginners, nested for loop

Nested for loop
Ex;
for(i=0;i<10;i++)
{
   for(j=0;j<10;j++)
   {
       //Code...
   }
}

//write a program to print multiplication table 10X10
#include<conio.h>
#include<stdio.h>
void main()
{
   int i,j,n;
   clrscr();
   printf("\n 10 X 10 Multiplication  Table \n");
   for(i=1;i<=10;i++)
   {
       for(j=1;j<=10;j++)
       {
           printf("%d X %d = %d",j,i,(j*i));
       }
       printf("\n");
   }
   getch();
}
O/P:
Check yourself.......

Thursday 29 August 2013

C Programming beginners, for loop

Syntax : for(initalization;termination condition; increament)
FOR loop statement is defined by 3 parameters
1) Initialization of variable, which says that from which value loop will start
2) Termination condition, at what point loop should stop
3) Increament of  variable,

for(i=2;i<=5;i++)
{
//Some code ....
}
above loop will run four times
loop start at i=2 and every time it increase value of "i" by 1
when "i" values becomes 6, lop will stop.

//Write a program to print 1 to N number, where 0<N<=100
#include<conio.h>
#include<stdio.h>
void main()
{
   int n,i;
   clrscr();
   printf("Enter Value for N(0 - 100):");
   scanf("%d",&n);
   if(n>0 && n<=100)
   {
      for(i=1;i<=n;i++)
      {
         printf("%d ",i);
      }
   }
   else
   {
     printf("Invalid input.\n Enter Value from 1 to 100\n");
   }
   getch();
}

O/P:
Enter Value for N(0 - 100):105
Invalid input.\n Enter Value from 1 to 100

Enter Value for N(0 - 100):15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

-----------------------------------------------------------------
//Find odd/even number from 0 - 100 and count frequency for the same
#include<conio.h>
#include<stdio.h>
void main()
{
   int n=100,i,Total_Even=0,Total_Odd=0;
   clrscr();
   //Find Even Numbers
   printf("Even Numbers:");
   for(i=0;i<=100;i++)
   {
      if(i%2==0)
      {
         printf("%d ",i);
         Total_Even+=1;
      }
   }
   //Find Odd Numbers
   printf("\n\nOdd Numbers:");
   for(i=0;i<=100;i++)
   {
      if(i%2!=0)
      {
         printf("%d ",i);
         Total_Odd+=1;
      }
   }
   prinft("\n\nTotal Even No:%d\n Total Odd No:%d",Total_Even,Total_Odd);
   getch();
}

Tuesday 27 August 2013

C Programming beginners, if...else programe

//SIMPLE IF..ELSE PROG. to FIND NUMBER IS ODD or EVEN NUMBER
#include<conio.h>
#include<stdio.h>
void main()
{
int n;
clrscr();
printf("Enter Number:");
scanf("%d",&n);
if(n%2==0)
{
printf("%d is EVEN Number.",n);
}
else
{
printf("%d is ODD number",n);
}
getch();
}
O/P:
Enter Number: 10
10 is EVEN Number

Enter Number: 13
13 is ODD number


-------------------------------------------------------------------------
//SIMPLE IF..ELSE PROG. to FIND NUMBER IS ZERO/POSITIVE/NEGATIVE
#include<conio.h>
#include<stdio.h>
void main()
{
int n;
clrscr();
printf("Enter Number:");
scanf("%d",&n);
if(n==0)
{
printf("You have entered ZERO");
}
else if(n>0)
{
printf("%d is POSITIVE number",n);
}
else
{
printf("%d is NEGATIVE number",n);
}
getch();
}

O/P:
Enter Number: 0
You have entered ZERO

Enter Number: 12
12 is POSITIVE number

Enter Number: -25
-25 is NEGATIVE number


-------------------------------------------------------------------------
//SIMPLE IF..ELSE PROG. to FIND NUMBER RANGE

#include<conio.h>
#include<stdio.h>
void main()
{
int n;
clrscr();
printf("Enter POSITIVE NUmber:");
scanf("%d",&n);
if(n>=0)
{
if(n>=0 && n<=10)
{
printf("%d is in between 0 to 10",n);
}
else if(n>=11 && n<=20)
{
printf("%d is in between 11 to 20",n);
}
else if(n>=21 && n<=50)
{
printf("%d is in between 21 to 50",n);
}

else if(n>50 && n<=100)
{
printf("%d is in between 51 to 100",n);
}
else
{
printf("%d is grater than 100",n);
}
}
else
{
printf("%d is NEGATIVE number",n);
}
getch();
}

O/P:
Enter Number: 25
25 is in between 21 to 50

Enter Number: 7
7 is in between 0 to 10

Enter Number: -9
-9 is NEGATIVE NUMBER

Enter Number: 15
15 is in between 11 to 20

Enter Number: 85
85 is in between 51 to 100

Enter Number: 105
105 is greater than 100

-------------------------------------------------------------------------
//SIMPLE IF..ELSE PROG. to FIND Minimum & Maximum Number
#include<conio.h>
#include<stdio.h>
void main()
{
int n,m,Calc;
clrscr();
printf("Enter Value for N:");
scanf("%d",&n);
printf("Enter Value for M:");
scanf("%d",&m);

if(n==m)
{
printf("N & M both are equal\n");
}
else
{
 if(m>n)
{
printf("M:%d is Greater Than N:%d\n",m,n);
}
else
{
printf("N:%d is Greater Than M:%d\n",n,m);
}
}

printf("N+M = %d",(n+m));
printf("N-M = %d",(n-m));
printf("N*M = %d",(n+m));
printf("N/M = %f",((float)n/(float)m));
getch();
}

O/P:
check yourself

Monday 26 August 2013

C Programming for beginners, operators used in C

Operators used in C



x=0; y=10; z=20;

Operator Usage Example Result
Arithmatic Operators
Addition of variables x = y + z  x=30
 - Subtraction of variables
alter variables +/- sign
x = y-z
x = -y 
x=-10
x= -20
 * Multiplication of variables  x = y*z  x=200
 /  Division of variables x = y/z where z>0 x=0 (integer)
x=0.500000(float)
 %  Modulus Operator  x = y%z x=10
 ++  Increment Operator
increase variable value by 1
 x++ x=1
 -- Decrement Operator
decrease variable value by 1
 x-- x=-1
Comparison/Relational Operators
 == Logical Equals to x==y FALSE or 0
 != Not Equal to x!=y TRUE or 1
  > Greater Than z>y
x>y
TRUE or 1
FALSE or 0
 < Less Than z<y
x<y
FALSE or 0
TRUE or 1
 >= Greater Than or Equals to z=10
z>=y
x>=y

TRUE or 1
FALSE or 0
 <= Less Than or Equals to y=0
z<=y
x<=y

FALSE or 0
TRUE or 1
Logical Operators
 ! Logical NOT
variable value zero than it returns 1
else returns 0(Zero)
!x
!y
1
0
 && Logical AND
returns 1 when both the values are same
else returns 0(Zero)
0 && 0
0 && 1
1 && 0
1 && 1
0
0
0
1
 || Logical OR
returns 0(Zero) when both the values are 0(Zero)
else returns 1
0 && 0
0 && 1
1 && 0
1 && 1
0
1
1
1
Bitwise Operators
 ~ Bitwise NOT x becomes binary 0000, so ~x  in bitwise 1111...

y=8 becomes binary 1000, so ~y becomes ...1110111
-1


-9
 & Bitwise AND x=5, y=6
z=x&y
x-0101
y-0110
-----------
z-0100 




4
 | Bitwise OR x=5, y=6
z=x&y
x-0101
y-0110
-----------
z- 0111




7
 

Friday 23 August 2013

C Programming beginners, Read Value from user in string

Reading value in string from user and display it in format

1)Read Single Character
#include<conio.h>
#include<stdio.h>
void main()
{
char str1;
clrscr();
printf("Enter Your Name:");
scanf("%c",&str1);
printf("\n\nYou have entered:%c",str1);
getch();
}
O/P:


2) Read Single Word
#include<conio.h>
#include<stdio.h>
void main()
{
char str1;
clrscr();
printf("Enter Your Name:");
scanf("%s",str1);//Read single word
printf("\n\nYou have entered:%c, ",&str1);//It will give some random Char
printf("\n\nYou have entered:%s",str1);//This will display Single Word
getch();
}
O/P:



3)Read Sentence
#include<conio.h>
#include<stdio.h>
void main()
{
char str1;//Store 2 byte data
clrscr();
printf("Enter Your Name:");
scanf("%[^\n]",str1);//Read single word
printf("\n\nYou have entered:%s",str1);
getch();
}
O/P:
This will read only 1 byte data. and more than 1 byte will result into garbage.


4)To overcome problem of above program string pointer is used.
in this program instead of char str1 we'll use pointer to read complete string
and it is declared  as char *str1, now it hold no. of bytes. and to read complete sentence we'll use
%[^\n].

#include<conio.h>
#include<stdio.h>
void main()
{
char *str1;
clrscr();
printf("Enter Your Name:");
scanf("%[^\n]",str1);//Read single word
printf("\n\nYou have entered:%s",str1);
getch();
}
O/P:

Thursday 22 August 2013

C Programming beginners, Read Value from user

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
float x,y;
clrscr();

printf("Enter Value for A:");
scanf("%d",&a);// "&" points to address of variable where value will be stored
printf("Enter Value for B:");
scanf("%d",&b);
printf("Enter Value for C:");
scanf("%d",&c);
printf("You have entered...\nA:%d\nB:%d\nC:%d",a,b,c);

printf("\n\nEnter float value( value that contains decimal place, six digit after decimal point) ");
printf("\nEnter Value for X:");
scanf("%f",&x);
printf("Enter Value for Y:");
scanf("%f",&y);
printf("\nYou have entered...\nX:%d\nY:%d",x,y);
printf("\nYou have entered...\nX:%f\nY:%f",x,y);
printf("\none decimal place\nX:%.1f\nY:%.1f",x,y);
printf("\ntwo decimal places\nX:%.2f\nY:%.2f",x,y);


getch();
}

O/P:

C Programming beginners - Introduction, Variable declaration and simple iteration

/*
 Global variables can be accesses any where in the program;
Local variables accessed within the scope defined by curly braces.
*/
#include<stdio.h>
#include<conio.h>
//As user defined function declared after main(), its prototype must be declared before main
void DoSum(int,int);
int Total;
void main()
{
int ValA,ValB;
clrscr();
ValA=23;
ValB=15;
printf("ValA:%d\nValB;%d\n",ValA,ValB);
Total=ValA-ValB;
printf("Before DoSum(), Value of Total=ValA-ValB=%d\n",Total);
//Calling of Function, function will add value of ValA,ValB
// and stores it in Total
DoSum(ValA,ValB);
printf("After DoSum(), Value of Total:ValA+ValB=%d",Total);
getch();
}
//DoSum() is user defined function
void DoSum(int A,int B)
{
//As Sum is Global Variable it is accesible here
//And it will holds addition of value
Total = A+B;
}

/**************************************************************/
#include<stdio.h>
#include<conio.h>

//Global Variable declaration
int Total;

//DoSum() is user defined function
//As function definition written before main(), no need to declare function prototype before main
void DoSum(int A,int B)
{
//As Sum is Global Variable it is accesible here
//And it will holds addition of value
Total = A+B;
}
void main()
{
int ValA,ValB;
clrscr();
ValA=23;
ValB=15;
printf("ValA:%d\nValB;%d\n",ValA,ValB);
Total=ValA-ValB;
printf("Before DoSum(), Value of Total=ValA-ValB=%d\n",Total);
//Calling of Function, function will add value of ValA,ValB
// and stores it in Total
DoSum(ValA,ValB);
printf("After DoSum(), Value of Total:ValA+ValB=%d",Total);
getch();
}

Monday 12 August 2013

C Programming beginners, Local Variable declaration

getch() is the function which scans single key stroke.
so it'll be used to check output at the end of execution of program.

conio.h : Console input output
stdio.h  : Standar input output

printf() is declared in stdio.h file while getch() is declared in conio.h file.

//Use of getch()
#include<conio.h>
#include<stdio.h>
void main()
{
   printf("Hello World...!!!");
   getch();
}



//Simple variable declaration

#include<conio.h>
#include<stdio.h>
void main()
{
   int a;
   clrscr();// clear screen - clears previous screen out put

   a=10;
   printf("Value of a:%d",a);
   getch();
}

output :
Value of a:10

//Multiple variable declaration

#include<conio.h>
#include<stdio.h>
void main()
{
   int a,b;
   clrscr();// clear screen - clears previous screen out put

   a=10;
   b=22;
   printf("Value of a:%d",a);
   printf("Value of b:%d",b);
   printf("\n");//\n to print next statement in new line
   printf("Value of a:%d and b:%d\n",a,b);
   printf("Value of a:%d and b:%d\n",b,a);
   getch();
}

output :
Value of a:10Value of b:22
Value of a:10 and b:22
Value of a:22 and b:10



Little Introduction for Beginners of C Programing

And E-Book is available on below link.

E-Book of Ansci C Balaguruswamy : Download E-Book (External Source)

In C Program, we include header files in which external/predefined functions declared.
These functions will be used in our programs, so related header file will be included.

->In C Program, double slash "//" is used to comment a single line.
   And /*..*/ is used to comment multiple lines.
->Commented line will not be compiled during compilation.
    It is good practice to write comments in program. Comments will help to understand the program.

->Every line must end with semi-colon ";"

->main() is the function denotes to compiler starting of program for compilation.


General format of C Program

//Header or Title or Definition of program

#include<...h> //Header File(s)

globle varible declaration

main()
{ //Program Starts
     Loca Variable declaration;
//     Executable part of program
     Input/Output Operation

}//end of program

user defined functions()
{
}

Important shortcuts for Turbo C Compiler
To Compile Program : ALT+F9
To Run Program : CTRL+F9
To View output/previous screen : ALT+F5

Open Turbo C and write following program.

First C Program
//Simple program to display "Hello World"

#include<stdio.h>
void main()
{
     printf("Hellow World...!!!");
}

check for errors by ALT+F9
if no errors hit CTRL+F9 to run program
and to check output hit ALT+F5


Now you can write C programs.....

Sunday 11 August 2013

Download Turbo C 3.0

To Write and Run you must first download Borland Turbo C Compiler.

Following are the links to download the "Borland Turbo C 3.0"

Download Turbo C 3.0

Unzip it and install it on C Drive.
following are the steps to install Turbo C









To Run Turbo C on Windows 7 and above version You need to install DOSBOX on your system

Download DOSBOX

Install DosBox and open it.

To run TC.EXe it is required to mount  "C:\TC" on "C" and the it'll works.


Contact Form

Name

Email *

Message *