Professional Documents
Culture Documents
com/category/c-programming/page/2/
C Program for LCM and GCD
#include<stdio.h>
void main()
{
int a[20],n,i,j,c,max,min;
unsigned long prod;
clrscr();
printf(Enter the no. of entries: );
scanf(%d,&n);
printf(Enter the entries:<BR>);
for(i=0;i<n;i++)
{
scanf(%d,&c);
if(c>0)
a[i]=c;
else
{
printf(Invalid Entry);
return;
}
}
max=a[0];
for(i=0;i<n;i++)
if(a[i]>=max)
max=a[i];
min=a[0];
for(i=0;i<n;i++)
if(a[i]<min)
min=a[i];
for(i=0,prod=1;i<n;i++)
prod=prod*a[i];
for(i=max;i<=prod;i+=max)
{
c=0;
for(j=0;j<n;j++)
if(i%a[j]==0)
c+=1;
if(c==n)
{
printf(The LCM of the nos: %d<BR>,i);
break;
}
}
for(i=min;i>0;i)
{
if (min%i==0)
{
c=0;
for(j=0;j<n;j++)
if(a[j]%i==0)
c+=1;
}
if(c==n)
{
printf(The GCD of the nos: %d,i);
break;
}
}
getch();
}
/* Splitting Odd and Even numbers from an Array */
#include<stdio.h>
#include<conio.h>
void main()
{
int num[20], n, i=0, o=0, e=0, odd[20], even[20];
clrscr();
printf(How many numbers?);
scanf(%d, &n);
printf(\nEnter numbers one by one\n);
for(i=0; i<n; i++)
scanf(%d, &num[i]);
for(i=0; i<n; i++)
{
if(num[i]%2==0)
{
even[e] = num[i];
e++;
}
else
{
odd[o] = num[i];
o++;
}
}
printf(\nEven Numbers in the array: );
for(i=0; i<e; i++)
printf(%d\t, even[i]);
printf(\nOdd Numbers in the array: );
for(i=0; i<o; i++)
printf(%d\t, odd[i]);
getch();
}
/* Prime Number Program Example for goto label */
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n, i, r;
clrscr();
printf(Enter the positive integer value);
scanf(%d, &n);
i=2;
step1:
if(i<=sqrt(n))
{
r=n%i;
if(r==0)
{
printf(%d is not a prime, n);
goto end;
}
}
else
{
i++;
goto step1;
}
printf(%d is prime, n);
getch();
end:
printf( );
}
/* Prime Number Using Functions */
#include<stdio.h>
void main()
{
void prime(int);
int n;
printf(Enter a Number: );
scanf(%d, &n);
prime(n);
}
void prime(int k)
{
int i, flag=0;
if(k <= 3)
flag=1;
else
{
for(i=2; [...]
{
for(j=i;j<=n-1;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
printf(\n The Numbers in ascending order are :\n);
for(i=0;i<n;i++)
printf(%d\t,a[i]);
}
/* Sorting array of numbers Using functions and nested for loop*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,t,j,a[10];
void sort(int a[10],int n);
clrscr();
printf( \n Enter the number [...]