-->

Lab works-6(user defined function)

 questions:

q1)Write a program to find the simple interest using function.

q2)Write a program find out greatest number among four different numbers.

q3)Write a program to print multiplication table of a number.

q4)Write a program using user defined function to calculate y raise power to x.

q5)Write a program to find factorial value of a number using recursion.

q6)Write a program to print Fibonacci series using recursive function

q7)Write a program to find sum and average of n numbers using array and function.

q8)Write a program to print greatest number among n numbers using array and function.

q9)Write a program to print transpose of matrices of order 2x2 using array and function.

q10)Write a program to print sum of two matrices of order 2x2 using array and function.

--------------------------------------------------------------------------------------------------------------

solutions:-

q1)Write a program to find the simple interest using function.

Code:-

//Write a program to find the simple interest using function.

#include<stdio.h>                      

float simpleinterest(float,float,float);                         

int main()                              

{

float p,t,r;

printf("enter p,t and r\n");

scanf("%f%f%f",&p,&t,&r);

printf("simple interest=%f",simpleinterest(p,t,r) );                                     

return 0;                                      

}

float simpleinterest(float p,float t,float r)                      

{

 float sinterest;

sinterest=p*t*r/100;

return sinterest;

}

q2)Write a program find out greatest number among four different numbers.

ans:

code:

//Write a program find out greatest number among four different numbers.

#include<stdio.h>                      

float greatestnum(float,float,float,float);                         

int main()                              

{

float num1,num2,num3,num4;

printf("enter four numbers\n");

scanf("%f%f%f%f",&num1,&num2,&num3,&num4);

printf("greatest number=%f",greatestnum(num1,num2,num3,num4) );                                     

return 0;                                      

}

float greatestnum(float a,float b,float c,float d)                 

{

 if(a>b&& a>c &&a>d)

  return a;

 else if(b>a&& b>c &&b>d)

  return b;

 else if(c>a&& c>b &&c>d)

  return c;

 else

return d;

}


q3)Write a program to print multiplication table of a number.

ans:

code:

//Write a program to print multiplication table of a number

#include<stdio.h>                      

void mtable(int x);                         

int main()                              

{

int number;

printf("enter a number\n");

scanf("%d",&number);

printf("multiplication table of number %d is\n",number);

   mtable(number);                                 

return 0;                                      

}

void mtable(int x)             

{

 int i;

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

{

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

}

}

q4)Write a program using user defined function to calculate y raise power to x.
ans:
code:
//Write a program using user defined function to calculate y raise power to x.
#include<stdio.h> 
#include<math.h>                     
int xtoy(int x,int y);                         
int main()                              
{
int x,y;
printf("enter a number(base))\n");
scanf("%d",&x);
printf("enter a number (power)\n");
scanf("%d",&y);
printf("result of y raise to power of x=%d\n",xtoy(x,y));
return 0;                                      
}
int xtoy(int x,int y)             
{
 int result;
 result=pow(x,y);
 return result;
}

q5)Write a program to find factorial value of a number using recursion.

ans:

code:

//program to find factorial value of a number using recursive function.

#include<stdio.h>                      

int factorial(int);                         

int main()                              

{

int number;

printf("enter a number\n");

scanf("%d",&number);

printf("factorial value=%d",factorial(number));                                     

return 0;                                      

}

int factorial(int n)                            

{

if(n<=0)

{

return 1;

}

else

{

return n*factorial(n-1);

}

}

q6)Write a program to print Fibonacci series using recursive function

q7)Write a program to find sum and average of n numbers using array and function.

ans:-

code:-

//Write a program to find sum and average of n numbers using array and function.

#include<stdio.h>                   

void sumandaverage(int ar[],int size);                         

int main()                              

{

int x[100],size,i;

printf("enter total size of array\n");

scanf("%d",&size);

printf("enter elements\n");

for(i=0;i<=size-1;i++)

{

scanf("%d",&x[i]);

}

sumandaverage(x,size);

return 0;                                      

}

void sumandaverage(int ar[100],int size)             

{

 int sum=0;

 float average;

 for(int i=0;i<=size-1;i++)

 {

  sum=sum+ar[i];

 }

 average=(float)sum/size;

 printf("sum=%d,average=%f",sum,average);

}


q8)Write a program to print greatest number among n numbers using array and function.

ans:

code:

//Write a program to print greatest number among n numbers using array and function.
#include<stdio.h>                   
int greatestnum(int ar[],int size);                         
int main()                              
{
int x[100],size,i;
printf("enter total size of array\n");
scanf("%d",&size);
printf("enter elements\n");
for(i=0;i<=size-1;i++)
{
scanf("%d",&x[i]);
}
printf("greatest number=%d",greatestnum(x,size));
return 0;                                      
}
int greatestnum(int ar[],int size)                               
{
 int gr=ar[0];
 for(int i=0;i<=size-1;i++)
 {
  if(gr<ar[i])
  {
      gr=ar[i];
  }
 }
 return gr;
}

