Thursday, September 4, 2014

find the greatest common divisor

0 comments

Problem

Write a function to compute the greatest common divisor given by Euclid’s algorithm, exemplified for J = 1980, K = 1617 as follows:
1980 / 1617 = 1   1980 – 1 * 1617 = 363
1617 / 363 = 4   1617 – 4 * 363 = 165
363 / 165 = 2   363 – 2 * 165 = 33
165 / 33 = 5   165 – 5 * 33 = 0
Thus, the greatest common divisor is 33.

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void gcd();
int main()
{
 gcd();
 system("pause");
 return 0;
}
void gcd()
{
 int j,k,x,y,z,a,b,c,d,e ;
 printf("Enter two numbers of 4 digit : ");
 scanf("%d%d",&j,&k);
 x = j/k ;
 y = j-x*k;
 z = k/y ;
 a = k-z*y;
 b = y/a;
 c = y-b*a;
 d = a/c;
 e = a-d*c;
 printf("The greatest common divisor : %d\n",c);
}

Sample Output

greatest common divisor c language

Read More..

write functions for dividing two numbers, finding larger number, and also prints the ATM menu

0 comments

Problem

Write a function prototype and definition for the following components:
  • A function that divides two numbers and returns the remainder
  • A function that finds the larger of two numbers and returns the result
  • A function that prints an ATM menu (it receives no parameters and returns no value)
Write main function and call the above function and check if it works as required or not.

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void divi();
void lrg();
void atm();
main()
{
 divi();
 lrg();
 atm();
 system("pause");
}
void divi()
{
 int r,x,y;
 printf ("Enter two numbers : ");
 scanf ("%d%d",&x,&y);
 r = x%y;
 printf("The remainder : %d\n",r);
 system("pause");
}
void lrg()
{
 int x,y;
 printf("Enter two numbers : ");
 scanf("%d%d",&x,&y);
 if (x > y)
 printf ("%d is greater than %d\n",x,y);
 else
  printf ("%d is greater than %d\n",y,x);
 system("pause");
}
void atm()
{
 printf("1.cash withdrawl\n");
 printf("2. Fast cash\n");
 printf("3. Balance inquiry\n");
 printf("4.Transictiobn receipt\n");
}

Sample Output

functions c language

Read More..

create a menu driven program to add, subtract, multiply, and divide two number using functions and switch case statement

1 comments

Problem

Write a menu driven program using switch statement that has the following options:
  1. Add numbers
  2. Subtract numbers
  3. Multiply numbers
  4. Divide numbers
  5. Exit
The prototype of the function for add, subtract, multiply and divide is as follows:
int add(int, int);
int sub(int, int);
int mul(int, int);
int div(int, int);

Solution - Code

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

int add (int, int);
int sub (int, int);
int mul (int, int);
int divi (int, int);

int main()
{
 int i,x,y,c;
 printf("Enter two numbers : ");
 scanf ("%d %d",&x,&y);
 printf("1. addition\n");
 printf("2. subtraction\n");
 printf("3. multiplication\n");
 printf("4. division\n");
 printf("5. exit\n\n");
 printf("Which action you want to perform : ");
 scanf ("%d",&i);
 switch(i)
 {
  case 1:
   c=add(x , y);
   printf("Answer = %d\n",c);
   system("pause");
   break;
  case 2:
   c=sub(x , y);
   printf("Answer = %d\n",c);
   system("pause");
   break;
  case 3:
   c=mul(x , y);
   printf("Answer = %d\n",c);
   system("pause");
   break;
  case 4:
   c=divi(x,y);
   printf("Answer = %d\n",c);
   system("pause");
   break;
  case 5 :
   exit(0);
   break;
  default :
   printf("Enter valid number\n");
   system("pause");
 }
}
int add(int x, int y)
{
 int c;
 c = (x+y);
 return c ;
}
int sub(int x, int y)
{
 int c;
 c = (x-y);
 printf("Answer = %d\n",c);
}
int mul(int x, int y)
{
 int c;
 c = (x*y);
 printf("Answer = %d\n",c);
}
int divi(int x, int y)
{
 int c;
 c = (x/y);
 printf("Answer = %d\n",c);
}

Sample Output

switch case statement

Read More..

Write a function to obtain the prime factors of this number.

1 comments

Problem

A positive integer is entered through the keyboard. Write a function to obtain the prime factors of this number.

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void pr_fact();
main()
{
 pr_fact();
 system("pause");
}
void pr_fact()
{
 int x,y,i;
 printf("Enter any integer number : ");
 scanf("%d",&x);
 printf("Prime factors of %d : \n",x);
 i = 1;
 while( i <= x )
 {
  if ( x%i==0 )
   printf("%d\n",i);
  i++;
 }
}

Sample Output


Read More..

Write a function to determine whether the year is a leap year or not.

0 comments

Problem

Any year is entered through the keyboard. Write a function to determine whether the year is a leap year or not.

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void lp_yr();
main()
{
 lp_yr();
 system("pause");
}
void lp_yr()
{
 int x,y;
 printf("Enter any year : ");
 scanf("%d",&x);
 if (x%4==0)
  printf("entered year is leap\n");
 else
  printf("enterd year is not leap\n");
}

