C Source Code [Program 1 - 10] 
Contents  Next Page >> 

Useful Links to C / C ++


1. /*Get and display a number*/

#include<stdio.h>

main()

{
 int age;

 printf("Enter Your AGE :");
 scanf("%d",&age);
 printf("You entered %d",age);
}

2. /* Get and display a Character */

#include<stdio.h>

main()
{
 char a;
 printf("Enter a Character:");
 a=getchar();    /* Gets a character from the Keyboard */
 printf("\n The Character is %c",a);
}

3. /* Convert a lowercase character to uppercase using a programmer defined
   function */

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

main()  {

char lower;
char ltu(char lower);

clrscr();
printf("\nEnter a lowercase character==>");
scanf("%c",&lower);
printf("\nThe uppercase equivalent is %c \n",ltu(lower));
getch();
}

char ltu(char c1)
{
char c2;
c2=(c1>='a'&&c1<='z')?('A'+c1-'a'):c1;
return(c2);
}

4./* Program to find out the first n fibonacci series numbers using
   recursive functions */

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

main()
{
int fib(int oldvalue,int new_value,int no,int index);

int n,new_,old;

clrscr();
new_=1;old=0;
printf("\nEnter the number of elements in the series==>");
scanf("%d",&n);
printf("(1)--------> %d\n",old);
printf("(2)--------> %d\n",new_);

fib(old,new_,n,2);
getch();
}

int fib(int old1,int new_1,int n1,int index)
{
int sum;
while (n1!=2) {
 sum=old1+new_1;
 printf("(%d)--------> %d\n",index+=1,sum);
 old1=new_1;
 new_1=sum;
 n1-=1;
 fib(old1,new_1,n1,index);
 return;
}
}

5. /* Program to find the factorial of a number using recursive functions */

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

main()  {

long int fact(int n);
int n,i;

clrscr();
printf("\nEnter any number==>");
scanf("%d",&n);
printf("The factorial of %d is %ld",n,fact(n));
getch();
}

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

6. /* Program to solve the Towers of Hanoi problem */

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

main()
{
   void transfer(int,char,char,char);
   int n;

   clrscr();
   printf("\nWelcome to the TOWERS OF HANOI\n\n");
   printf("\nHow many disks?==>");
   scanf("%d",&n);
   transfer(n,'L','R','C');
   getch();
}

void transfer(int n,char from,char to,char temp)
/* Transfer n disks from one pole to another
   n=no. of disks
   from=origin
   to=destination
   temp=temporary storage  */

{
   if(n>0)
   {
     transfer(n-1,from,temp,to);
     /* move n-1 disks from origin to temporary */
     printf("Move disk %d from %c to %c\n",n,from,to);
     /* move nth disk from origin to destination */
     transfer(n-1,temp,to,from);
     /* move n-1 disks from temporary to destination */
   }
   return;
}

7. /* Program to find the GCD of two numbers using recursive functions */

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

main()  {

int gcd(int firstno,int secondno);
int a,b;

clrscr();
printf("Enter any two numbers\n");
scanf("%d \n %d",&a,&b);
printf("GCD of %d and %d is %d",a,b,gcd(a,b));
getch();
}

int gcd(int a,int b)
{
  if(a<b)
     return(gcd(b,a));
     else
     if(b==0)
 return(a);
 else
 return(gcd(b,a%b)); /* 'a%b' is 'a MOD b' */
}

8. /* Roots of quadratic equations */

#include<stdio.h>
#include<conio.h>
#include<math.h>

