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

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');
}








CONVERSION OF DECIMAL TO BINARY

/*convert decimal to binary */
#include<stdio.h>
#include<conio.h>
void main()
{
int a=1,rem,bin=0,num;
clrscr();
printf("Enter decimal number :");
scanf("%d",&num);
while(num>0)
{
rem=num%2;
bin+=(rem*a);
num/=2;
a*=10;
}
printf("binary =%d",bin);
getch();
}

CONVERSION OF BINARY TO DECIMAL

/*convert binary to decimal */
#include<stdio.h>
#include<conio.h>
void main()
{
int j=1,dec=0,d,n,rem;
clrscr();
printf("Enter a binary number :");
scanf("%d",&n);
while(n>0)
{
rem=n%10;
d=rem*j;
dec+=d;
j*=2;
n/=10;
}
printf("decimal number is = %d",dec);
getch();
}

PRODUCT OF DIGITS

 /*find the product of digits */
 #include<stdio.h>
 #include<conio.h>
 void main()
 {
int rem,prod=1,n;
clrscr();
printf("Enter a integer :");
scanf("%d",&n);
while(n>0)
{
rem=n%10;
prod*=rem;
n/=10;
}
printf("factorial %d",prod);
getch();
 }

FACTORIAL : (Example 2)

/*find the factorial using funtion calling */
#include<stdio.h>
#include<conio.h>
long int fact(int);
void main()
{
int n;
long int a;
clrscr();
printf("Enter a number :");
scanf("%d",&n);
a=fact(n);
printf("factorial %ld ",a);
getch();


}
long int fact(int n)
{
if(n==0)
return (1);
else
return (n*fact(n-1));
}

FACTORIAL : (Example 1)

/*find the factorial  of a number */
#include<stdio.h>
#include<conio.h>
void main()
{
int n,num;
long fact=1;
clrscr();
printf("Enter the number :");
scanf("%d",&n);
num=n;
if(n<0)
printf("No factorial of -ve number \n");
else
{
while(n>1)
{
fact*=n;
n--;
}
printf("factorial of %d =%ld",num,fact);
}
getch();
}