You are on page 1of 10

Write a C program to check number whether

a number is Fibonacci term or not.

Ans.
/*c program to check a number is include in
Fibonacci term or not*/
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,c,next,num;
printf("Enter any number: ");
scanf("%d", &num);
if((num==0)||(num==1))
printf("\n%d is a Fibonacci term",num);
else
{
a=0;
b=1;
c=a+b;
while(c<num)
{
a=b;
b=c;
c=a+b;
}
if(c==num)
printf("\n%d is a Fibonacci
term",num);
else
printf("\n%d is not a Fibonacci
term",num);
}
getch();
return 0;
}
/************Output************/

Screen shot for number is Fibonacci term C program


Example: Check Prime Number
#include <iostream>
using namespace std;

int main()
{
int n, i;
bool isPrime = true;

cout << "Enter a positive integer: ";


cin >> n;

for(i = 2; i <= n / 2; ++i)


{
if(n % i == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
cout << "This is a prime number";
else
cout << "This is not a prime number";

return 0;
}

Write a program to sum


the following series
1/3 + 3/5 + 5/7 + 7/9 +
11/13 + ... + 95/97 +
97/99

1 #include<iostream> Edit & Run


2 using namespace std;
3
4 int main()
5 {
6 int i;
7 int n;
8 float sum=0;
9
10
11 for(i=1,n=i+2;i<100&&n<=100; i++,n++)
12
13 //cout << i/n;
14 sum += (i/n);
15
16 cout<<"Sum = "<<sum;
17
18
19 return 0;
20 }
Perfect Number Program in C, C++

Write a C, C++ program to check whether a given number is perfect or not. We have to write a program to check

whether a input number is perfect.

Perfect Number

A perfect number, is a positive integer that is equal to the sum of its proper divisors.

For example - 6 is a perfect number.

6 = 1 + 2 + 3 (1,2,3 is it's factors).

Similarly 28 is a perfect number.

28 = 1 + 2 + 4 + 7 + 14

Let's take another example of 12. Factors of 12 is 1, 2, 3, 4, 6.

1 + 2 + 3+ 4 + 6 = 16. So we can conclude that 12 is not a perfect number.

Perfect Number Program in C++

#include <iostream>

using namespace std;

int main() {
int num, sum = 0;

cout << " Enter a number to check whether it's a perfect or not \n";

cin >> num;

for (int i = 1; i < num; i++) {

if (num % i == 0) {

sum = sum + i;

if ( sum == num) {

cout << num << " is a Perfect number";

} else {

cout << num << " is not a Perfect number";

return 0;

}
isFibo.cpp
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {

int t;
cin >> t;
while( t-- )
{
long long n;
cin >> n;
double root = sqrt(5.0*n*n+4.0);
long long iRoot = root;
if( root == iRoot )
{
cout << "IsFibo" << endl;
}

else
{

root = sqrt(5.0*n*n-4.0);
iRoot = root;
if( root == iRoot )
{
cout << "IsFibo" << endl;
}
else
{

cout << "IsNotFibo" << endl;


}
}
}
return 0;
}
C++ code to find prime number using for loop
#include<iostream>

using namespace std;

int main()
{

int num,factorial=1;

cout<<" Enter Number To Find Its Factorial: ";

cin>>num;

for(int a=1;a<=num;a++)

factorial=factorial*a;

cout<<"Factorial of Given Number is ="<<factorial<<endl;

return 0;

}
Image View Of Program

You might also like