google-site-verification: googlef8aa845b858144d3.html COMPUTER SCIENCE NOTES: 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();
}

Friday, March 3, 2017

Leap Year: (Example 2) This program checks whether year is a leap year ...


/* program to find whether a year is leap or not */
#include<stdio.h>
#include<conio.h>
void main()
{
int year;
printf("Enter year :");
scanf("%d",&year);
if(year%100==0)
{
if(year%400==0)
printf("Leap year \n");
else
printf("Not leap year \n");
}
else
{
if(year%4==0)
printf("Leap year \n");
else
printf("Not leap year \n");
}
getch();
}




Leap Year: (Example 1)C program to check if a given year is leap year or...




/*find a year is lear or not */
#include<stdio.h>
#include<conio.h>
void main()
{
int year;
clrscr();
printf("Enter a year : ");
scanf("%d",&year);
if(year%4==0 &&(year%100!=0 || year%400==0))
printf("This is leap year ");
else
printf("This is not leap year ");
getch();
}

Largest:(Example 3)C Program to Find the Largest Number Among Three Numb...

/* 3 program of largest number from given 3 numbers */
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter 3 number a, b and c :\n");
scanf("%d %d %d",&a,&b,&c);
if(a>b && a>c)
puts("a is the greatest");
else if(b>c)
puts("b is the greatest");
else
puts("c is the greatest");
getch();
}


Largest: (Example 2) C Program to Find the Largest Number Among Three Nu...

/*program to find largest number from three given numbers */
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,larger;
clrscr();
printf("Enter three numbers :\n");
scanf("%d %d %d",&a,&b,&c);
if(a>b)
{
if(a>c)
larger=a;
else
larger=c;
}
else
{
if(b>c)
larger=b;
else
larger=c;
}
printf("Larger number is %d ",larger);
getch();
}





Largest: (Example 1)C Program to Find the Largest Number Among Three Num...

/*find the largest number from give 3 numbers */
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter 3 numbers a,b and c:\n");
scanf("%d %d %d",&a,&b,&c);
printf("largest number is : %d",(a>b?(a>c?a:c):(b>c?b:c)));
getch();
}

//thanx



Thursday, March 2, 2017

Swapping: (Example 5) Swapping of two numbers using call by reference

/* 5 program of swap 2 number */
#include<stdio.h>
#include<conio.h>
void swap(int *,int *);
void exchange(int **, int *);
void main()
{
int c,d;
clrscr();
printf("Enter the value of c and d:\n");
scanf("%d %d",&c,&d);
swap(&c,&d);
printf("value of c=%d \nvalue of d=%d",c,d);
getch();

}
void swap(int *cc, int *dd)
{
exchange(&cc,dd);
}
void exchange(int **cc, int *dd)
{
int t;
t=**cc;
**cc=*dd;
*dd=t;
}

Swapping: (Exapmle 4) Algorithm for swapping of two numbers using pointers.

/* 4 program of swap 2 number */
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,temp,*ptr1,*ptr2;
clrscr();
printf("Enter value of a and b:\n");
scanf("%d %d",&a,&b);
ptr1=&a;
ptr2=&b;

temp=*ptr1;
*ptr1=*ptr2;
*ptr2=temp;

printf("value of a is %d and value of  b is %d",a,b);
getch();
}




Swapping: (Example 3) Write C program to swap two numbers using third va...

/* 3 program of swap 2 number */
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,temp;
clrscr();
printf("Enter the value of a:");
scanf("%d",&a);
printf("\nEnter the value of b:");
scanf("%d",&b);

temp=a+b;
b=temp-b;
a=temp-a;

printf("Value of a =%d\nValue of b =%d",a,b);
getch();
}





Swapping: (Example 2) Write a c program to swap two numbers without usin...

/* 2 program of swap 2 number */
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter 2 number a and b:\n");
scanf("%d %d",&a,&b);

a=a+b;
b=a-b;
a=a-b;
printf("value of a=%d\nvalue of b=%d ",a,b);
getch();
}


Swapping: (Example 1) C program to swap two numbers using third variable...

/* 1 program of swap 2 number */
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,t;
clrscr();
printf("Enter a number for a :");
scanf("%d",&a);
printf("Enter a number for b :");
scanf("%d",&b);
t=a, a=b, b=t;
printf("value of a=%d\nvalue of b=%d",a,b);
getch();
}








Even Odd: (Example 5) Write a program to check whether the given number ...

  1. /* 5 program of even and odd number */
  2. #include<stdio.h>
  3. #include<conio.h>
  4. void main()
  5. {
  6. int n,quotient;
  7. clrscr();
  8. printf("Enter a number :");
  9. scanf("%d",&n);
  10. quotient=n/2;
  11. if(quotient*2==n)
  12. printf("even number ");
  13. else
  14. printf("odd number");
  15. getch();
  16. }

