Saturday 31 October 2015

Simple C programs for Easy Understanding


                                                C PROGRAM

#include<stdio.h>
#include<conio.h>
void main ()
{
int  a,b,c;
clrscr();
a=10;
b=20;
c=a+b;
printf("Sum:%d",c);
getch();
}
Output:
30

#include<stdio.h>
#include<conio.h>
void main ()
{
int  a,b,c;
clrscr();
printf("Enter the a&b");
scanf("%d%d",&a,&b);
c=a+b;
printf("Sum:%d",c);
getch();
}
Output:
10
20
Sum:30






UNIT-II
2.1 Branching
Simple if statement       
#include<stdio.h>
#include<conio.h>
void main ()
{
int a,b;
clrscr();
a=20;
b=10;
if(a>b)
{
printf("a is big");
}
getch();
}
OUTPUT:
  a is big
if…  else statement
#include<stdio.h>
#include<conio.h>
void main ()
{
int a,b;
a=110;
b=210;
if(a>b)
{
printf("A is big");
}
else
{
printf("B is big");
}
getch();
}
Output:
    B is big

else if ladder:
#include<stdio.h>
#include<conio.h>
void main ()
{
int a,b,c;
a=10;
b=20;
c=30;
if(a>b && b>c)
{
printf("a is big");
}
else if (b>c)
{
printf("b is big");
}
else
{
printf("c is big");
}
getch();
}
*/
/*
#include<stdio.h>
#include<conio.h>
void main ()
{
int a,b,c;
clrscr();
a=10;
b=20;
c=30;
if(a>b)
{
if(a>c)
printf("a is greater\n");
else
printf("c is greater\n");
}
else
{
if (b>c)
printf("b is greater\n");
else
printf("c is greater\n");
}



                      2.2  looping statements
                          While statement:
#include<stdio.h>
#include<conio.h>
void main ()
{
int i;
clrscr();
i=0;
while(i<5)
{
printf("welcome to c");
i++;
}
getch();
}
Output:
 welcome to c
welcome to c
 welcome to c
 welcome to c
welcome to c

do- while statement:
#include<stdio.h>
#include<conio.h>
void main ()
{
int i;
clrscr();
i=0;
do
{
printf("welcome to c");
i++;
}while(i<5);
getch();
}

Output:
 welcome to c
welcome to c
 welcome to c
 welcome to c
welcome to c

for statement
#include<stdio.h>
#include<conio.h>
void main ()
{
int i;
clrscr();
for(i=0;i<5;i++)
{
printf("welcome to c");
}
getch();
}
Output:
 welcome to c
welcome to c
 welcome to c
 welcome to c
welcome to c
Nested for statement:
#include<stdio.h>
#include<conio.h>
void main ()
{
int i,j;
clrscr();
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("welcome to c");
}
}
getch();
}
Output:
 welcome to c
welcome to c
 welcome to c
 welcome to c
1-D  Array:
#include<stdio.h>
#include<conio.h>
void main ()
{
int a[10],i;
clrscr();
printf("ENTER THE ARRAY VALUE");
for(i=0;i<5;i++)
{
scanf("%d”,&a[i]);
}
printf("LIST THE ARRAY VALUE");
for(i=0;i<5;i++)
{
printf("%d",a[i]);
}
getch();
}




Output:
ENTER THE ARRAY VALUE
10
20
30
40
50
LIST THE ARRAY VALUE
10
20
30
40
50

2-D  Array:
#include<stdio.h>
#include<conio.h>
void main ()
{
int a[10],i;
clrscr();
printf("ENTER THE  2-D ARRAY VALUE");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d,&a[i][j]);
}
}
printf("LIST THE  2-D  ARRAY VALUE");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("\t%d",a[i][j]);
}
printf(“\n”);
}
getch();
}
OUTPUT:
ENTER THE  2-D ARRAY VALUE
1
2
3
4
 THE  2-D  ARRAY VALUE

1            2
3      4

#include<stdio.h>
#include<conio.h>
void main ()
{
char c;
clrscr();
c='A';
printf("%c",c);
getch();
}

OUTPUT:
A

#include<stdio.h>
#include<conio.h>
void main ()
{
char c;
clrscr();
printf("Enter the char");
scanf("%c",&c);
printf("%c",c);
getch();
}
OUTPUT
M
M

2.4 STRINGS
Reading and Writing String


#include<stdio.h>
#include<conio.h>
void main ()
{
char name[10];
clrscr();
printf("Enter the name");
scanf("%s",name);
printf("%s",name);
getch();
}
OUTPUT:
Enter  the name
Boopathi  kumar

Boopathi 


#include<stdio.h>
#include<conio.h>
void main ()
{
char name[10];
clrscr();
printf("Enter the name");
gets(name);
puts(name);
getch();
}
OUTPUT:
Enter  the name
Boopathi kumar

Boopathi  kumar


  String  handling function

strlen( )  function:

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main ()
{
int n;
char name[10];
scanf("%s",name);
n=strlen(name);
printf("String length=%d",n);
}
Output
Boopathi
String length :8

strcpy() function:

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main ()
{
char name1[50],name2[50];
scanf("%s%s",name1,name2);
strcpy(name1,name2);
printf("%s%s",name1,name2);
}
Input:
 kumar
Boopathi
Output:
Boopathi  Boopathi





strcat() function:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main ()
{
char name1[50],name2[50];
scanf("%s%s",name1,name2);
strcat(name1,name2);
printf("%s%s",name1,name2);
}
Input:
 Boopathi
Kumar
Output:
Boopathi kumar   kumar

strcmp() function:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
void main ()
{
char *name1,*name2;
int n;
clrscr();
scanf("%s%s",name1,name2);
n=strcmp(name1,name2);
switch(n)
{
case  0:
printf(“two string are same”);
break;
case 1:
print(“First string is greater than are second”);
break;
case -1:
print(“second string is greater than the first”);
break;
}
getch();
}
Input
 LION
 TIGER
OUTPUT:
Second string is greater than the first


                 //,.,.,.,.graphics.,.,.,.//
/*
#include<graphics.h>
#include<conio.h>
void main()
{
int gdriver=DETECT,gmode;
initgraph(&gdriver,& gmode,"");
rectangle(100,100,200,200);
getch();
cleardevice ();
closegraph();
}
*/
/*
#include<graphics.h>
#include<conio.h>
void main()
{
int gdriver=DETECT,gmode;
initgraph(&gdriver,& gmode,"");
arc(100,100,0,135,50);
getch();
cleardevice ();
closegraph();
}
*/
/*
#include<graphics.h>
#include<conio.h>
void main()
{
int gdriver=DETECT,gmode;
initgraph(&gdriver,& gmode,"");
ellipse(100,100,0,360,50,25);
getch();
cleardevice ();
closegraph();
}
*/
/*
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main ()
{
int a=10;
clrscr();
printf("%d",isdigit(a));
getch();
}
*/
/*
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main ()
{
char a='a';
clrscr();
printf("%d",isdigit(a));
getch();
}
*/
/*
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main ()
{
char a='A';
clrscr();
printf("%d",isdigit(a));
getch();
}
*/
/*
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main ()
{
printf("%d",isupper('A'));
getch();
}
*/
/*
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main ()
{
printf("%d",isalpha('e'));
getch();
}
*/
/*
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main ()
{
printf("%d",isalpha('i'));
getch();
}
*/
/*
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main ()
{
printf("%d",islower('A'));
getch();
}
*/
/*
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main ()
{
printf("%d",tolower('A'));
getch();
}
*/
/*
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main ()
{
printf("%d",toupper('A'));
getch();
}


#include<stdio.h>
#include<conio.h>
void main ()
{
void add();
clrscr();
add();
getch();
}
void add()
{
int a,b,c;
a=10;
b=20;
c=a+b;
printf("%d",c);
}

#include<stdio.h>
#include<conio.h>
void main()
{
int i,*p;
int a[10];
clrscr();
printf("Enter 10 numbers\n");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
p=a;
printf("INDEX VALUES ADDRESS\n");
for(i=0;i<10;i++)
{
printf("X[%d]\t%d\t%u\n",i,*p,p);
p++;
}
getch();
}
*/