Posts

Showing posts from July, 2020

Structure

Bank details #include <stdio.h> struct bank {   int acno;   float money;   char name[ 10 ]; }; void main() {   struct bank a[ 3 ];   int i;   for (i= 1 ;i<= 3 ;i++)   {   printf ( "enter ac number \n" );   scanf ( "%d" ,&a[i].acno);   printf ( "enter amount of ac no :%d\n" ,a[i].acno);   scanf ( "%f" ,&a[i].money);   printf ( "enter name of ac no :%d\n" ,a[i].acno);   scanf ( "%s" ,&a[i].name);   }   for (i= 1 ;i<= 3 ;i++)   display(a[i]); } display( struct bank a) {   printf ( "ac number     money            name\n" );   printf ( "  %d           %.2f             %s\n" ,a.acno,a.money,a.name); }

Recursion

Factorial #include <stdio.h> unsigned long fact( int ); void main() {   int a;   printf ( "\nenter value of n " );   scanf ( "%d" ,&a);   printf ( "\n%d" ,fact(a));   /*b=sum(a);   printf("\n%d",b);*/ } unsigned long fact( int a) {   if (a== 1 )   return 1 ;   return (a*fact(a- 1 )); } Fibonacci  #include <stdio.h> void main() {   int a= 0 ,i= 0 ; //b=1,n,i=2,sum=0;   printf ( "\nenter length" );   scanf ( "%d" ,&a);   for (i= 1 ;i<=a;i++)   printf ( "\n%d" ,fib(i));   }   int fib( int a) {   if (a== 1 )   return 0 ;   else if (a== 2 )   return 1 ;   return (fib(a- 1 )+fib(a- 2 )); } GCD //greatest common devider #include <stdio.h> void main() {   int a,b;   printf ( "\nenter values " );   scanf ( "%d%d" ,&a,&b);   printf ( "%d" ,gcd(a,b));   } int gcd( int

Stack

Creation Of Stack  #include <stdio.h> #include <conio.h> #define capacity 3 int stack[capacity]; int top=- 1 ; void push( int ); int pop(); void traverse(); void peek(); int empty( int ); int full( int ); void main() {    int a,b,c;         while ( 1 ) {      printf ( "For push enter 1 \n" );    printf ( " for pop enter 2 \n" );    printf ( " for peek 3 \n" );    printf ( " for traverse enter 4 \n" );    printf ( " for quit enter 5 \n" );      scanf ( "%d" ,&a);      switch (a)   {     case 1 :{             printf ( "enter Element To Push\n" );             scanf ( "%d" ,&b);             push(b);             break ;             }     case 2 :{ if (empty(top))               printf ( "stack underflow\n" );               else         {                             c=pop();            printf ( &quo

Dinamic mamory allocation

Malloc #include <stdio.h> void main() {   int n,*p,sum= 0 ,i;   printf ( "enter the value of n" );   scanf ( "%d" ,&n);   p=( int *) malloc (n* sizeof ( int )); for (i= 0 ;i<n;i++) {    scanf ( "%d" ,p+i);    sum= sum+*(p+i);   } printf ( "sum=%d" ,sum); } Calloc #include <stdio.h> void main() {   int n,*p,i,sum= 0 ;     printf ( "enter value of n" );   scanf ( "%d" ,&n);     p=( int *) calloc(n, sizeof ( int ));     for (i= 0 ;i<n;i++)   {     scanf ( "%d" ,p+i);     sum=sum+*(p+i);   }   printf ( "%d" ,sum); }

Rules of pointers

Int a,*b; b=&a; (right) Cant asing any value in adress &a=5 ( Wrong ) Pointer and variable must be in same datatype Pointer Always Contain 2 Bytes mamory  Pointer of pointers Int a,*b,**c; b=&a; c=&b; *C points on b  **C points on a Can't add ,multiply or devide two adress ( substraction possible) but constant can add Int a,b; Int *c,*d; C=&a d=&b; NOT Possible &a+&b c+d &a*&b c*d &a/&b c/d Substraction Possible &a-&b  c-d If c=140 d=120 and it's int c-d = 10 substraction Of Adress = Normal Substraction /sizeof(variable)

Structure

Image
Structure = Assign value in structure data type 1. Struct a { Int x Char y }B={4,n};// Declaire Variable and assign values Video #include <stdio.h> struct book input( void ); // declaire structure return function struct book // here ";" not available {   int BI;   char BN[ 10 ];   float BP; }; // here ";" Is Awailable void main() {   struct book B1; // must use struct before datatype   B1=input();   display();   } struct book input() // use return type {   struct book B2;   printf ( "enter the Book Id\n" );   scanf ( "%d" ,&B2.BI);   printf ( "enter the name of book\n" );   fflush(stdin);   gets(B2.BN);   printf ( "entert Book Price\n" );   scanf ( "%f" ,&B2.BP);   return B2; } void display( struct book B1) // use of input {   printf ( "book id = %d\n book name= %s \n book price=%f" ,B1.BI,B1.BN,B1.BP); }

Macros

 Object macros //define Variable from macro its call object macro #include <stdio.h> #define a 10 //no ";" void main() {   int b= 5 ;   printf ( "addition=%d" ,a+b);   } Function Macros //define Function From Macro #include <stdio.h> #define malti(a,b) (a*b) //no ";" void main() {   int a ,b;   printf ( "a&b=" ); // for input   scanf ( "%d%d" ,&a,&b);   printf ( "%d" ,malti(a,b));   }

Call By Value And Call By Refrence

Call By Value #include <stdio.h> void swap( int , int ); void main() {    int a,b;       printf ( "enter the value of a and b " );    scanf ( "%d%d" ,&a,&b);    printf ( "before Swipe a=%d And b=%d" ,a,b);    swipe(a,b);    printf ( "\nafter swapping a=%d And b=%d" ,a,b); } void swipe( int a, int b) {    int temp;    temp=x;    x=y;    y=temp;    } Call By Reference #include <stdio.h> void swap( int *, int *); void main() {   int a,b;   printf ( "enter value of a and b" );   scanf ( "%d%d" ,&a,&b);   printf ( "before swap a=%d and b=%d" ,a,b);   swap(&a,&b);   printf ( "after swapping a=%d and b=%d" ,a,b);   } swap( int *x, int *y) {   int t;   t=*x;   *x=*y;   *y=t;   }