TOTAL PAGE VIEWS

hit counter

C/C++

Easy way to learn C language 


What is c  c is a programming language developed at AT&T’s Bell laboratories of USA in 1972. It was designed and written by a man named Dennis Ritchie.


Steps in learning c:
Alphabets                           constants           
Digits                     ->            variables->          Instructions->    program
Special Symbols                   
keywords
Constant: constant is a value or character or floating value which value can’t be changed.
ex:- 34, 56.6
Variable: variable is an memory location which value can be change at the run time or excecution time.
ex:- a=45, b=100,c=0
       c=a+b then c=145
C keywods Keywords are the words whose meaning has already been explained to the C compiler.
There are only 32 keywords available in C.
auto                      double                   int                           struct
break                    else                       long                        switch
case                      enum                   register                  typedef
char                      extern                  return                       union
const                    float                     short                      unsigned
continue               for                        signed                      void
default                 goto                      sizeof                      volatile
do                          if                           static                           while


The first simple C Program
1./* simple program */

 
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
printf(“welcome to first c program”);
getch();
}


Here above the some text enclosed within “/* */”It’s called comment. Comments are necessary, it is good practice to begin a program with a comment indicating the purpose of the program, its author and the date on which the program was written.any number of comments written at any place in the program.
#include<stdio.h>
#include<conio.h>
these are the header files. Some of the functions are predefined in this headerfiles
main():-main is a function. It is the main of the program. All the statements that belong to main() are enclosed within a pair of braces { }as shown below
 

main()
{
Statement 1;
statement 2;
statement 3;
}
printf:-printf is a predefined function in #include<stdio.h> headerfile. It works as prints the formatted text in output section.
scanf:-scanf is a predefined function in #include<stdio.h> header file. It works as read the values from output.
Compilation and Execute the program
start the run->command->c:\>
c:\>cd tc
c:\>tc
then write the program and save the program press F2
then after to compile the program press Alt+F9(it is shows the errors in program)
and for output press Ctrl + F9



2./* Write a program to perform sum of two numbers */
 

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
c=a+b;
printf(“the sum of the two numbers is: %d”,c);
getch();
}
 Data types:- A data type represents the type of the data. Which are meanly two types

Primary datatypes                          
    secondary data types                      
Integer datatype                                              Array
Real datatype                                                   Pointer
Character datatype                                         Structure
                                                                         Union
                                                                         Enum etc…
Integer data type: it stores all the numerical values, it represents as key word “int”.
ex:- int a=45;
        int b=62;
Real data type: it stores all the floating values or real values, it represents as keyword “float”.
ex:-float a=45.45;
       float b=53.32;


Character data type: it stores the charecters and strings, it represents as keyword “char”.
ex:-char ch=’a’;
      char ch[10]={‘language’};
3./* write a program to perform sum of three numbers*/
 

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,d;
clrscr() ;
printf(“enter any three values”);
scanf(“%d%d%d”,&a,&b,&c);
d=a+b+c;
printf(“the sum of three numbers is %d”,d);
getch();
}
& “Reference variable”:- it stores the address of the value and use to read the value from output.

4./* write a program to perform swaping of two numbers using third variable */
 

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf(“\n Enter any two numbers”);
scanf(“%d%d”,&a,&b);
printf(“\n before swapping”);
printf(“\n a=%d”,a);
printf(“\n b=%d”,b);
c=a;
a=b;
b=c;
printf(“\n after swapping”);
printf(“\n a=%d”,a);
printf(“\n b=%d”,b);
getch();
}
5./* w.a.p to perform swaping of two numbers with out using third variable */
 

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf(“\n Enter any two numbers”);
scanf(“%d%d”,&a,&b);
printf(“\n before swapping”);
printf(“\n a=%d”,a);
printf(“\n b=%d”,b);
a=a+b;
b=a-b;
c=a-b;
printf(“\n after swapping”);
printf(“\n a=%d”,a);
printf(“\n b=%d”,b);
getch();
}
6./* write a program to find simple intrest */ 
#include<stdio.h>
#include<conio.h>
void main()
{
int p,n;
float r,si;
printf(“Enter values of p,n,r”);
scanf(“%d%d%d”,&p,&n,&r);
si=p*n*r/100;
printf(“simple intrest is %f”,si);
getch();
}
Operators:- A operator is a symbol that performs a operation, these type of operators are 7 types they are given below:
1.Arthamatical operators {+,-,*,/,…}
2.Rational operators {<,<=,>,>=,==,!+}
3.Logical operators{&&,||,!!}
4.Assignment operators{=}
5.Increment and decrement operators{++,--}
6.Conditional operators{():?}
7.Special operators{,}
7/* write a program to find the Average of two numbers*/
 

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
float c;
printf(“\n enter any two numbers”);
scanf(“%d%d”,&a,&b);
c=(a+b)/2;
printf(“\n Average of two numbers is: %f”,c);
getch();
}
8./* write a program to find Area of circle */
 

