Size of Numeric value

Thursday, 8 September 2011

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("\nSize= %d byte",sizeof(5));
printf("\nSize= %d byte",sizeof(4.5));
printf("\nSize= %d byte",sizeof(4.5f));
getch();
}


OUTPUT:


Size=2 byte
Size=8 byte
Size=4 byte











Size of Data Types

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
float b;
char c;
clrscr();
printf("\nSize of a=%d byte",sizeof(a));
printf("\nSize of b=%d byte",sizeof(b));
printf("\nSize of c=%d byte",sizeof(c));
getch();
}


OUTPUT:


Size of a=2 byte
Size of b=4 byte
Size of c=1 byte









Maximum of two Number using Relational Operator

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter the two integer number\n");
scanf("%d%d",&a,&b);
c=a>b?a:b;
printf("Max of %d and %d is %d\n",a,b,c);
getch();
}


OUTPUT:


Enter the two integer number
4
5
max of 4 and 5 is 5













Use of Relational Operator

#include<stdio.h>
#include<conio.h>
void main()
{
int a=5,b=6,c,d,e,f,g;
clrscr();
c=a>b;
d=a<b;
e=(4!=3);
f=(8>=17);
g=(123<=123);
printf("Value of c=%d\n",c);
printf("Value of d=%d\n",d);
printf("Value of e=%d\n",e);
printf("Value of f=%d\n",f);
printf("Value of g=%d\n",g);
getch();
}


OUTPUT:


Value of c=0
Value of d=1
Value of e=1
Value of f=0
Value of g=1












Logical Operator

#include<stdio.h>
#include<conio.h>
void main()
{
int a=40,b=30,c=50,d,e;
clrscr();
d=a==40&&b==c;
e=b==a&&b<c;
printf("d=%d\ne=%d",d,e);
getch();
}


OUTPUT:


d=0
e=0










Pre Increement

#include<stdio.h>
#include<conio.h>
void main()
{
int x,y;
clrscr();
x=10;
y=++x;
printf("x=%d\ny=%d",x,y);
getch();
}


OUTPUT:


x=11
y=11









Post Increement

#include<stdio.h>
#include<conio.h>
void main()
{
int x,y;
clrscr();
x=10;
y=x++;
printf("x=%d\ny=%d",x,y);
getch();
}


OUTPUT:


x=11
y=10











Pre & Post Increemaent

#include<stdio.h>
#include<conio.h>
void main()
{
int a=5,b=4,c,d;
clrscr();
c=a++-b;
d=++a-b;
printf("c=%d\nd=%d",c,d);
getch();
}

OUTPUT:

c=1
d=3









Comma Operator

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
a=(b=4,c=5,b+c);
printf("a=%d\nb=%d\nc=%d",a,b,c);
getch();
}


OUTPUT:


a=9
b=4
c=5











Swaping Using Bit Wise Operator

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("enter the value of a&b");
scanf("%d%d",&a,&b);
a=a^b;
b=a^b;
a=a^b;
printf("a=%d\nb=%d",a,b);
getch();
}


OUTPUT:


enter the value of a&b
2
3
a=3
b=2








Left & Right Shift Bit wise operator

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
a=5<<2;
b=5>>1;
printf("a=%d\nb=%d",a,b);
getch();
}


OUTPUT:


a=20
b=2












Assignment Operator

#include<stdio.h>
#include<conio.h>
void main()
{
int a=5,b=10,c=15;
clrscr();
a+=5;
b*=5;
c-=5;
printf("a=%d\nb=%d\nc=%d",a,b,c);
getch();
}


OUTPUT:


a=10
b=50
c=10













Subtraction Using Arithmatic Operator

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("enter the value of a&b");
scanf("%d%d",&a,&b);
c=a-b;
printf("\nthe value of c is %d",c);
getch();
}


OUTPUT:


enter the value of a&b
5
4
the value of c is1









Structure and Union

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


struct address
 {
   char h_no[5];
   int s_no;
   char city[10];
   char state[10];
 };