Even Odd: (Example 4) Write a program to check whether the given number ...



/* 4 program of even odd number */

#include<stdio.h>

#include<conio.h>

void main()

{

int n,a;

clrscr();

printf("Enter a number :");

scanf("%d",&n);

a=n<<(8*sizeof(int)-1);

if(a==0)

printf("even number");

else

printf("odd number");

getch();

}

Even Odd: (Example 3) Write a program to check whether the given number ...



 /* 3 program of even and odd number */

 #include<stdio.h>

 #include<conio.h>

 void main()

 {

int n;

clrscr();

printf("Enter a number :");

scanf("%d",&n);

if((n*n)%2==0)

printf("even number");

else

printf("odd number");

getch();

 }

Even Odd: (Example 2) Write a program to check whether the given numbe...

/* 2 program of even and odd */
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("Enter a number :");
scanf("%d",&n);
if((n&1)==1)
printf("odd number");
else
printf("even number");
getch();
}

Even Odd: (Example 1) Write a program to check whether the given number ...


/* 1 program of even and odd */
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("Enter a number :");
scanf("%d",&n);
if(n%2==0)
printf("even number");
else
printf("odd number");
getch();
}

Monday, February 27, 2017

Prim's Algorithm



How does Prim’s Algorithm Work? The idea behind Prim’s algorithm is simple,
a spanning tree means all vertices must be connected.
So the two disjoint subsets (discussed above) of vertices must be connected to make a Spanning Tree.
And they must be connected with the minimum weight edge to make it a Minimum Spanning Tree.
// A C / C++ program for Prim's Minimum Spanning Tree (MST) algorithm.
// The program is for adjacency matrix representation of the graph
 
#include <stdio.h>
#include <limits.h>
 
// Number of vertices in the graph
#define V 5
 
// A utility function to find the vertex with minimum key value, from
// the set of vertices not yet included in MST
int minKey(int key[], bool mstSet[])
{
   // Initialize min value
   int min = INT_MAX, min_index;
 
   for (int v = 0; v < V; v++)
     if (mstSet[v] == false && key[v] < min)
         min = key[v], min_index = v;
 
   return min_index;
}
 
// A utility function to print the constructed MST stored in parent[]
int printMST(int parent[], int n, int graph[V][V])
{
   printf("Edge   Weight\n");
   for (int i = 1; i < V; i++)
      printf("%d - %d    %d \n", parent[i], i, graph[i][parent[i]]);
}
 
// Function to construct and print MST for a graph represented using adjacency
// matrix representation
void primMST(int graph[V][V])
{
     int parent[V]; // Array to store constructed MST
     int key[V];   // Key values used to pick minimum weight edge in cut
     bool mstSet[V];  // To represent set of vertices not yet included in MST
 
     // Initialize all keys as INFINITE
     for (int i = 0; i < V; i++)
        key[i] = INT_MAX, mstSet[i] = false;
 
     // Always include first 1st vertex in MST.
     key[0] = 0;     // Make key 0 so that this vertex is picked as first vertex
     parent[0] = -1; // First node is always root of MST
 
     // The MST will have V vertices
     for (int count = 0; count < V-1; count++)
     {
        // Pick the minimum key vertex from the set of vertices
        // not yet included in MST
        int u = minKey(key, mstSet);
 
        // Add the picked vertex to the MST Set
        mstSet[u] = true;
 
        // Update key value and parent index of the adjacent vertices of
        // the picked vertex. Consider only those vertices which are not yet
        // included in MST
        for (int v = 0; v < V; v++)
 
           // graph[u][v] is non zero only for adjacent vertices of m
           // mstSet[v] is false for vertices not yet included in MST
           // Update the key only if graph[u][v] is smaller than key[v]
          if (graph[u][v] && mstSet[v] == false && graph[u][v] <  key[v])
             parent[v]  = u, key[v] = graph[u][v];
     }
 
     // print the constructed MST
     printMST(parent, V, graph);
}
 
 
// driver program to test above function
int main()
{
   /* Let us create the following graph
          2    3
      (0)--(1)--(2)
       |   / \   |
      6| 8/   \5 |7
       | /     \ |
      (3)-------(4)
            9          */
   int graph[V][V] = {{0, 2, 0, 6, 0},
                      {2, 0, 3, 8, 5},
                      {0, 3, 0, 0, 7},
                      {6, 8, 0, 0, 9},
                      {0, 5, 7, 9, 0},
                     };
 
    // Print the solution
    primMST(graph);
 
    return 0;
}