google-site-verification: googlef8aa845b858144d3.html COMPUTER SCIENCE NOTES

Monday, July 17, 2017

PRIME NUMBER (Example 3)

/* 3 program of prime number Note=> 1 is not prime number */
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,n,flag=0;
clrscr();
printf("Enter a number:");
scanf("%d",&n);
for(i=2;i<=sqrt(n);i++)
{
if(n%i==0)
flag=1;
}
if(flag==1)
printf("Not prime number ");
else
printf("Prime number ");
getch();
}

PRIME NUMBER (Example 2)

/* 2 program of prime number and check from 2and so on */
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
clrscr();
printf("Enter a number :");
scanf("%d",&n);
i=2;
while(i<=n-1)
{
if(n%i==0)
{
printf("Not a prime number ");
break;
}
i++;
}
if(i==n)
printf("prime number");
getch();
}

PRIME NUMBER (Example 1)

/* 1 program of prime number */
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,c=0;
clrscr();
printf("Enter a number :");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
c++;
}
if(c==2)
printf("Prime Number ");
else
printf("Not Prime Number");
getch();
}

GENERATE FIBONACII SERIES

/* program  to generate fibonacii series
1,1,2,3,5,8,13,34,........
in this series each number is a sum of the previous two numbers */
#include<stdio.h>
#include<conio.h>
void main()
{
long int x,y,z;
int i,n;
x=0;
y=1;
printf("Enter the number of terms :");
scanf("%d",&n);
printf("%ld ",y);
for(i=0;i<n;i++)
{
z=x+y;
printf("%ld ",z);
x=y;
y=z;
}
printf("\n");
getch();
}

Sum of series 1+2+3+... using formula

/*find the sum of series 1+2+3+... using formula */
#include<stdio.h>
#include<conio.h>
void main()
{
int num,sum=0;
clrscr();
printf("Enter index of series from like (1 to 10)");
scanf("%d",&num);
sum=num*(num+1)/2;
printf("sum of series %d",sum);
getch();
}

COUNT DIGIT OF ANY NUMBER

/*count the digit of any number */
#include<stdio.h>
#include<conio.h>
void main()
{
int num,count=0;
clrscr();
printf("Enter number :");
scanf("%d",&num);
do{
num/=10;
count++;
}while(num>0);

printf("digit =%d",count);
getch();
}

CONVERT ALL INTEGER INTO ANY BASE

/*convert all integer into any base */
#include<stdio.h>
#include<conio.h>
void convert(int,int);
void main()
{
int num,base;
clrscr();
printf("Enter a integer :");
scanf("%d",&num);
printf("\nEnter base :");
scanf("%d",&base);
convert(num,base);
getch();
}
void convert(int num, int base)
{
int rem=num%base;
if(num==0)
return ;
convert(num/base ,base);
if(rem<10)
printf("%d",rem);
else
printf("%c",rem-10+'A');
}