union person
 {
   int id;
   char sex;
   struct address adr;
 };


main()
{
union person p;
p.id=12;
p.sex='M';
strcpy(p.adr.city,"Alwar");
strcpy(p.adr.state,"Rajasthan");
clrscr();
printf("Id:   %d\n",p.id);
printf("Sex:  %c\n",p.sex);
printf("City: %s\n",p.adr.city);
printf("State:%s\n",p.adr.state);
getch();
}


OUTPUT:


Id:    77
Sex:   M
City:  Alwar
State: Rajasthan








Simple Union

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


union person
  {
   int id;
   char sex;
  };


void main()
{
union person p;
clrscr();
p.id=12;
p.sex='m';
printf("Id is %d\n",p.id);
printf("Sex is %c\n",p.sex);
getch();
}


OUTPUT:


Id is 109
Sex is m







Structure and Function

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


  typedef struct
   {
      int ecode;
      char ename[10];
      int esal;
   }emp;


void main()
{
emp e={1013,"Mahesh",20000};
void show(emp);
clrscr();
show(e);
getch();
}


void show(emp p)
{
  printf("\nDetails of Employee\n");
  printf("Name:\t%s\n",p.ename);
  printf("Ecode:\t%d\n",p.ecode);
  printf("Sal:\t%d\n",p.esal);
}


OUTPUT:


Detail's of Employee
Name:   Mahesh
Ecode:  1013
Sal:    20000










Structure and Pointer

#include<stdio.h>
#include<conio.h>
 typedef struct
  {
    int icode;
    char iname[10];
    int quan;
    int uprice;
  }item;
void main()
{
item i1;
item *p;
clrscr();
p=&i1;
printf("Enter Item code,Item name,Quan. and Unit Price");
scanf("%d%s%d%d",
                    &p->icode,p->iname,&p->quan,&p->uprice);
printf("\n Item Code=%d\n",i1.icode);
printf("\n Item Name=%s\n",i1.iname);
printf("\n Item Quantity=%d\n",i1.quan);
printf("\n Item Unit price=%d \-\n",i1.uprice);
getch();
}


OUTPUT:


Enter Item code,Item name,Quantity and Unit Price 
101
Monitor
1
250
Item Code=101
Item Name=Monitor
Item Quantity=1
Item Unit Price=250







Array of Structure

#include<stdio.h>
#include<conio.h>
void main()
{
  typedef struct
    {
      char sname[10];
      int rollno;
      char cname[10];
    }student;
student sarr[3];
int i;
clrscr();
for(i=0;i<3;i++)
{
 printf("\nEnter name and class of student no %d",i+1);
 scanf("%s%s",sarr[i].sname,sarr[i].cname);
 printf("Enter the roll no\n");
 scanf("%d",&sarr[i].rollno);
}
printf("\n Student Detail's\n");
printf("\nSname\tCname\tRollNo\n");
for(i=0;i<3;i++)
{
 printf("%s\t%s\t",sarr[i].sname,sarr[i].cname);
 printf("%d\n",sarr[i].rollno);
}
getch();
}


OUTPUT:


Enter name and class of student no 1 Tushar
first
Enter the roll no
1
Enter name and class of student no 1 Kamal
Second
Enter the roll no
2
Enter name and class of student no 1 Rahul
Third
Enter the roll no
3

 Student Detail's

Sname     Cname    RollNo
Tushar    First        1
Kamal    Second    2
Rahul     Third       3










Declaration and Intialization of Structure

#include<stdio.h>
#include<conio.h>
void main()
{
   struct person
   {
    char name[10];
    int age;
   };
struct person p1={"Avani",21};
struct person p2={"Anshul",23};
clrscr();
printf("Person 1 Details\n");
printf("Name=%s\tAge=%d\n",p1.name,p1.age);
printf("Person 2 Details\n");
printf("Name=%s\tAge=%d\n",p2.name,p2.age);
getch();
}

OUTPUT:


Person 1 Detail's
Name=Avani   Age=21
Person 2 Detail's
Name=Anshul  Age=23








