Wednesday, 10 May 2023

C programming notes from FUNCTIONS

                                                             FUNCTIONS 

Function is a block of code that perform particular task 

TAKE ARGUMENT → DO WORK → RETURN RESULT 


#include<stdio.h>


void printHello();  //decelaration / prototype

int main() {

    printHello();  // function call  

  

    return 0;

}

 

 void printHello()   // function definition

 {

     printf("Hello !\n");

 }


#include<stdio.h>


void printHello();

void printGoodbye();


int main()

{

    printHello();            // jitne bar int mai hum printHello(); likhenge utne bar vo function print hoga                                                    simply   jo hum ne niche likha hai printHello() mai vo print hoga 

    printGoodbye();     

    

    

    return 0;

}


void printHello() 

{

  printf("Hello! \n");  

}

void printGoodbye()

{

    printf("Good bye!\n");

}


// Print namaste is user in indian or if french print bonjour


#include<stdio.h>


void namaste();

void boujour();


int main()

{

printf("Enter country first letter :");

char ch;

scanf("%c ", &ch);


if (ch =='i') 

{

    namaste();

}

else 

{

    boujour();

}    

    

    return 0;

}

 

 void namaste()

 {

     printf("Namaste\n");

 }

 void bonjour()

 {

     printf("bonjour\n");

 } 

Q    DO SUM OF NATURAL NUMBERS  in function

 #include<stdio.h>


int sum(int a, int b);


int main()

{

    int a,b;

    printf("enter first number : ");

    scanf("%d",&a);

    printf("enter second number :");

    scanf("%d",&b);

    

    int s = sum(a, b);

    printf("sum is %d  ", s );

    

    return 0;

}


int sum(int x, int y)

{

    return x+y;

}


Q PRINT ANY TABLE IN FUNCTION 

#include<stdio.h>


void printTable(int n);


int main() {

    int n; 

    printf("enter the number :");

    scanf("%d", &n);

    

    printTable(n);  // argument / actual parameter 

    

    return 0;

}


void printTable(int n) {   // parameter / formal parameter 

    for(int i=1; i<=10; i++) {

        printf("%d\n", i*n);

    }

}         

   DIFFERENCE between argument and parameter 

1) values that are passed in function cell       1) values in function deceleration & definition

2) used to send value                                    2) used to receive value 

3) actual parameter                                        3) formal parameters 


NOTE : a) Function can only return one value at a time

b) changes to parameter in function don't change te value in calling function.


#include<stdio.h>


void calculatePrice(float value);


int main()

{

    float value = 100;

    calculatePrice(value);  // yaha pe jo hum niche function banaya hai vo work kare ga 

    printf("value is : %f \n", value);     // ye bus value jo original hai jo hum ne 100 likha hai vo print 

                                                             //   karega  iska niche ke function se koi lena dena nhi hai 

    

    return 0;

}


void calculatePrice(float value)

{

    value = value + (0.18*value);

    printf("final value : %f", value);

}


~ )  library function  pow(n, 2)  //ye simple power calculate kar ta hai means n ki jage koi bhi nhiber                                          rakho ye uski  power 2 kar dega and hum vo 2 ki jage 3 ya koi aur number likha                                                 tho n ki power vo numbe rho jae gi 

 // Q write function to calculate area of a square , a circle, & a recangle. 

 

 #include<stdio.h>

float squareArea(float side);

float circleArea(float radius);

float rectangleArea(float l, float b);

 

 int main()

 { float l = 5;

   float b = 10;

   printf("area is : %f", rectangleArea(l, b));

  

     return 0;

 }

 

 float squareArea(float side)

 {

     return side * side;

 }

 float circleArea(float radius)

 {

     return 3.14 * radius * radius;

 }

 float rectangleArea(float l, float b)

 {

     return l * b;

 }

SAME PROGRAME MAI BUS PRINT KA THODA STYLE CHANGE KIYA HAI 

 // Q write function to calculate area of a square , a circle, & a recangle. 

 

 #include<stdio.h>

float squareArea(float side);

float circleArea(float radius);

float rectangleArea(float l, float b);

 

 int main()

 { float l = 5;

   float b = 10;

   rectangleArea(l, b);

  

     return 0;

 }

 

 float squareArea(float side)

 {

     return side * side;

 }

 float circleArea(float radius)

 {

     return 3.14 * radius * radius;

 }

 float rectangleArea(float l, float b)

 {

     printf("area is %f", l*b);

 }


                                                RECURSION         


print hello world n times 

#include<stdio.h>


void printHw(int count);


int main()

{

    printHw(5);     // yaha pe   n=5

    return 0;

}

void printHw(int count)