main()  {

float a,b,c,root1,root2,d,img1,img2,r1,r2;
int choice;

clrscr();
printf("\n\t\t******* ROOTS OF QUADRATIC EQUATIONS ********\n");
printf("\nEnter the values of A,B and C:\n");
scanf("%f %f %f",&a,&b,&c);
d=b*b-4*a*c;
if(d==0)
   choice=1;
else
   if(d>0)
     choice=2;
   else
      if(d<0)
 choice=3;
switch(choice)  {
case 1:printf("\nTHE ROOTS ARE EQUAL\n");
       root1=-b/(2*a);
       root1=root2;
       break;
case 2:printf("\nTHE ROOTS ARE REAL\n");
       root1=(-b+sqrt(d))/(2*a);
       root1=(-b-sqrt(d))/(2*a);
       break;
case 3:printf("\nTHE ROOTS ARE IMAGINARY\n");
       r1=-b/(2*a);
       r2=r1;
       img1=sqrt(abs(d))/(2*a);
       img2=img1;
       break;
       }
if(choice==3)
 printf("\nROOT1=%f+i%f\tROOT2=%f-i%f",r1,img1,r2,img2);
else
 printf("\nROOT1=%f\tROOT2=%f",root1,root2);
getch();
}

9. /* To count the occurances of the word 'the' in the input stream and finally
   print the same.

   The state machine principle to solve this problem.As per the mechanism
   the program is usually in one of a finite number of states and takes a
   corresponding action based on the i/p character.For example,in this case
   we can list the possible states of the state machine as follows:

   a> Waiting for begining of a word
   b> Got a letter 't' and waiting for 'h'
   c> Got the letters 'th' and waiting for 'e'
   d> Got the letters 'the' and waiting for white space  */

#include<stdio.h>

#define WAITING  0
#define GOT_T  1
#define GOT_TH  2
#define GOT_THE  3
#define GOT_WRONG 4
#define WHITE(c) ((c==' ')||(c=='\t')||(c=='\n'))
#define END        '.'

main() {

 int state=WAITING;
 int c;
 int the_count=0;

 clrscr();
 printf("Program to count the no. of occurances of the word 'the' in the I/P stream\n");
 printf("Enter a line of data,ending with a '.' ==>\n");

 while ((c=getchar())!=END) {
   switch(state) {

     case WAITING:
       if ((c=='t')||(c=='T')) state=GOT_T;
       else if (WHITE(c)) continue;
       else state=GOT_WRONG;
       break;

     case GOT_T:
       if ((c=='h')||(c=='H')) state=GOT_TH;
       else if (WHITE(c)) state=WAITING;
       else state=GOT_WRONG;
       break;

     case GOT_TH:
       if ((c=='e')||(c=='E')) state=GOT_THE;
       else if (WHITE(c)) state=WAITING;
       else state=GOT_WRONG;
       break;

     case GOT_THE:
       if (WHITE(c)) {
 the_count++;
 state=WAITING;
       }
       else state=GOT_WRONG;
       break;

     case GOT_WRONG:
       if (WHITE(c)) state=WAITING;
       break;
     }
   }
   printf("Number of occurances of word 'the' in input=%d\n",the_count);
   getch();
 }

10. /* Program to check if the input word,pharse or sentence is a palindrome */
#include<stdio.h>
#include<conio.h>

#define EOL '\n'
#define TRUE  1
#define FALSE 0

main()  {

int count,tag,countback,flag,loop=TRUE;
char letter[80];

clrscr();
while (loop) {
 flag=TRUE;
 printf("\nPROGRAM TO CHECK FOR A PALINDROME\n");
 printf("\nPlease enter a word,pharse or sentence below\n");
 for (count=0;(letter[count]=getchar())!=EOL;++count)
 if ((letter[0]=='E'||letter[0]=='e')&&(letter[1]=='N'||letter[1]=='n')
 &&(letter[2]=='D'||letter[2]=='d')) { loop=FALSE;break;}
 tag=count-1;

 for ((count=0,countback=tag);count<=tag/2;(++count,--countback))  {
  if (letter[count]!=letter[countback]) {
   flag=FALSE;
   break;
  }
 }

 for (count=0;count<=tag;++count)
  putchar(letter[count]);
  if (flag) printf(" IS A PALINDROME");
  else printf(" IS NOT A PALINDROME");
 }
} 


Useful Links to C/C++

Contents 

Next Page >> 

© Copyrights Madhu Sudan Rao G.K  

[CodeEverywhere.Com]