Sunday 23 April 2023

Exercise 7-To demonstrate writing C programs in modular way (use of user defined functions)

SET A
1. Write a function isEven, which accepts an integer as parameter and returns 1 if the number is
even, and 0 otherwise. Use this function in main to accept n numbers and ckeck if they are even
or odd.

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

int iseven(int n)
{
 if(n%2==0)
   return 1;
 else
   return 0;
}

void main()
{
 int n,ans;
 clrscr();
 printf("\nEnter Number:");
 scanf("%d",&n);

 ans=iseven(n);

 if(ans==1)
   printf("\n%d is even",n);
 else
   printf("\n%d is not even",n);

 getch();
}
/*

Enter Number:6                                                               
                                                                             
6 is even                                                                     

Enter Number:9                                                               
                                                                             
9 is not even                                                                 
                                                                             
*/
2. Write a function, which accepts a character and integer n as parameter and displays the next
n characters.

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

void fun1(char ch,int n)
{
 int i;
 for(i=0;i<n;i++)
     {
      printf("\n%c",ch);
      ch=ch+1;
     }
}
void main()
{
 int n;
 char ch;
 clrscr();
 printf("\nEnter Character:");
 scanf("%c",&ch);
 printf("\nEnter Digit:");
 scanf("%d",&n);
 fun1(ch,n);
 getch();
}
/*

Enter Character:R                                                             
                                                                             
Enter Digit:3                                                                 
                                                                             
R                                                                             
S                                                                             
T                                                                             

 */

SET B
 1. Write a function isPrime, which accepts an integer as parameter and returns 1 if the number
is prime and 0 otherwise. Use this function in main to display the first 10 prime numbers.

#include<stdio.h>
#include<conio.h>
int isprime(int n)
{
 int i,ans=0;
 for(i=2;i<n;i++)
    {
     if(n%i==0)
       {
ans=1;
break;
       }
    }
 return ans;
}

void main()
{
 int i,ans;
 clrscr();
 printf("\nPrime no upto 10");
 for(i=1;i<=10;i++)
    {
     ans=isprime(i);
     if(ans==0)
       printf("\n%d",i);
    }
 getch();
}
/*

Prime no upto 10                                                             
1                                                                             
2                                                                             
3                                                                             
5                                                                             
7                                                                             

*/

2. Write a function that accepts a character as parameter and returns 1 if it is an alphabet, 2 if it
is a digit and 3 if it is a special symbol. In main, accept characters till the user enters EOF and use
the function to count the total number of alphabets, digits and special symbols entered.

 3. Write a function power, which calculates xy. Write another function, which calculates n! Using
for loop. Use these functions to calculate the sum of first n terms of the Taylor series:
sin(x) = x -3!x3+5!x5+ ……

#include<stdio.h>
#include<conio.h>
int power(int x,int y)
{
 int i,ans=1;
 for(i=0;i<y;i++)
     ans=ans*x;
 return ans;
}
int fact1(int n)
{
 int i,fact=1,n1;
 for(i=1;i<=n;i++)
     fact=fact*i;
 return fact;
}
void main()
{
 int x,y,n,ans;
 clrscr();
 printf("\nEnter number and power:");
 scanf("%d%d",&x,&y);
 ans=power(x,y);
 printf("\n %d power %d is %d",x,y,ans);
 printf("\nEnter the number:");
 scanf("%d",&n) ;
 ans=fact1(n);
 printf("\nFactorial of %d is : %d",n,ans);

 getch();
}
/*

Enter number and power:3                                                     
3                                                                             
                                                                             
 3 power 3 is 27                                                             
Enter the number:3                                                           
                                                                             
Factorial of 3 is : 6                                                         
                                               
                            

*/

SET C
Set C . Write C programs for the following problems
1. Write a menu driven program to perform the following operations using the Taylor series.
Accept suitable data for each option. Write separate functions for each option.
i. ex
ii. sin(x)
iii. cos (x)

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

void fun1(int x,int n)
{
 int i,x1=1;
 float fact=1,ans=1;
 for(i=1;i<n;i++)
    {
     x1=x1*x;
     fact=fact*i;
     ans=ans+(x1)/fact;
    }
 printf("\n %f",ans);
}
void fun2(int x,int n)
{
 int i,d=1,x1=x,j;
 float ans;
 for(i=1;i<=n;i++)
    {


     ans=ans+x1/d;

     if(i%2==0)
x1=-x1*x*x;
     else
x1=x1*x*x;

     for(j=1;j<2*i-1;j++)
d=d+j;
    }
 printf("\n sin(%d) : %f",x,ans);
}
void main()
{
 int ch,n,x;
 clrscr();
 printf("\n1.e^x\n2.sin(x)\n3.cos(x)\n4.Exit");

 while(ch!=5)
      {
       printf("\nEnter your choise:");
       scanf("%d",&ch);
       switch(ch)
     {
      case 1:printf("\nEnter number and renge:");
     scanf("%d%d",&x,&n);
     fun1(x,n);
     break;
      case 2:printf("\nEnter number and renge:");
     scanf("%d%d",&x,&n);
     fun2(x,n);
     break;
     }
      }
 getch();

}

No comments:

Post a Comment

Questions

  Exercise 1 Set A . Apply all the three program development steps for the following examples.  1. Accept dimensions of a cylinder and p...