Person Detail's

#include<stdio.h>
#include<conio.h>
void main()
{


struct person
{
char name[10];
int age;
}p1,p2;


clrscr();
printf("Enter name and age of Person 1\n");
scanf("%s%d",p1.name,&p1.age);
printf("Enter name and age of Person 2\n");
scanf("%s%d",p2.name,&p2.age);
printf("Person 1 Details\n");
printf("Name=%s\tAge=%d\n",p1.name,p1.age);printf("Person 2 Details\n");
printf("Name=%s\tAge=%d\n",p2.name,p2.age);
getch();
}


OUTPUT:


Enter name and age of Person 1
Tushar
18
Enter name and age of Person 2
Kamal
20
Person 1 Detail's
Name=Tushar    Age=18
Person 2 Detail's
Name=Kamal     Age=20











Simple Structure

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


struct person
{
char name[10];
int age;
};


void main()
{
struct person p;
clrscr();
printf("Enter name of Person\n");
scanf("%s",p.name);
printf("Enter age of Person\n");
scanf("%d",&p.age);
printf("Name=%s\n",p.name);
printf("Age=%d\n",p.age);
getch();
}


OUTPUT:


Enter name of Person
Tushar
Enter age of Person
18
Name=Tushar
Age=18








String Comparison with string function

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[]="Aakash";
char str2[]="Harsha";
int c;
clrscr();
c=strcmp(str1,str2);
if(c>0)
 {
   printf("string 1 is greater");
 }
else
{
  if(c<0)
   {
     printf("String 2 is greater");
   }
  else
   {
     printf("Equal");
   }
}
getch();
}


OUTPUT:


String 2 is greater









String Reverse

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char stro[30],strr[30];
int i=0,len,j;
clrscr();
printf("Enter any String\n");
gets(stro);
len=strlen(stro);
printf("Original String %s\n",stro);
printf("Revese String\n");
for(i=0,j=len-1;i<len,j>=0;i++,j--)
  {
     strr[j]=stro[i];
  }
for(i=0;i<len;i++)
  {
     printf("%c",strr[i]);
  }
getch();
}


OUTPUT:


Enter eny String
tushar
Orignal String tushar
Reverse String
rahsut









Changing case of string characters

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[30];
int i=0,len;
clrscr();
printf("Enter any string\n");
gets(str);
len=strlen(str);
printf("\nOriginal String:\t");
puts(str);
printf("\nString with case changed:\t");
while(i<len)
{
    if(str[i]>='a'&&str[i]<='z')
    {
       printf("%c",str[i]-32);
    }
else
  {
   if(str[i]>='A'&&str[i]<='z')
    {
      printf("%c",str[i]+32);
    }
   else
     {
     printf("%c",str[i]);
     }
  }
  i++;
}
getch();
}


OUTPUT:


Enter any string
Tushar
Original String:    Tushar
String with case changed:   tUSHAR









Length & Size of string with string length function

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[20];
int len,size;
printf("Enter your name\n");
scanf("%s",str);
len=strlen(str);
printf("Length=%d\n",len);
printf("Size=%d\n",sizeof(str));
getch();
}


OUTPUT:


Enter your name
Tushar
Length=6
Size=20









String Length without using string function

#include<stdio.h>
#include<conio.h>
void main()
{
char str[20];
int i=0;
clrscr();
printf("Enter the String\t");
scanf("%s",str);
printf("The String is\t");
while(str[i]!='\0')
{
  printf("%c",str[i++]);
}
printf("\nThe String Length is %d",i);
getch();
}


OUTPUT:


Enter the String   Tushar
The String is Tushar
The String Length is 6










Pointer and String

#include<stdio.h>
#include<conio.h>
void main()
{
char s[10];
char *p=s;
clrscr();
printf("Enter the String\n");
gets(s);
printf("The String is\n");
for( ;*p!=0;p++)
 {
  printf("%c",*p);
 }
getch();
}


OUTPUT:


Enter the String
Tushar
The String is
Tushar









Array and Pointer and Function

