• Click below to download Unit 2 programs in pdf file: -



Unit 2


/* 1. Write a user defined function which will swap the values of two

variables declared locally in the main program. */


#include <stdio.h>


void swap(int *xp, int *yp)

{

    int temp = *xp;

    *xp = *yp;

    *yp = temp;

}

int main()

{

    int x=10, y=20;

    swap(&x, &y);

    printf("After Swapping: x = %d, y = %d", x, y);

    return 0;

}




/* 2. Write a user defined function calc(), which will returns the sum,

subtraction, multiplication, and division values of two variable

locally declared in the main function. */


#include<stdio.h>


int main()

{

  int num1, num2;


  printf("Enter two numbers: ");

  scanf("%d %d", &num1, &num2);


  printf("%d + %d = %d\n", num1, num2, add(num1, num2));

  printf("%d - %d = %d\n", num1, num2, subtract(num1, num2));

  printf("%d * %d = %d\n", num1, num2, multiply(num1, num2));

  printf("%d / %d = %d\n", num1, num2, divide(num1, num2));

}


int add(int n1, int n2)

{

  int result;

  result = n1 + n2;

  return result;

}


int subtract(int n1, int n2)

{

  int result;

  result = n1 - n2;

  return result;

}


int multiply(int n1, int n2)

{

  int result;

  result = n1 * n2;

  return result;

}


int divide(int n1, int n2)

{

  int result;

  result = n1 / n2;

  return result;

}




/* 3. Write a user defined function which will return the length of the

string declared locally in the main function. */


#include<stdio.h>


int main()

{

   char str[100];

   int length;


   printf("Enter the String : ");

   gets(str);


   length = FindLength(str);


   printf("\nLength of the String is : %d", length);

}


int FindLength(char str[]) {

   int len = 0;

   while (str[len] != '\0')

      len++;

   return (len);

}




/* 4. Write a program, which takes a name of the user in the

lowercase letters. Call a user defined function upper which will

convert all lowercase letters into the uppercase letter. Finally print

the string. */


#include<stdio.h>


int main()

 {

     char s[100];

     int i = 0;


     printf("Enter a string: ");

     scanf("%s", s);


     while( s[i] != '\0' )

     {

        if( s[i] >= 'a' && s[i] <= 'z' )

        {

           s[i] = s[i] - 32;

        }

        i++;

     }

     printf("In Upper Case is: ");

     puts(s);


     return 0;

 }




/* 5. Write a user defined function to reverse the given string. */

#include <stdio.h>

#include <string.h>


void revstr(char *str1)

{

    int i, len, temp;

    len = strlen(str1);


    for (i = 0; i < len/2; i++)

    {

        temp = str1[i];

        str1[i] = str1[len - i - 1];

        str1[len - i - 1] = temp;

    }

}


    int main()

    {

        char str[50];

        printf (" Enter the string: ");

        gets(str);


        revstr(str);

        printf ("After reversing the string: %s", str);

    }




/* 6. Create a structure product with ProductCode (int), Name (char

array) and Price data elements. In the main function declare p[5] of

product. Do the necessary data entry for all five products. Pass the

base address of an array to user defined function inc_price(), which

will increase the price of all the products by 10%. Print all the

products with all the details again after increasing the price. */


#include<stdio.h>

#include<conio.h>


struct product

{

    int  cod;

    char name[10];

    int  p;

}s[3];


void input(struct product up[],int n)

{

    int i;


    for(i=1;i<=n;i++)

{

    printf("%d) ITEM CODE:",i);

    scanf("%d",&up[i].cod);


    printf("ITEM NAME:");

    scanf("%s",up[i].name);


    printf("ITEM PRICE:");

    scanf("%d",&up[i].p);

}

}


void output(struct product up[],int n)

{

    int i;

    for(i=1;i<=n;i++)

{

    printf("\n%d ITEM CODE:%d",i,up[i].cod);

    printf("\nITEM NAME:%s",up[i].name);

    printf("\nITEM PRICE:%d",up[i].p *10/100+up[i].p);

}

}


void main()

{

    int i;


    input(s,3);

    output(s,3);

}




/* 7. Create a structure student with rollno (int), name (char array),

marks (int), grade (char). Create an array stu[5] of type student.

Take the details of students like rollno, name, and marks from the

user. Call UDF prepare_result() which will store the values for

grade based on marks (if marks >=75 then ‘A’, Between 60 to 75

‘B’, Between 50 to 60 ‘C’, Between 35 to 50 ‘D’ and Below 35 ‘F’

grade). Print the details of the students with Grade. */



#include <stdio.h>


struct student

{

    char name[50];

    int roll;

    int marks;

    char grade;

} stu[5];


int main()

{

    int i,n=5;

    struct student stu[5];



for(i=0;i<n;i++)

{

    printf("Enter information of student %d:\n",i+1);


    printf("Enter name: ");

    scanf("%s", stu[i].name);


    printf("Enter roll number: ");

    scanf("%d", &stu[i].roll);


    printf("Enter marks: ");

    scanf("%d", &stu[i].marks);

}


printf("Displaying Information:\n");


for(i=0;i<n;i++)

{

    printf("%d)Student info\n",i+1);


    printf("\tName:%s\n ",stu[i].name);


    printf("\t Roll number: %d\n",stu[i].roll);


    printf("\t Marks: %d\n",stu[i].marks);


    printf("\t Grade: %c\n",stu[i].grade=prepare_result());


}

}


int prepare_result(int marks)

{

    char grade;

    int n=5;


    for(int i=0;i<n;i++)

    {

    if (stu[i].marks >=75)

    {

        return grade = 'A';

    }


    else if (stu[i].marks >=60 && stu[i].marks<75)

    {

        return grade = 'B';

    }


    else if (stu[i].marks >=50 && stu[i].marks<60)

   {

     return grade = 'C';

   }


    else if (stu[i].marks >=35 && stu[i].marks<50)

    {

        return grade = 'D';

    }


    else if (stu[i].marks<35)

    {

       return grade = 'F';

    }


  }

}