Here is a series of a few source codes on Series and Numbers. The series are nothing only logic and the relations between the numbers all you have to find.
Factors : Print those numbers between 1 to N/2 those is divisible by each values between 1 until N/2,
#include <stdio.h>
#include <conio.h>
void main()
{
int N,i;
clrscr();
printf("\nEnter N : ");
scanf("%d",&N);
printf("\nNo. Inputed is %d ",N);
printf("\n\nFactors fo %d in Ascending order : ",N);
for(i=1;i<=N/2;i++)
{
if(N%i==0)
{
printf("\n%d ",i);
}
}
getch();
}
Perfect Numbers : Where sum of the factors of a numbers is equal to the number itself. e.g. 6 = factors are 1, 2, 3 and if we add we get 6.
#include <stdio.h>
#include <conio.h>
void main()
{
int N,i,s=0;
clrscr();
printf("\nEnter N : ");
scanf("%d",&N);
printf("\nNo. Inputed is %d ",N);
for(i=1;i<=N/2;i++)
{
if(N%i==0)
{
s=s+i;
}
}
if(N==s)
printf("\n%d is a Perfect No ",n);
else
printf("\n%d is not a Perfect No ",n);
getch();
}
Prime numbers are the number, which only divisible by itself and one only, the numbers divisible by others too are known as Composite numbers. Therefore, to get in the logic all you have to do that divide the number in check with all other number between 2 to N/2, if it has failed in divisibility check in all occasion then it is a prime else it is not a prime. Example - 5, 11 etc.
C - Prime //Progarm to Check for a Prime No.
#include <stdio.h>
#include <conio.h>
void main() //Start of main
{
int N,i,flag=0; //variables
clrscr();
printf("\nEnter Number to Check : ");
scanf("%d",&N); //input number
for(i=2;i<=N/2;i++) //start of loop
{
if(N%i==0) //checking divisibility
{
flag=1; //toggle flag 0 to 1 if failed
break;
}
}
if(flag==0)
printf("%d is a Prime No. ",N);
else
printf("%d is a Prime No. ",N);
printf("\nPress Any key to Cont...");
getch();
}
twin prime is a prime number that differs from another prime number by two. Except for the pair (2, 3), this is the smallest possible difference between two primes. Some examples of twin prime pairs are (3, 5), (5, 7), (11, 13), (17, 19), (29, 31) and (41, 43). Sometimes the term twin prime is used for a pair of twin primes; an alternative name for this is prime twin.
//Twin Prime between the ranges
#include <stdio.h>
#include <conio.h>
//Prime checking Function
int IsPrime(int n)
{
int i;
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
return 1;
}
}
return 0;
}
// Starting main
void main()
{
int i,n,n1;
clrscr();
printf("\nEnter Starting Range : ");
scanf("%d",&n);
printf("\nEnter Upper Range : ");
scanf("%d",&n1);
printf("\nList of Twin Prime Between %d to %d : ",n,n1);
for(i=n;i<=n1;i++)
{
if(IsPrime(i)==0 && IsPrime(i+2)==0)
{
printf("\n %d , %d",i,i+2);
}
}
getch();
}
Prime Factor – Print the Factors (Refers to the Program Factors) of a number those are prime too.
#include <stdio.h>
#include <conio.h>
int prime(int) ; // Function Prime() Prototype
void main()
{
int a,i,f;
clrscr();
printf("ENTER A NO.:");
scanf("%d",&a);
printf("\nPrime factor of %d is = ",a);
for(i=1;i<=a/2;i++)
{
if(a%i==0)
{
f=prime(i);
if(f==0)
printf(" %d ",i);
}
}
getch();
}
//Prime Function
int prime(int a)
{
int i;
for(i=2;i<=a/2;i++)
{
if(a%i==0)
return 1;
}
return 0;
}
Near Prime, Next Prime and Previous Prime – Input a Composite No. then find the Prime number before it, after it and the one near to it.
//Prime checking Function
#include <stdio.h>
#include <conio.h>
int isPrime(int n)
{
int i;
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
return 1;
}
}
return 0;
}
// Starting main
void main()
{
int i,n,np,pp,nrp;
clrscr();
start:
printf("\nEnter A Composite No :: ");
scanf("%d",&n);
if(isPrime(n)==0)
{
printf("\n\t Wrong Input, Enter a Composite No. ");
goto start;
}
for(i=n+1;;i++)
{
if(isPrime(i)==0)
{
np=i;
printf("\nNext Prime is %d ",np);
break;
}
}
for(i=n-1;i>1;i--)
{
if(isPrime(i)==0)
{
pp=i;
printf("\nPrevious Prime is %d ",pp);
break;
}
}
if(n-pp>np-n)
{
printf("\nNext Prime %d is the Near Prime",np);
}
else if(np-n>n-pp)
{
printf("\nPrevious Prime %d is the Near Prime ",pp);
}
else
{
printf("\nBothe are Near Prime is %d %d",np,pp);
}
getch();
}
No comments:
Post a Comment