Monday, May 23, 2016

How to find GCD of two numbers in C Language

Find GCD of two numbers in C Language

C Program for GCD of Two Numbers:


#include<stdio.h>
void main()
{
int x, y;
x = 12; y = 18;

while(x != y)
{
if(x > y)
x = x - y;
else
y = y - x;
}

printf("GCD of x and y is = %d", x);
}

How to find LCM of two numbers in C Language

Find LCM of two numbers in C Language

C Program for LCM of Two Numbers:


#include<stdio.h>
void main()
{
int x, y, LCM;
x = 4; y = 9; LCM =1;

for(;;)
{
if(LCM % x != 0 || LCM % y != 0)
++LCM;
else
break;
}

printf("LCM of x and y is = %d", LCM);
}

How to swap two numbers in C Language

Swapping of two numbers in C Language

Using Two Variable:

Program 1:

#include<stdio.h>
#define SWAP(a,b) (a)^=(b)^=(a)^=(b)
void main()
{
int a,b;
a=12; b=17;
SWAP(a,b);
printf("a=%i,b=%i",a,b);
}

Program 2:

#include<stdio.h>
void main()
{
int a,b;
a=12; b=17;
a=a+b;
b=a-b;
a=a-b;
printf("a=%i,b=%i",a,b);
}

Using Third Variable:

#include<stdio.h>
void main()
{
int a,b,temp;
a=12; b=17; temp=0;
temp=a;
a=b;
b=temp;
printf("a=%i,b=%i",a,b);
}

Sunday, May 15, 2016

How to print "Hello World" without using semicolon in main?

How to print "Hello World" without using semicolon in main

There are number of ways you can do it.

First Way:

#include<stdio.h>
void main()
{
if (printf("Hello World")) {}
}

Second Way:

#include<stdio.h>
void main()
{
while(!printf("Hello World")) {}
}

Third Way:

#include<stdio.h>
void main()
{
switch(printf("Hello World")) {}
}

Fourth Way:

#include<stdio.h>
#define PRINT printf("Hello World")
void main()
{
if(PRINT) {}
}