#include<stdio.h>
#include<conio.h>
void main()
{
int a[]={1,2,3,4,5};
void show(int **,int);
int *p=a;
clrscr();
show(&p,5);
getch();
}


void show(int **pp,int s)
{
   int i;
   printf("Array elements are\n");
   for(i=0;i<s;i++)
   printf("%d\n",*(*pp+i));
}


OUTPUT:


Array elements are
1
2
3
4
5











Pointer and Function & Call By Reference

#include<stdio.h>
#include<conio.h>
void main()
{
void change(int *);
int a;
clrscr();
printf("Enter a number");
scanf("%d",&a);
change(&a);
printf("a=%d\n",a);
getch();
}
void change(int *p)
 {
  *p=*p+10;
 }


OUTPUT:


Enter a number 5
a=15

/*It automatically return the value*/






Pointer to Pointer

#include<stdio.h>
#include<conio.h>
void main()
{
int a=5;
int *p1,**p2,***p3;
clrscr();
p1=&a;
p2=&p1;
p3=&p2;
*p1=10;
printf("a=%d\n",a);
**p2=15;
printf("a=%d\n",a);
***p3=20;
printf("a=%d\n",a);
getch();
}


OUTPUT:


a=10
a=15
a=20










Addition with Pointer

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,*pa,*pb,*pc;
clrscr();
pa=&a;
pb=&b;
pc=&c;
printf("Enter the value of a&b\n");
scanf("%d%d",pa,pb);
*pc=*pa+*pb;
printf("a=%d\n",*pa);
printf("b=%d\n",*pb);
printf("c=%d\n",*pc);
getch();
}


OUTPUT:


Enter the value of a&b
2
3
a=2
b=3
c=5













Demo of Pointer

#include<stdio.h>
#include<conio.h>
void main()
{
int a,*pa;
clrscr();
pa=&a;
*pa=10;
printf("Address of a=%u\n",&a);
printf("Value of a=%d\n",a);
getch();
}

OUTPUT:

Address of a=65524
value of a=10








Value & Address of a Variable

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
float b;
a=10;
b=20.34;
printf("Address of a=%u\n",&a);
printf("Value of a=%d\n",a);
printf("Address of b=%u\n",&b);
printf("Value of b=%f\n",b);
getch();
}


OUTPUT:


Address of a=65524
Value of a=10
Address of b=65520
Value of b=20.340000








Sum of Two Float Numbers using Function

#include<stdio.h>
#include<conio.h>
void main()
{
void sum(float,float);
float a,b;
clrscr();
printf("Enter two number\n");
scanf("%f%f",&a,&b);
sum(a,b);
getch();
}


void sum(float x,float y)
{
float t;
t=x+y;
printf("The Sun is %f",t);
}


OUTPUT:


Enter two number
6
3
The Sum is 9.000000








Simple program

Wednesday, 7 September 2011

#include<stdio.h>
#include<conio.h>
void main()
{
void show(int);
int a;
clrscr();
printf("Enter a number");
scanf("%d",&a);
show(a);
getch();
}


void show(int x)
{
printf("U entered=%d\n",x);
}


OUTPUT:


Enter a number5
U entered=5










Nesting of Function

#include<stdio.h>
#include<conio.h>
void main()
{
void fun1();
void fun2();
clrscr();
printf("In main");
fun1();
printf("\nBack in Main");
getch();
}


void fun2()
{
printf("\nIn Function 2");
}


void fun1()
{
printf("\nIn function 1");
fun2();
}




OUTPUT:


In main
In function 1
In function 2Back in Main







Working with Two Function

#include<stdio.h>
#include<conio.h>
void main()
{
void show();
void disp();
clrscr();
printf("In main");
show();
disp();
printf("\nBack in main");
getch();
}


void show()
{
printf("\nIn show function");
}


void disp()
{
printf("\nIn disp function");
}




OUTPUT:


In main
In show function
In disp function
Back in main





Print the statement using Function

#include<stdio.h>
#include<conio.h>
void main()
{
void show(void);
clrscr();
show();
getch();
}