#include<stdio.h>
#include<conio.h>
void main()
{
int r;
float c;
clrscr();
printf(“\n Enter the radius”);
scanf(“%d”,&r);
c=3.14*r*r;
printf(“The area of circle is: %f”,c);
getch();
}
Conditional statements :  Conditional statement is a statement which is used to check the condition and whether the condition is true then execute the condition other wise cursor come out of next statement, these are given below
i, simple if statement
ii, if else statement
iii, else if statement
iv, nested if statement

i, if statement:-                if the condition whatever it is, is true, then the statement is executed, instead the program skips past it.
ex:-9. /* simple if statement */
 

#include<stdio.h>
#include<conio.h>
void main()
{
int value;
clrscr();
printf(“\n enter any number above the hundred”);
scanf(“%d”,&value);
if(value>100)
printf(“value is %d”,value);
getch();
}
10./*find the biggest of two numbers*/
 

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf(“enter any two numbers”);
scanf(“%d%d”,&a,&b);
if(a>b)
printf(“\n a is the biggest number %d”,a);
if(a<b)
printf(“\n b is the biggest number %d”,b);
getch();
}
ii, if-else statement:- which is used to check the condition whether the condition is true then true statement or if part is executed otherwise false statement or else part will be executed.

syntax:- main()
                {
                if(condition)
                true statement;
                else
                false statement;
                }


ex:-11,/* write a program to find biggest of two numbers  using if-else statement */
 

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf(“\n enter any two number”);
scanf(“%d%d”,&a,&b);
if(a>b)
printf(“\n a is the biggest number”);
else
printf(\n b is the biggest number”);
getch();
}
iii,Nested if-else statement:- which construct within either the body of the if statement or the body of an else statement. This is called ‘nesting’ of ifs. 

ex:-12,/*write a program to find biggest of three numbers*/
 

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf(“\n Enter any three numbers”);
scanf(“%d%d%d”,&a,&b,&c);
if((a>b)&&(a>c))
printf(“\n a is the biggest number %d”,a);
else
{
if(b>c)
printf(“\n b is the biggest number %d”,b);
else
printf(“\n c is the biggest number %d”,c);
}
getch();
}
iv,else if ladder:- This involves usage of else if blocks, it can use to reduces the indentation of the statements. In this case every else is associated with its previous if. The last else goes to work only if all the conditions fail. Even in else if ladder the last else is optional.

ex:13,/*else if ladder simple program*/
 

#include<stdio.h>
main()
{
int s1,s2,s3,s4,s5,per;
per=(s1+s2+s3+s4+s5)/per;
if(per>=60)
printf(“First division”);
else if(per>=50)
printf(“Second division”);
else if(per>40)
printf(“Third division”);
else
printf(“fail”);
getch();
}
*Logical operators:-  These are meanly three types, They are:
&&         -And operator
||           -Or operator
!!             -Not operator
The && and || operators allow two or more conditions to be combined in an if statement.


 ex:14,/*finding division obtained by the student with out using logical operator */
 

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

 main()
{
int s1,s2,s3,s4,s5,per;
printf(“Enter marks in five subjects”);
scanf(“%d%d%d%d%d”,&s1,&s2,&s3,&s4,&s5);
per=(s1+s2+s3+s4+s5)/5;
if(per>=60)
printf(“First division”);
else
{
if(per>=50)
printf(“Second division”);
else
{
if(per>=40)
printf(“Third division”);
else
printf(“Fail”);
}
}getch();
}
ex:15,/*finding division obtained by the student with using logical operator */
 
#include<stdio.h>
#include<conio.h>

 main()
{
int s1,s2,s3,s4,s5,per;
printf(“Enter marks in five subjects”);
scanf(“%d%d%d%d%d”,&s1,&s2,&s3,&s4,&s5);
per=(s1+s2+s3+s4+s5)/5;
if(per>=60)
printf(“First division”);
if((per>=50)&&(per<60))
printf(“Second division”);
if((per>=40)&&(per<50))
printf(“Third division”);
if(per<40)
printf(“Fail”);
getch();
}
Loop:- The computer performs a set of instructions repeatedly.
Looping statements:- A statement which is executing same instruction number of times until the given condition is failure. These type of statements are meanly three types, They are:
i,while statement
ii, do-while statement
iii,for statement
*while statement:- this statement first check the condition, if the condition is true execute the  block of the statement other wise control comes out of the loop.


ex:16,/*write a program to print 1 to 50 numbers*/
 