{

    if (count ==0)

    {

        return;

    }

    printf("Hello world\n");

    printHw(count - 1);

}



//Q 30 sum of first n natural numbers


#include<stdio.h>


int sum(int n);


int main()

{

    printf("sum is : %d", sum(5));

    

    return 0;

}


int sum(int n)

{

  if (n==1)

  {

      return 1;

  }


int sumNm1 = sum(n-1);

int sumN = sumNm1 + n;

return sumN;

}


Q  PRINT N FACORIAL 

// Online C compiler to run C program online

#include <stdio.h>


int fact(int n);

int main() {

    

    printf("Factorial is %d", fact(5));  //here we put n=5



    return 0;

}


int fact(int n)

{

    

        if(n==1)         // this line is base case 

        {

            return 1;

        }

    

    int factNm1= fact(n-1);

    int factN = (factNm1 * n);

}


#include <stdio.h>


int factorial(int n);


int main() {

    int n;

    printf("enter the number :");

    scanf("%d", &n);

    int fact = factorial(n);     //ye nhi bhi likha tho chalega sidha niche print karwaya factorial(n) tho                                                                                                                                                     chalega 

    printf("factorial is %d", fact);


    return 0;

}

int factorial(int n){

    if(n==1){

        return 1;

    }

    else{

        return n*factorial(n-1);

    }

}

Q 32 Do degree to farhenite 5:05:00


//Q  33 write function to calculate % of a student from marks in science math & sanskrit.


#include<stdio.h>


int percrn(int science, int maths, int sanskrit);


int main()

{

    int sc = 98;

    int san = 99;

    int math = 95;

    

    printf("Percentage is : %d", percen(sc, san, math));

    

    return 0;

}

int percen(int science, int maths, int sanskrit)

{

    return ((science + maths + sanskrit)/3 ); 

}    

    // function ko niche recall kiya aur fir aur fir return likha ki vo kya hamare liye kya karega . 

//Q write function to print n termss of the fihonacci sequence  


#include<stdio.h>


int fib(int n);


int main()

{

    

    fib(6);

    

    return 0;

}

int fib(int n)

{

    if(n==0 || n ==1) // har recussive functon base case ke bina aadhura  hota hai ya fir galat hota hai tho base case likh na bauth zaruri hai pehele pura function liklo fir baad mai bsaae case likhne upper aao if wala . simply base case vo hai vo pehele se hi define ho like fibnochi of 0 and 1 pehele se hi difine hote hai jise 0 == 0 hota hai aur 1==1 hota hai 

    {

        if(n==0)

        {

            return 0;

        }

        if(n==1)

        {

            return 1;

        }

    }

    int fibNm1= fib(n-1);   // jo fin(int n ) hai vo khud hi ko cal laga raha hai jaise n ke liye calculate kar                                                   na tha tho pehele n-1 calculate kiye aur  usko finNm1 mai store kar diya 

    int fibNm2= fib(n-2);

    int fibN= fibNm1 + fibNm2;

    

    printf("fib of %d is : %d \n", n, fibN);

    return fibN;

}


//Q write function to print n termss of the fihonacci sequence  


// IS PROGRAM MAI FIB OF EVERY TERM PRINT HOGA AUR FIBNOCHI HO N TERM LAST WALA VO BHI PRINT HO GA  KYU KYUKI HUM NE MAIN FUNCTION MAI PRINT WALA STATEMENT LIKHA aur agar maine niche jo function banaya hai ussme vo print wala statement nikal diya aur main mai print wala bus rakha tho bus N term hi print hoga means ex: fibnochi of 6 = 8 bus 8 peint hoga 


#include<stdio.h>




int fib(int n);




int main()


{


    


    printf("%d", fib(6));


    


    return 0;


}


int fib(int n)


{


    if(n==0 || n ==1) // har recussive functon base case ke bina aadhura  hota hai ya fir galat hota hai tho base case likh na bauth zaruri hai pehele pura function liklo fir baad mai bsaae case likhne upper aao if wala . simply base case vo hai vo pehele se hi define ho like fibnochi of 0 and 1 pehele se hi difine hote hai jise 0 == 0 hota hai aur 1==1 hota hai 


    {


        if(n==0)


        {


            return 0;


        }


        if(n==1)


        {


            return 1;


        }


    }


    int fibNm1= fib(n-1); // jo fin(int n ) hai vo khud hi ko call                   laga raha hai jaise n ke liye calculate kar                    na tha tho pehele n-1 calculate kiye aur                    usko finNm1 mai store kar diya 


    int fibNm2= fib(n-2);


    int fibN= fibNm1 + fibNm2;


    


    printf("fib of %d is : %d \n", n, fibN);


    return fibN;


}





No comments:

Post a Comment