Sei sulla pagina 1di 5

GCD program

C Program to Find GCD of two Numbers


Note: GCD of any two numbers (say a and b) lies between 1 and minimum of
two numbers
LCM(a,b) * GCD(a,b)=a*b
main()
{
int num1, num2, min,i;
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
min=(num1>num2)?num2:num1; /* minimum value is stored in variable
min */
for(i=min;i>=1;--i)
{
if(num1%i==0 && num2%i==0)
{
printf("HCF of %d and %d is %d", num1, num2,i);
break;
}
}
}

C Program to Find GCD of n Numbers


Note: GCD of any n numbers (say a and b) lies between 1 and minimum of n
numbers

main()
{
int a[100], n, min,i;
printf("Enter number of elements in an array");

scanf("%d",&n);
printf("Enter %d integers: ",n);
for(i=0;i<n;i++)
scanf("%d", &a[i]);
/* Find minimum value and store it in variable min */
min=a[0];
for(i=1;i<n;i++)
{
if(a[i]<min)
min=a[i];
}
for(i=min;i>=1;--i)
{
count=0;
for(j=0;j<n;j++)
{
if(a[j]%i==0)
count++;
}
if(count==n)
{
printf("HCF is %d", i);
break;
}
}
}

C program to print following pyramid pattern of stars

1. Find prime numbers upto n numbers.


2. Eliminate repeated letters in Array.
1) GCD of elements in an array.
2) Arranging K number of elements of an array in ascending order, remaining in descending order.

3)I had a program to print "Alternate Sorted array" for Eg: 9, 5, 3, 16, 5 then the output must be 3, 5, 16
and the other was merge sort using dynamic memory allocation.
1) Removal of vowel from string and,
2) GCD programs.

main()
{
int row, c, n, temp;
printf("Enter the number of rows in pyramid of stars you wish to see ");
scanf("%d",&n);
temp = n;
for ( row = 1 ; row <= n ; row++ )
{
/* for spacing purpose */
for ( c = 1 ; c < temp ; c++ )
printf(" ");
temp--;
/* for printing stars */
for ( c = 1 ; c <= 2*row - 1 ; c++ )
printf("*");
printf("\n");
}
}
Write a c program to print Pascal triangle.
In mathematics, Pascal's triangle is a triangular array of the binomial
coefficients. In much of the Western world it is named after French

mathematician Blaise Pascal, although other mathematicians studied it


centuries before him in India, Iran, China, Germany, and Italy.

long factorial(int);
main()
{
int row, c, n, temp;
printf("Enter the number of rows you wish to see in pascal triangle\n");
scanf("%d",&n);
temp = n;
for ( row = 0 ; row < n ; row++ )
{
/* for spacing purpose */
for ( c = 1 ; c < temp ; c++ )
printf(" ");
temp--;
/* for printing stars */
for ( c = 0 ; c <= row ; c++ )
printf("%ld ",factorial(row)/(factorial(c)*factorial(row-c)));
printf("\n");
}
}
long factorial(int n)
{
int c;
long int result = 1;
for (c = 1; c <= n; c++)
result = result*c;
return result;

}
Write a c program for Floyds triangle.
This program prints Floyd's triangle. Number of rows of Floyd's triangle to print is entered by the user.
First four rows of Floyd's triangle are as follows :1
23
456
7 8 9 10
It's clear that in Floyd's triangle nth row contains n numbers.

main()
{
int n, row, c, a = 1;
printf("Enter the number of rows of Floyd's triangle to print\n");
scanf("%d", &n);
for (row = 1; row <= n; row++)
{
for (c = 1; c <= row; c++)
{
printf("%d ",a);
a++;
}
printf("\n");
}
}

Potrebbero piacerti anche