#include<stdio.h>
#include<conio.h>
void main()
{
int n=1;
clrscr();
printf(“1 to 50 numbers printed below”);
while(n<=50)
{
printf(“\n%d”,n);
n=n+1;
}
getch();
}
Do-while statement:- this statement execute the statement exactly once , then check the condition if the condition is true control continues the program otherwise control comeout of the loop.

ex:17,/* write a program to print 1 to 50 using do-while loop*/
 
#include<stdio.h>
#include<conio.h>

 main()
{
int n=1;
clrscr();
do
{
printf(“\n%d”,n);
n=n+1;
}
while(n<=50);
getch();
}
For loop:- this statement can be use to reduce the program length, it check the condition, initialization and incrementing or decrement process can be done by single for condition.

ex:18,/*write a program to print 1 to 50 using for loop*/
 
#include<stdio.h>
#include<conio.h>

 main()
{
int n;
clrscr();
for(n=1;n<=50;n++)
printf(“\n%d”,n);
getch();
}
Switch statement:
the control statement that allows us to make a decision from the number of choices is called switch.


ex:19,/*simple switch program*/
 
#include<stdio.h>
#include<conio.h>

 main()
{
int i=2;
switch(i)
{
case 1:
printf(“I am in case 1:”);
case 2:
printf(“I am in case 2:”);
case 3:
printf(“I am in case 3:”);
default:
printf(“I am in default case :”);
}
getch();
}
Ex:20,/*w.a.p to find given character is vowel or not using switch statement*/
 

#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
printf(“Enter any character”);
scanf(“%c”,&ch);
switch(ch)
{
case ‘a’:
case ‘A’:
                printf(“vowel”);
case ‘e’:
case ‘E’:
                printf(“vowel”);
case ‘i’:
case ‘I’:
                printf(“vowel”);
case ‘o’:
case ‘O’:
                printf(“vowel”);
case ‘u’:
case ‘U’:
                printf(“vowel”);
default:
                printf(“consonet”);
}
getch();
}
Ex:21,/* write a program to perform mathematical operation between two numbers*/

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

 main()
{
int a,b,oper,temp;
printf(“Enter any two numbers”);
scanf(“%d%d”,&a,&b);
printf(“1.Addition”);
printf(“2.Subtraction”);
printf(“3.Multiply”);
printf(“4.Devide”);
printf(“which operation do you want 1,2,3 or 4”);
scanf(“%d”,&oper);
switch(oper)
{
case 1:
                temp=a+b;
                printf(“addition is %d”,temp);
                break:
case 2:
                temp=a-b;
                printf(“subtraction is %d”,temp);
                break;
case 3:
                temp=a*b;
                printf(“multiply is %d”,temp);
                break;
case 4:
                temp=a/b;
                printf(“divide is %d”,temp);
                break;
default:
                printf(“Invalid choice”):
}
getch();
}
Break statement:- we often come across situations where we want to jump out of a loop instantly, with out waiting to get back to the conditional test the keyword break allows us to do this. When break is encountered inside any loop, control automatically passes to the first statement after the loop.
Some of the example programs:
22./* write a program to print 1 to 50 even numbers */


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

 main()
{
int n=1;
clrscr();
while(n<=50)
{
if(n%2==0)
printf(“\n%d”,n);
n=n+1;
}
getch();
}


23./write a program to print 1  to 50 odd numbers using do-while loop/ 

#include<stdio.h>
#include<conio.h>
 
main()
{
int n=1;
clrscr();
do
{
if(n%2==1)
printf(“\n%d”,n);
n++;
}
while(n<=50);
getch();
}
24./write a program to print multiplication table/
#include<stdio.h>
#include<conio.h>

 main()
{
int n,s,i=0
clrscr();
printf(“\n Enter any number”);
scanf(“%d”,&n);
while(i<=10)
{
s=n*i;
printf(“%d X %d = %d”,n,i,s);
i=i+1;
}
getch();
}
25./write a program to find reverse of a given number/

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

main()
{
int n,r,rev=0;
clrscr();
printf(“enter any three numbers”);
scanf(“%d”,n);
while(n!=0)
{
r=n%10;
rev=rev*10+r;
n=n/10;
}
printf(“\n reverse of a given number is %d”,rev);
getch();
}
26./write a program to find given number is positive or negative or zero/
#include<stdio.h>
#include<conio.h>
main()
{
int n;
printf(“enter any number”);
scanf(“%d”,&n);
if(n>0)
printf(“given number is positive number”);
if(n<0)
printf(“given number is negative number”);
if(n==0)
printf(“given number is zero”);
getch();
}

Twitter Delicious Facebook Digg Stumbleupon Favorites More