Sample Output

leap year or not in c language

Read More..

calculate the area of a rectangle using a function

0 comments

Problem

Write a function that calculates the area of a rectangle. The prototype of the function is
int aor(int len, int bre);

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int aor(int len, int bre);
main()
{
 int len,bre,c;
 printf("Enter length and bredth of the rectangle : ");
 scanf("%d%d",&len,&bre);
 c = aor(len,bre);

 printf("Area of the rectangle : %d",c);
 system("pause");
}
int aor(int len, int bre)
{
 int c;
 c = (len * bre) ;
 return c;
}

Sample Output


Read More..

write a menu driven program to calculate factorial of a number, prime or not, and even or odd using functions

0 comments

Problem

Write the program using functions. There are three functions fact (), prime() and even_odd() that contains the logic for calculating factorial of a number, determine that entered number is prime or not and determine that entered number is even or odd, respectively. Prototypes of the three functions are given below:
void fact(void);
void prime(void);
void even_odd(void);

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void fact(void);
void prime(void);
void even_odd(void);
main()
{
 int c;

 printf("Select one :\n 1. factorial.\n2. prime nmber:\n3. even or odd:\n4. exit.\n Enter choice:");
 scanf("%d",&c);

 switch(c)
 {
  case 1:
   fact();
   break;
  case 2:
   prime();
   break;
  case 3:
   even_odd();
   break;
  case 4:
   exit(0);
  default:
      printf("enter correct choise");
 }
 getch();
}

void fact (void)
{
 int n,b,i;
 printf("enter the first no.:");
 scanf("%d",&n);
 b=1;
 for(i=n;i>0;i--)
  b=b*i;
 printf("factorial is %d",b);
}
void prime(void)
{
 int j;
 printf("enter the first no.:");
 scanf("%d",&j);
 if(j%2==0)
 {
  printf("it is not a prime numbr.");  }
 else
  printf("it is a prime number.");
}
void even_odd(void)
{
 int g;
 printf("enter the first no.:");
 scanf("%d",&g);
 if(g%2==0)
  printf("it is an even number.");

 else
  printf("not an even number");
}

Sample Output


Read More..

Write a menu driven program using switch case statement which calculates factorial of a number, prime or not, even or odd, and power

2 comments

Problem

Write a menu driven program using switch case statement (without functions) that has the following options:
  1. Factorial of a number
  2. Prime or not
  3. Odd or even
  4. Power ( x raise to power y)
  5. Exit
Input should be taken inside the particular case. For example if the user enters choice is 4. Then in case 4 your program can prompt the user to input value of x and then value of y. The switch case statement and the menu (mentioned above) should be in while(1) loop. The only way to end the program is that the user has to selection choice 5. In case 5 you can simply call a function exit (0);

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main()
{
 int c,n,b,i,j,g;
 printf("Select one :\n 1. factorial.\n2. prime nmber:\n3. even or odd:\n4. exit.\n Enter choice:");
 scanf("%d",&c);
 switch(c)
 {
  case 1:
   printf("enter no.:");
   scanf("%d",&n);
   b=1;
   for(i=n;i>0;i--)
    b=b*i;
   printf("factorial is %d",b);
   system("pause");
   break;
  case 2 :
   printf("Enter no.:");
   scanf("%d",&j);
   if(j%2==0)
   {
    printf("it is not a prime numbr.\n");
   }
   else
    printf("it is a prime number.\n");
   system("pause");
   break;
  case 3 :
   printf("enter the :");
   scanf("%d",&g);
   if(g%2==0)
    printf("it is an even number.\n");
   else
    printf("not an even number\n");
   system("pause");
   break;
  case 4 :
   exit(0);
 }
 system("pause");
}

Sample Output


Read More..

Write a menu driven program using switch statement which add, subtract, multiply, and divide

0 comments

Problem

Write a menu driven program using switch statement (without functions) that has the following options:
  1. Add numbers
  2. Subtract numbers
  3. Multiply numbers
  4. Divide numbers
  5. Exit
The switch case statement and the menu (mentioned above) should be in while(1) loop. The only way to end the program is that the user has to selection choice 5. In case 5 you can simply call a function exit (0);

Solution - Code

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int main()
{
 int i,x,y,c;
 printf("Enter two numbers : ");
 scanf("%d %d",&x,&y);
 printf("1. addition\n");
 printf("2. subtraction\n");
 printf("3. multiplication\n");
 printf("4. division\n");
 printf("5. exit\n\n");
 printf("Which action you want to perform : ");
 scanf("%d",&i);
 switch(i)
 {
  case 1:
   c = (x+y);
   printf("Answer = %d\n",c);
   break;
  case 2:
   c = (x-y);
   printf("Answer = %d\n",c);
   break;
  case 3:
   c = (x*y);
   printf("Answer = %d\n",c);
   break;
  case 4:
   c = (x/y);
   printf("Answer = %d\n",c);
   break;
  case 5 :
   exit(0);
  default :
   printf("Enter valid number\n");
 }
 system("pause");
 return 0;
}

Sample Output

switch statement in c language

Read More..

Copyright 2017. All Rights Reserved. Privacy Policy / Terms And Conditions / Sitemap / Contact