Sunday 6 April 2014

Basic C Programs

Program to find weather given number has square root or not
#include<stdio.h>
main()
{
int n,temp,f;
printf("enter any number\n");
scanf("%d",&n);
temp=n;
if((temp%2)==0)
{
printf("square root is possible\n");
f=check(n);
}
else
printf("square root is not possible\n");
}
check(int x)
{
int count=0;
while((x/2)==0)
count++;
if((count%2)==0)
printf("power of 2 possible\n");
else
printf("power of 2 not possible\n");
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc recurse.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter any number
34
square root is possible
power of 2 possible
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter any number
36
square root is possible
power of 2 possible
Program to evaluate an expression
#include<stdio.h>
#include<stdlib.h>
operations()
{
int a;
printf("Program for various operations\n");
printf("enter ur choice 1:tabs\n 2:manipulate\n 3:forloop\n");
scanf("%d",&a);
if(a==1)
{
printf("performing tabs operation\n");
tabs();
}
else if(a==2)
{
printf("performing manipulations..\n");
manipulate();
}
else if(a==3)
{
printf("performing forloop operation...\n");
forloop();
}
}
tabs()
{
printf("for back Space\b");
printf("for tab space\t");
printf("for coming to next line\n");
}
manipulate()
{
int a,b,result,n;
printf("read the values for a and b\n");
scanf("%d%d",&a,&b);
printf("enter ur choice N\n");
scanf("%d",&n);

switch(n)
{
case 1:
printf("addition operation\n");
result=a+b;
break;

case 2:
printf("substraction operation\n");
if(a>=b)
result=a-b;
else
result=b-a;
break;

case 3:
printf("multiplication operation\n");
result=a*b;
break;

case 4:
printf("division operation\n");
result=(a/b);
break;

default:printf("enter correct choice\n");
}
printf("result is %d\n",result);
}
forloop()
{
 int i,j,n;
char ch='a';
 printf("enter value for N\n");
   scanf("%d",&n);
  for(i=0;i<n;i++)
  {
   for(j=0;j<=i;j++)
   printf("%c ",ch++);
   printf("\n");
  }
}

main()
{
int choice;
operations();
}
OutPut:
Program for various operations
enter ur choice 1:tabs
 2:manipulate
 3:forloop
3
performing forloop operation...
enter value for N
3
a 
b c 
d e f 
madan@madan-Lenovo-G570:~/madan$ ./a.out
Program for various operations
enter ur choice 1:tabs
 2:manipulate
 3:forloop
2
performing manipulations..
read the values for a and b
12
35
enter ur choice N
4
division operation
result is 0
program to check various conditions
#include<stdio.h>
#include<stdlib.h>
main()
{
int choice;
printf("program to check various conditions:1:prime\n 2:evenodd\n 3:swapping\n 4:sign_check\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("perform prime number series\n");
prime();
break;
case 2:
printf("to check even or odd\n");
evenodd();
break;
case 3:printf("swapping operation...\n");
swapping();
break;
case 4: printf("to check sign..\n");
sign();
break;
default:printf("enter correct choice\n");
}
printf("your task successfully completed\n");
}
prime()
{
int n,i;
printf("enter value for n:\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
if(i%2!=0)
{
printf("%d\n",i);
}
}
}
evenodd()
{
int n;
printf("enter value for n\n");
scanf("%d",&n);
if(n%2==0)
{
printf("even number\n");
}
else
{
printf("odd number\n");
}
}
swapping()
{
int a,b,c;
printf("enter values for a and b\n");
scanf("%d%d",&a,&b);
c=a;
a=b;
b=c;
printf("after swapping a and b values are %d\t%d\n",a,b);
}
sign()
{
int n;
printf("enter value for n");
scanf("%d",&n);
if(n>0)
printf("positive number\n");
else
printf("negative number\n");
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ ./a.out
program to check various conditions:1:prime
 2:evenodd
 3:swapping
 4:sign_check
2
to check even or odd
enter value for n
45
odd number
your task successfully completed
madan@madan-Lenovo-G570:~/madan$ ./a.out
program to check various conditions:1:prime
 2:evenodd
 3:swapping
 4:sign_check
3
swapping operation...
enter values for a and b
54
675
after swapping a and b values are 675 54
your task successfully completed
madan@madan-Lenovo-G570:~/madan$ ./a.out
program to check various conditions:1:prime
 2:evenodd
 3:swapping
 4:sign_check
4
to check sign..
enter value for n-567
negative number
your task successfully completed
Program to examine int,float data types
#include<stdio.h>
#include<stdlib.h>
main()
{
int a=5,b=2,c;
float d=1.5,e=2.5,f;
c=(a+e);
f=(b+d);
printf("c=%d\n",c);
printf("f=%f\n",f);
}
OutPut:
c=7
f=3.500000
Program to understand data types
#include<stdio.h>
#include<stdlib.h>
datatypes()
{
int choice;
printf("to understand data types\n");
printf("1:integer\n 2:floating\n 3:chartype\n");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("integer operation\n");
       integer();
       break;
case 2:printf("floating point operation\n");
       floating();
       break;
case 3:printf("character type operation\n");
chartype();
break;
default:printf("enter correct choice...\n");
printf("we are in main function\n");
}
}
integer()
{
 int a,b,c,area,volume;
printf("enter values for a,b and c\n");
scanf("%d%d%d",&a,&b,&c);
area=2*(a*b+b*c+c*a);
volume=a*b*c;
printf("area of rectangle=%d\n",area);
printf("volume of rectangle=%d\n",volume);
}
floating()
{
float a,b,c;
printf("enter values for a and b\n");
scanf("%f%f",&a,&b);
c=a+b;
printf("result(sum) of c=%f\n",c);
}
chartype()
{
char ch;
ch=getchar();
putchar(ch='a');
printf("the entered character is %c",ch);
}
main()
{
datatypes();
}
OutPut:
to understand data types
1:integer
 2:floating
 3:chartype
1
integer operation
enter values for a,b and c
1 
2
3
area of rectangle=22
volume of rectangle=6
madan@madan-Lenovo-G570:~/madan$ ./a.out
to understand data types
1:integer
 2:floating
 3:chartype
2
floating point operation
enter values for a and b
12.33
23.44
result(sum) of c=35.770000
madan@madan-Lenovo-G570:~/madan$ ./a.out
to understand data types
1:integer
 2:floating
 3:chartype
3
character type operation
athe entered character is a
Program to print sizes of various data types
#include<stdio.h>
#include<stdlib.h>
main()
{
int a=5;
float c=1.5;
printf("size of integer type is %d\n",sizeof(int));
printf("size of float type is %d\n",sizeof(float));
printf("size of double type is %d\n",sizeof(double));
printf("size of char type is %d\n",sizeof(char));
printf("size of a is %d\n",sizeof(a));
printf("size of F is %d\n",sizeof(1.5));
}
OutPut:
size of integer type is 4
size of float type is 4
size of double type is 8
size of char type is 1
size of a is 4
size of F is 8
Program to examine loops concept
#include<stdio.h>
#include<stdlib.h>
main()
{
int i=0,n;
printf("enter value for n\n");
scanf("%d",&n);
for(;;)
{
do
{
printf("%d\t",i);
i++;
}while(i<n);
break;
}
}
OutPut:
enter value for n
5
0 1 2 3 4
Program to print student information
void name(void);
void age(void);
studentdata()
{
printf("Student information\n");
name();
age();
printf("we are in main function\n");
exit(0);
}
void name(void)
{
 char ch;
 printf("enter any character between A-Z\n");
 ch=getchar();
 putchar(ch);
 if((ch>='a')&&(ch<='n'))
 {
  printf("name is MADANREDDY\n");
 }
 else
 {
  printf("name is SURESH\n");
 }
}
void age(void)
{
int n;
printf("enter value for n:\n");
scanf("%d",&n);
if((n>0)&&(n<14))
{
printf("child\n");
}
else if((n>14)&&(n<30))
{
printf("young age\n");
}
else if((n>30)&&(n<60))
{
printf("middle age\n");
}
else if(n>60)
{
printf("old age");
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ vi studentinfo.c
madan@madan-Lenovo-G570:~/madan$ cc -nostartfiles studentinfo.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
Student information
enter any character between A-Z
e
ename is MADANREDDY
enter value for n:
56
middle age
we are in main function
Example program to understood header
/*author:B2
  institute:vector
  program written to check preprocessor stage*/
main()
{
 int a,b; //identifiers
 printf("enter values for a and b:\n:");
 scanf("%d%d",&a,&b);
  if(a>b) //loop declaration
{
printf("A is Bigger one\n");
}
else
{
printf("B is bigger\n");
}
}
#include<stdio.h>
OutPut:
enter values for a and b:
:12
43
B is bigger
Program to examine various loops
#include<stdio.h>
#include<stdlib.h>
loops()
{
printf("program to define loops\n");
printf("**************************\n");
forloop();
whileloop();
dowhile();
exit(0);
}
forloop()
{
 int i,n,sum=0;
printf("program to print sum of first N integer values\t");
printf("enter value for N\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
sum=sum+i;
}
printf("sum ofn integers is %d\n",sum);
}
whileloop()
{
 int i=1,n,mul=1;
printf("program to multiply first N integers\t");
printf("***************************************\n");
printf("enter value for n\n");
scanf("%d",&n);
while(i<n)
{
 mul=mul*i;
i++;
}
printf("multiplication=%d\n",mul);
}
dowhile()
{
int i=0,n;
printf("program to show first N integers\n");
printf("**********************************\n");
printf("enter value for N\n");
scanf("%d",&n);
do
{
  printf("the numbers are %d\n",i);
i++;
}while(i<n);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ vi operations.c
madan@madan-Lenovo-G570:~/madan$ vi operations.c
madan@madan-Lenovo-G570:~/madan$ cc -nostartfiles operations.c
/usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 0000000000400440
madan@madan-Lenovo-G570:~/madan$ ./a.out
program to define loops
**************************
program to print sum of first N integer values enter value for N
5
sum ofn integers is 10
program to multiply first N integers
enter value for n
5
multiplication=120
program to show first N integers
enter value for N
5
the numbers are 0
the numbers are 1
the numbers are 2
the numbers are 3
the numbers are 4
Program without main
#include<stdio.h>
#include<stdlib.h>
vector()
{
printf("program without main function...\n");
f1();
f2();
printf("we are in vector function\n");
exit(0);
}
f1()
{
printf("we are in f1 function\n");
}
f2()
{
printf("we are in f2 function\n");
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ vi without_main.c
madan@madan-Lenovo-G570:~/madan$ cc -nostartfiles without_main.c
/usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 0000000000400390
madan@madan-Lenovo-G570:~/madan$ ./a.out
program without main function...
we are in f1 function
we are in f2 function
we are in vector function

Program to print average of two numbers
#include<stdio.h>
main()
{
 int a,b;
 float c;
 printf("enter two values:\n");
 scanf("%d%d",&a,&b);
 c=(float)(a+b)/2;
 printf(" average of first two numbers is %f",c);
} 
OutPut:
enter two values:
1
24 
 average of first two numbers is 12.500000
Program to access a character from getchar function
#include<stdio.h>
main()
{
 int ch;
printf("enter any character:");
ch=getchar();
printf("entered character:");
putchar(ch);
}
OutPut:
enter any character:a
entered character:a
Program to explain escape sequences
#include<stdio.h>
#include<stdlib.h>
main()
{
printf("lineSpace:\n");
printf("tabSpace:\t");
printf("backspace:\b");
}
OutPut:
lineSpace:
tabSpace: backspace
program without main with 2 functions
#include<stdio.h>
#include<stdlib.h>
vector()
{
printf("function with   main....\n");
f1();
f2();
printf("we are in main function\n");
exit(0);
}
f1()
{
printf("we are in f1 function\n");
}
f2()
{
printf("we are in f2 function\n");
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ vi with2fun.c
madan@madan-Lenovo-G570:~/madan$ cc -nostartfiles with2fun.c
/usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 0000000000400390
madan@madan-Lenovo-G570:~/madan$ ./a.out
function with   main....
we are in f1 function
we are in f2 function
we are in main function
program to reverse a number
#include<stdio.h>
#include<string.h> 
main()
{
int n,i,result=0;;
printf("enter any number...\n");
scanf("%d",&n);
printf("number before reverse..%d\n",n);
while(n>0)
{
result=result*10;
result=result+(n%10);
n=n/10;
}
printf("reversed number is=%d\n",result);
}
OutPut:
enter any number...
12
number before reverse..12
reversed number is=21
Program for armstrong number
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//////////////////////////
void arms(int n)
{
int temp,rem,sum=0;
temp=n;
while(n>0)
{
rem=n%10;
sum=sum+(rem*rem*rem);
n=n/10;
}
printf("sum:%d\n",sum);
if(sum==temp)
printf("armstrong number...\n");
else
printf("not an armstrong number..\n");
}


main(int argc,char **argv)
{
int n,sum=0,rem,temp,i;
//if(argc!=2)
//printf("enter correct number of arguments...\n");
for(i=1;i<argc;i++)
{
n=atoi(argv[i]);
arms(n);
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ vi arm.c
madan@madan-Lenovo-G570:~/madan$ cc arm.c
madan@madan-Lenovo-G570:~/madan$ ./a.out 153
sum:153
armstrong number...
madan@madan-Lenovo-G570:~/madan$ ./a.out 153
sum:153
armstrong number...
madan@madan-Lenovo-G570:~/madan$ ./a.out 123
sum:36
not an armstrong number..
Program to convert a number into HexaDecimal format
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
main()
{
int i=0,n,count=0,a[10];
char ch[10];
printf("enter a number\n");
scanf("%d",&n);
while(n>0)
{
a[i]=(n%16);
n=n/16;
i++;
count++;
}
for(i=count-1;i>=0;i--)
{
if(a[i]==10)
ch[i]=a[i]+55;
if(a[i]==11)
ch[i]=a[i]+56;
if(a[i]==12)
ch[i]=a[i]+57;
if(a[i]==13)
ch[i]=a[i]+58;
if(a[i]==14)
ch[i]=a[i]+59;
if(a[i]==15)
ch[i]=a[i]+60;
printf("%c",ch[i]);
printf("\n");
}
}
OutPut:
enter a number
12
E
Program to find square and cube of a number
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int sum(int a,int b)
{
int sum1;
sum1=a+b;
return sum1;
}
int squ(int c)
{
int square;
square=(c*c);
return square;
}
int cube(int s)
{
int cube;
cube=(s*s*s);
return cube;
}
main()
{
int s,c;
int a=10,b=20,cu=cube(s=squ(c=sum(a,b)));
printf("sum=%d square=%d cube=%d ",c,s,cu);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ ./a.out
sum=30 square=900 cube=729000000
Program to view the difference between data types
#include<stdio.h>
#include<string.h>
main()
{
int a=10,b=22,c;
printf("a=%d b=%d c=%d\n",a,b,c=(a+b));
printf("a=%d b=%f c=%f\n",a,b,c=(a+b));
printf("a=%f b=%f c=%f\n",a,b,c=(a+b));
printf("a=%d b=%d c=%d\n",a,b,c=(a+b));
}
OutPut:
enter values for a and b
10
20
a=10 b=20 c=30
a=10 b=0.000000 c=0.000000
a=0.000000 b=0.000000 c=0.000000
a=10 b=20 c=30
Program to find Factorial of a number
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void factorial(int n)
{
int fact=1,i;
for(i=1;i<=n;i++)
fact=fact*i;
printf("factorial of %d =%d\n",n,fact);
}
main(int argc,char **argv)
{
int i,num;
for(i=1;i<argc;i++)
{
num=atoi(argv[i]);
factorial(num);
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ ./a.out 5
factorial of 5 =120
Program to print Fibonacci series
#include<stdio.h>
#include<string.h>
main()a
{
int a=0,b=1,c=0,n;
printf("enter value for n");
scanf("%d",&n);
printf("%d %d ",a,b);
while(n>c)
{
c=a+b;
printf(" %d",c);
a=b;
b=c;
}
printf("\n");
}
OutPut:
enter value for n:
9
0 1 1 2 3 5 8 13
Program to find the string is Armstrong or not
#include<stdio.h>
#include<string.h>
void armstr(int);
main()
{
int min,max,i=0,n,a;
printf("enter min and max range...\n");
scanf(" %d %d",&min,&max);
for(i=min;i<=max;i++)
{
//n=min;
armstr(i);
//temp=n;
}
}

void armstr(int n)
{
//printf("n=%d\n",n);
int temp,rem=0,sum=0;
temp=n;
while(n>0)
{
rem=n%10;
sum=sum+(rem*rem*rem);
n=n/10;
}
if(sum==temp)
printf("armstrong:%d\n",sum);
}
OutPut:
enter min and max range...
100
500
armstrong:153
armstrong:370
armstrong:371
armstrong:407
Program to convert decimal number to any other number format(hex/octal/binary)
#include<stdio.h>
#include<string.h>
main()
{
int n,i=0,a[10],choice=0,count=0;
printf("enter any number...\n");
scanf("%d",&n);
printf("enter ur choice>\n 1:decimal \n2:octal \n3:hexadecimal \n4:binary\n");
scanf("%d",&choice);
if(choice==1)
printf("decimal=%d\n",&n);
else if(choice==2)
{
while(n>0)
{
a[i]=(n%8);
n=(n/8);
i++;
count++;
}
for(i=count-1;i>=0;i--)
printf("%d",a[i]);
printf("\n");
}
else if(choice==3)
{
while(n>0)
{
//i=0;
a[i]=(n%16);
n=(n/16);
i++;
count++;
}
for(i=count-1;i>=0;i--)
printf("%d",a[i]);
printf("\n");
}
else if(choice==4)
{
int bit=31;
loop:
while(bit>=0)
{
n&(1<<bit)?printf("1"):printf("0");
bit--;
goto loop;
}
printf("\n");
}
else
printf("enter correct choice...\n");
}
OutPut:

enter any number...
144
enter ur choice>
 1:decimal 
2:octal 
3:hexadecimal 
4:binary
2
220
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter any number...
100
enter ur choice>
 1:decimal 
2:octal 
3:hexadecimal 
4:binary
3
64
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter any number...
100
enter ur choice>
 1:decimal 
2:octal 
3:hexadecimal 
4:binary
4
00000000000000000000000001100100
Program to print a number is palindrome or not
#include<stdio.h>
#include<string.h> 
main()
{
int n,i,result=0,num;
printf("enter any number...\n");
scanf("%d",&num);
printf("number before reverse..%d\n",num);
n=num;
while(n>0)
{
result=result*10;
result=result+(n%10);
n=n/10;
}
if(num==result)
printf("number is palindrome\n");
else
printf("not a palindrome number...\n");
}
OutPut:
enter any number...
121
number before reverse..121
number is palindrome
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter any number...
123
number before reverse..123
not a palindrome number...
Program to find square root of a number
#include<stdio.h>
#include<stdlib.h>
main()
{
int n,i;
printf("enter n\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
if(i*i==n)
printf("square root of %d is %d\n",n,i);
}
OutPut:
enter n
4
square root of 4 is 2
Program to find sum of digits of a number
#include<stdio.h>
#include<string.h>
/////////////program to print sum of digits///////////
void sumdigit(int n)
{
int sum=0,rem=0;
while(n>0)
{
rem=n%10;
sum=sum+rem;
n=n/10;
}
printf("sum of digits is %d\n",sum);
sum);
}


main(int argc,char **argv)
{
int n,sum=0,rem=0,i,result=0;
for(i=1;i<argc;i++)
{
result=result+sum;
n=atoi(argv[i]);
sumdigit(n);
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ ./a.out 123
sum of digits is 6
madan@madan-Lenovo-G570:~/madan$ ./a.out 14356
sum of digits is 19
Program to swap two numbers
#include<stdio.h>
main()
{
int C,D,temp;
printf("enter values for C and D\n");
scanf("%d%d",&C,&D);
C=(C+D);
D=(C-D);
C=(C-D);
//temp=C;
//C=D;
//D=temp;
printf("c=%d d=%d\n",C,D);
}
OutPut:
enter values for C and D
10
20
c=20 d=10
Program to find Average of numbers
#include<stdio.h>
main()
{
int m1,m2,m3,m4,m5,total=0;
float average;
printf("enter marks in 5 subjects\n");
scanf("%d%d%d%d%d",&m1,&m2,&m3,&m4,&m5);
if(m1&&m2&&m3&&m4&&m5<=50)
{
total=(m1+m2+m3+m4+m5);
average=((m1+m2+m3+m4+m5)/5);
printf("Total:%d Average:%f\n",total,average);
}
else
printf("enter currect marks\n");
}
OutPut:
enter marks in 5 subjects
43
54
32
33
22
Total:184  Average:36.000000
Program to find Area & Perimeter of a circle
//#define pi=3.14;
#include<stdio.h>
main()
{
float l,b,r,arear,perr,areac,perc,pi=3.14;
printf("enter length, breadth of rectangle\n");
scanf("%f%f",&l,&b);
arear=(l*b);
perr=2*(l+b);
printf("area=%f perimeter=%f\n",arear,perr);
printf("enter radius of circle\n");
scanf("%f",&r);
areac=(pi*r*r);
perc=2*pi*r;
printf("area of circle=%f perimeter of circle=%f\n",areac,perc);
}
OutPut:
enter length, breadth of rectangle
1
2
area=2.000000 perimeter=6.000000
enter radius of circle
4
area of circle=50.240002 perimeter of circle=25.120001
Program to print octal,decimal,hexa decimal formats
#include<stdio.h>
main()
{
int n;
printf("enter value for n\n");
scanf("%d",&n);
printf("n=%d n=%x n=%o\n",n,n,n);
}
OutPut:
enter value for n
45
n=45 n=2d n=55
Program to print integer equivalent of character
#include<stdio.h>
main()
{
int n;
printf("enter any integer\n");
scanf("%d",&n);
printf("n=%d n=%c\n",n,n);
}
OutPut:
enter any integer
123
n=123 n={
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter any integer
45
n=45 n=-
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter any integer
56
n=56 n=8
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter any integer
98
n=98 n=b
Dynamic Memory allocation concept Related to 1D array
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
main()
{
int a[4]={0},*p,i;
p=malloc(4*sizeof(int));
for(i=0;i<4;i++)
scanf("%d",&a[i]);
for(i=0;i<4;i++)
printf(" %d ",a[i]);
}
OutPut:
1
2
3
4
 1  2  3  4
Implementation of atoi function
#include<stdio.h>
int myatoi(const char *string);
int main(int argc, char* argv[])
{
int i;
for(i=1;i<argc;i++)
  printf("\n%d\n", myatoi(argv[i]));
//  getch();
 // return(0);
}
int myatoi(const char *string)
{
    int i;
    i=0;
    while(*string)
    {
         i=(i<<3) + (i<<1) + (*string - '0');
         string++;
         // Dont increment i!
    }
    return(i);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ ./a.out 1234

1234
Program to print Binary Equivalent of a number
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
main()
{
int num,bit=0;
printf("enter a number\n");
scanf("%d",&num);
l1:
num&(1<<bit)?printf("1"):printf("0");
bit++;
if(bit<=31)
goto l1;
printf("\n");
}
OutPut:
enter a number
12
00110000000000000000000000000000
(for big endian machine)
Program to print strings through command line
#include"myHeader.h"
main(int argc,char **argv)
{
int i;
for(i=0;i<argc;i++)
printf("%s\n",argv[i]);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ ./a.out 1223 1223
1223
1223
Pointers with address references example
#include"myHeader.h"
main()
{
int a=10;
char *p=&a;
int *p1=&a;
float *p2=&a;
printf("%d\n",*p);
printf("%d\n",*p1);
printf("%d\n",*p2);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ ./a.out
10
10
2147483645
Program to read a file by using fgets
#include"myHeader.h"
main(int argc,char **argv)
{
char ch[20];
FILE *fp;
if(argc!=2)
printf("enter correct no of arguments\n");

fp=fopen(argv[1],"r");
if(fp==NULL)
{
printf("no file exists\n");
return;
}
while((fgets(ch,20,fp)))
printf("%s",ch);
fclose(fp);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter correct no of arguments
no file exists
madan@madan-Lenovo-G570:~/madan$ ./a.out 1.c
#include<stdio.h>
main()
{
 int ch;
printf("enter any character:");
ch=getchar();
printf("entered character:");
putchar(ch);
}
fseek,ftell importance through program
#include"myHeader.h"
main()
{
int pos;
char str[20]="abcdefgh";
FILE *fp;
fp=fopen("madan","r");
if(fp==NULL)
{
printf("no data\n");
return;
}
fseek(fp,4,0);
pos=ftell(fp);
printf("%c ",str[pos]);
fseek(fp,3,1);
pos=ftell(fp);
printf("%c ",str[pos]);
fseek(fp,-1,1);
pos=ftell(fp);
printf("%c ",str[pos]);
//fseek(fp,0,0);
//pos=ftell(fp);
rewind(fp);
printf("%c ",str[pos]);
fclose(fp);
}
OutPut:
e h g g