void show(void)
{
printf("Hello from show function");
}


OUTPUT:
  
Hello from show function





Factorial

int s=10;                                  //Global Variable
#include<stdio.h>
#include<conio.h>
main()
{
int sqr();
int ans;
clrscr();
ans=sqr();
printf("Side=%d\nSquare=%d\n",s,ans);
getch();
}


int sqr()
{
return s*s;
}


OUTPUT:

Side=10Square=100




Area of Circle

#include<stdio.h>
#include<conio.h>
void main()
{
float area(float);
float r,a;
clrscr();
printf("Enter the radius\n");
scanf("%f",&r);
a=area(r);
printf("Radius=%f\tArea=%f\n",r,a);
getch();
}
float area(float r)
{
float t;
t=3.14*r*r;
return t;
}


OUTPUT:


Enter the radius
5
Radius=5.000000  Area=78.500000
                                                                                                                        
               





Square of two number

#include<stdio.h>
#include<conio.h>
void main()
{
int sqr(int);
int num,s;
clrscr();
printf("Enter the number\n");
scanf("%d",&num);
s=sqr(num);
printf("num=%d\nSquare of number=%d\n",num,s);
getch();
}


int sqr(int x)
{
int t;
t=x*x;
return t;
}


OUTPUT:


Enter the number
5
num=5
Square of number=25







Enter & Display the elements in a 2-D Array

#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i;
clrscr();
printf("Enter the 5 element in array");
for(i=0;i<5;i++)
  {
    scanf("%d",&a[i]);
  }
printf("\nThe Elements are");
for(i=0;i<5;i++)
  {
    printf("\n%d",a[i]);
  }
getch();
}


OUTPUT:


Enter the 5 element in array2
6
9
4
6

The elements are
2
6
9
4
6





Printing of Odd Number

#include<stdio.h>
#include<conio.h>
void main()
{
   int t=0;
   clrscr();
   while(t<=10)
   {
        t++;
        if(t%2)
    continue;
  
    printf("%d\t",t);
   }
getch();
}


OUTPUT:


2   4   6   8   10




Generation of table of any given number


#include<stdio.h>
#include<conio.h>
void main()
{
int n,t=1,value;
clrscr();
printf("Enter any +ve number\n");
scanf("%d",&n);
while(t<=10)
{
   value=n*t;
   printf("\n%d * %d =%d",n,t,value);
   t++;
}
getch();
}


OUTPUT:


Enter any +ve number
3

3 * 1 =3
3 * 2 =6
3 * 3 =9
3 * 4 =12
3 * 5 =15
3 * 6 =18
3 * 7 =21
3 * 8 =24
3 * 9 =27
3 * 10 =30





Printing all odd number between 1&30

Monday, 29 August 2011

#include<stdio.h>
#include<conio.h>
void main()
{
int t=1;
clrscr();
while(t<30)
{
    if(t%2==1)
    printf(" %d",t);
    t++;
}
getch();
}

OUTPUT:

1  3  5  7  9  11  13  15  17  19  21  23  25  27  29





Number is Even or Odd

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("enter the value of a ");
scanf("%d",&a);
if(a%2==0)
 {
   printf("\nnumber is even");
 }
else
 {
   printf("\nnumber is odd");
 }
getch();
}


OUTPUT:

enter the value of a 5
number is odd




Maximum of three Number

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter the value of a");
scanf("%d",&a);
printf("\nEnter the value of b");
scanf("%d",&b);
printf("\nEnter the value of c");
scanf("%d",&c);
if(a>b&&a>c)
 {
   printf("\na is greater");
 }
if(b>a&&b>c)
 {
   printf("\nb is greater");
 }
else
 {
   printf("\nc is greater");
 }
getch();
}

OUTPUT:

Enter the value of a 5
Enter the value of b 7
Enter the value of c 9
c is greater





 

Blogger news

Blogroll

Enter your email address:

Delivered by FeedBurner

Total Pageviews

Followers

Most Reading