Open Means Open Means

C Program to compare two numbers.....

No comments on “C Program to compare two numbers.....”

This program takes two numbers as inputs and compares them to show which one is greater.

 

#include                                                             // Header file

#include                                                           // Header file

void main()                                                                        // Main function

{

int a,b flag;                                                                          // Define variables

flag=2;                                                                                 // Initialize variables

clrscr()                                                                                 // Clear screen

printf(" Enter the first number: \n");

scanf("%d",&a);                                                                 // Input First number

printf(" Enter the second number: \n");

scanf("%d",&b);                                                                  // Input second number

if ( a>b)

flag=1;

else if (a

flag=0;

if( flag==1)

printf( "%d is greater than %d ", a,b);

else if(flag==0)

printf( "%d is greater than %d ", b,a);

else

printf(" Both are equal ");

getch();

}

*************************************************** OUTPUT ***************************************************

Enter the first number :10

Enter the second number:12

12 is greater than 10

Demonstration of Fibonacci Series Using Recurssion

No comments on “Demonstration of Fibonacci Series Using Recurssion”
Inlcude required header files
#include<iostream.h>
#include<conio.h>
Function Declaration
void fibonacci(long int i,long int j);
Instance Variables Declaration
static int totNum;
static int count;
Main Method which uses the recursive function to print fibonacci series
void main(){

Local variables declaration

long int i=0,j=1;
clrscr();

Accept the max number of series

cout<<"Enter The Limit:";
cin>>totNum;

Print initial numbers

cout<<endl<<i<<" "<<j<<" "; 

Track count, initialize to 2, as the starting two numbers are already printed.

count=2;

Recurse through function to print series

fibonacci(i, j);
getch();
}

Now the actual funtionality to get the fibonacci number reccursively.

void fibonacci(long int i,long int j){
// Print next number
cout<<i+j<<" ";

// Increase the count
count++;

Check if count reaches the max number else recurse.

 if(count<=totNum){
fibonacci(j,i+j);
}
}

Demonstration of Selection Sort on Character Array

No comments on “Demonstration of Selection Sort on Character Array”

/* Include required header files */

#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<conio.h>

Selection sort method. This method accepts pointer to the character array which contain the characters to be sorted
and the length of the array.

void selsort(char *items,int count){

// Local variable declaration
int a,b,c;
int exchange;
char t;

// Loop over the array of characters
for(a
=0;a<count-1;++a){

exchange=0;
c=a;
t=items[a];


Test and swap the current character with the next position till the next character is less than the current one. In a way to say move the character to its position in the present iteration of sorting. The continue with the next character and so on... For example, consider the word "sortme" In the first iteration the character 's' will be moved to the third position by comparing with each character until the character 't' founds, which is greater than 's'.

for(b=a+1;b<count;++b){
if(items[b]<t){
c=b;
t=items[b];
exchange=1;
}
}
if(exchange){
items[c]=items[a];
items[a]=t;
}
}
}

Now use the above methods to sort the characters of a string.

void main(){
clrscr();
char s[30];
cout<<"Enter A String:";
gets(s);
selsort(s,strlen(s));
cout<<"The String After Selection Sort:"<<s;
getch();
}

  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

Page 21 of 21

  • About Us
  • Faqs
  • Contact Us
  • Disclaimer
  • Terms & Conditions