q9)Write a program to print transpose of matrices of order 2x2 using array and function.

ans:-

code:-

//Write a program to print transpose of matrices of order 2x2 using array and function.
#include<stdio.h>                   
void transpose(int ar[2][2]);                         
int main()                              
{
int x[2][2],i,j;
printf("enter elements\n");
for(i=0;i<=1;i++)
{
    for(j=0;j<=1;j++)
    {
scanf("%d",&x[i][j]);
    }
}
transpose(x); 
return 0;                                      
}
void transpose(int ar[2][2])                         
                     
{
    int i,j;
for(i=0;i<=1;i++)
{
    for(j=0;j<=1;j++)
    {
printf("%d",ar[j][i]);
    }
    printf("\n");
}
 
}

q10)Write a program to print sum of two matrices of order 2x2 using array and function.

//Write a program to print sum of two matrices of order 2x2 using array and function.
#include<stdio.h>                   
void sum_matrix(int ma[2][2],int ma1[2][2]);                         
int main()                              
{
int m1[2][2],m2[2][2],i,j;
printf("enter elements of first matrix\n");
for(i=0;i<=1;i++)
{
    for(j=0;j<=1;j++)
    {
scanf("%d",&m1[i][j]);
    }
}
printf("enter elements of second matrix\n");
for(i=0;i<=1;i++)
{
    for(j=0;j<=1;j++)
    {
scanf("%d",&m2[i][j]);
    }
}
sum_matrix(m1,m2); 
return 0;                                      
}
void sum_matrix(int ma[2][2],int ma1[2][2])                        
{
    int i,j;
for(i=0;i<=1;i++)
{
    for(j=0;j<=1;j++)
    {
printf(" %d ",ma[i][j]+ma1[i][j]);
    }
    printf("\n");
}
 
}

--------------------------------------------------------------------------------------------------------------

case study:

Write an interactive program that takes a set of numbers and performs the following operation based on user's choice.Make separate function for the respective problem(array related multi functions program).

1. Print in ascending order

2.Average of the numbers

3.Search for a key number

4.find out maximum

5.Exit from the program

Ans:-

/*case study of user defined function*/

#include<stdio.h>

#include<stdlib.h>

void ascend_order(int numbers[],int size);

void average_of_numbers(int numbers[],int size);

void search_a_number(int numbers[],int size);

void max_a_number(int numbers[],int size);

int main()

{

int number[100];

int i,n,ch;

printf("enter the size of array\n");

scanf("%d",&n);

printf("enter numbers\n");

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

    {

    scanf("%d",&number[i]);

    }

    while(1)

    {

    printf("\n|------------------------------------------|\n");

    printf("\n|1--->to arrange numbers in ascending order|\n");

    printf("\n|2--->to find Average of numbers           |\n");

   printf("\n|3--->to Search Record                     |\n");

    printf("\n|4--->to find Highest/maximum Marks        |\n");

    printf("\n|5--->to Exit                              |\n");

    printf("\n|------------------------------------------|\n");

    printf("\n|Please input your choice:                 |\n");

    scanf("%d",&ch);

    switch(ch)

    {

        case 1: printf("\nwe are going to arrange records in ascending order\n");

ascend_order( number,n);

break;

    case 2: printf("average of numbers=\n");

average_of_numbers(number,n);

break;

case 3: printf("we are going to search a number\n");

search_a_number(number,n);

break;

case 4: printf("\nwe are going to find maximum number:\n");

max_a_number(number,n);

break;

default:

printf("\nthank you for using program! now exiting...");

exit(0);

    }

}

}

void ascend_order(int numbers[],int size)

{

int i,j,t;

for(i=0;i<=size-1;i++)

{

for(j=i+1;j<=size-1;j++)

{

if(numbers[i]>numbers[j])

{

t=numbers[i];

numbers[i]=numbers[j];

numbers[j]=t;

}

}

}

for(i=0;i<=size-1;i++)

{

         printf("%d\n",numbers[i]);

}

}

void average_of_numbers(int numbers[],int size)

{

int i,j,sum=0;

float average;

for(i=0;i<=size-1;i++)

{

sum=sum+numbers[i];

}

average=(float)sum/size;

printf("average=%f\n",average);

}

void search_a_number(int numbers[],int size)

{

int i,j,number_to_search;

int flag=0;

printf("enter a number to search\n");

scanf("%d",&number_to_search);

for(i=0;i<=size-1;i++)

{

        if(number_to_search==numbers[i])

            flag=1;

}

if(flag==0)

{

    printf("number is not in the list\n");

}

else

{

    printf("number is in the list\n");

}

}

void max_a_number(int numbers[],int size)

{

    int i,j,max;

    max=numbers[0];

for(i=0;i<=size-1;i++)

{

    if(max<numbers[i])

    max=numbers[i];

}

printf("maximum number=%d",max);

}

No comments:

Post a Comment