SETHA SITE INFORMATION

  • Increase font size
  • Default font size
  • Decrease font size
SETHA SITE INFORMATION

How to find greatest common divisor (GCD) of two numbers in C++?

E-mail Print PDF
The below code is a function in C++ use to return GCD (greatest common divisor of two numbers.
Example:
GCD of 20 and 5 is 5
GCD of 24 and 18 is 2
int GCD(int x, int y)
{
	while(x!=y)
	{
		if(x>y)x=x-y;
		else y=y-x;
	}
	return(x);
}
 

Calculate sum of Fibonacci in C++

E-mail Print PDF
The below code is to calculate the sum of Fibonacci (s=1+2+3+5+8+....).
Example:
if n=2 => s=1+2
if n=3 => s=1+2+3
if n=4 => s=1+2+3+5
if n=5 => s=1+2+3+5+8
.....................
long Fibonacci(int n)
{
	long s=0;
	int f1, f2, f;
	f1=1;
	f2=2;
	s=f1+f2;
	for(int i=3;i<=n;i++)
	{
		f=f1+f2;
		s+=f;
		f1=f2;
		f2=f;
	}
	return s;
}
 

How to get perfect number in C++?

E-mail Print PDF
This article shows you the C++ code to display perfect number between two defined numbers.
A perfect number is a positive integer that is equal to the sum of its proper positive divisors excluding the number itself.
Example: 28=1+2+4+7+14
void showPerfectNumber(int start_n, int end_n)
{
	int s, k;
	int d[5000];
	for(int i=start_n; i<=end_n; i++)
	{
		s=0;
		k=0;
		d[k]='\0';
		for(int j=1; j<=i/2; j++)
		{
			if(i%j==0)
			{
				s+=j;	
				d[k]=j;
				k+=1;
			}
		}
		if(s==i) 
		{
			printf("%d is perfect. Its divisors are\n",i);
			for(int i=0;i<k;i++)
			{
				printf("%d",d[i]);
				if(i<k-1) printf(", ");
			}
			printf("\n");
		}
	}
}

  Download Source Code Here
 

How to know prime number in C++?

E-mail Print PDF
This article codes in Visual C++ shows you how to know one number is a prime number or is not.
A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself.
Example: 2, 3, 5, 7,...
bool isPrime(int n)
{
	bool blnRe=true;
	
	for(int i=2;i<=n/2;i++)
	{
		if(n%i==0)
		{
			blnRe=false;
			break;
		}
	}

	return blnRe;
}
Download Source Code
 

How to get file name from a specified directory in Visual Basic.Net?

E-mail Print PDF
The below VB.Net code uses to get all document files (*.docx) in directory C:\Test\. For example, if we have 3 document files (.docx) in C:\Test\ directory then the below code will result those three files in full path on 3 message boxes sequentially.
 
Visual Basic .Net Code
Dim strFiles() As String
strFiles = System.IO.Directory.GetFiles("C:\Test\","*.docx")
For Each myStr As String In strFiles
    MsgBox(myStr)
Next
 


Page 7 of 19