You are on page 1of 6

http://labprograms.wordpress.

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; [...]

/* Reversing a String Using Loop */


#include<stdio.h>
#include<conio.h>
void main()
{
int i=0, n=0;
char str[20];
clrscr();
printf(Enter a String: );
scanf(%s, str);
/*Counting no. of characters in the string*/
while(str[i] != )
{
n++;
i++;
}
printf(\nThe Reversed String is );
for(i=n-1; i>=0; i)
printf(%c, str[i]);
getch();
}
/* Reversing String Using Recursive Function */
#include<stdio.h>
#include<conio.h>
void main()
{
void reverse(void);
clrscr();
printf(\nEnter the Text: );
reverse();
getch();
}
void reverse (void)
{
char c;

if(( c = getchar( ) ) != \n)


reverse();
/* [...]
/* Swapping two Numbers Using temporary variable */
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,t;
clrscr();
printf( Enter two numbers :);
scanf(%d%d,&a,&b);
printf(\nBefore Swapping:\n);
printf(a=%d \n b=%d, a,b);
t=a;
a=b;
b=t;
printf(\nAfter Swapping:\n);
printf( The Values of a = %d and b= %d,a,b);
getch();
}
/* Swapping two Numbers without using temporary variable*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,t;
clrscr();
void swap(int a ,int b);
printf( Enter two numbers :);
scanf(%d%d,&a,&b);
printf(\n Before Exchange a=%d , b=%d ,a,b);
swap(a,b);
printf(\n After Exchange [...]
/* Sorting array of numbers- Using Single Dimensional Array& nested for loop */
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,t,j,a[10];
printf( \n Enter the number of values : );
scanf(%d,&n);
prinft(\n Enter the numbers :);
for(i=0;i<n;i++)
scanf(%d,&a[i]);
for(i=0;i<n-1;i++)

{
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 [...]

You might also like