Showing posts with label C Programs. Show all posts
Showing posts with label C Programs. Show all posts

Saturday, December 5, 2015

Pyramid Programs in C - Type #2

Program #6

Description:
Pyramid Type #2

#include "stdio.h"
#include "conio.h"
int main() {
 int i,j,k,n;

 /* clear the screen */
 clrscr();

 printf(" Enter a number to make Type #2 Pramid:\n ");
 scanf("%d", &n);

 /* Validate input */
 if (n < 2 || n > 10){
   return 0;
 }

 /* Make some space between input and Pyramid */
 printf("\n\n");

 for (i=1; i<=n; i++) {
   /* move cursor to center to make pyramid view */
   for (k=1; k<=(n-i); k++) printf(" ");

   for (j=1; j<=i; j++){
     /* display only first and last number in every row */
     if (j==1 || j==i){
 printf("%d ", i);

     } else if (i==n){
 /* print all remaining number in last row to complete Pyramid */
 printf("%d ", i);
     } else if (j<i){

 /* print blank space to avoid pyramid shape */
 printf("  ");
     }

   }
   /* Move to next line */
   printf("\n");
 }

 /* Wait after print Pramid */
 getch();
 return 1;
}



 

Pyramid Program in C - Type #1


Program #5

Description:
Pyramid Type #1

#include "stdio.h"
#include "conio.h"


int main() {
 int i,j,k,n;

 /* clear the screen */
 clrscr();

 printf(" Enter a number to make Type #1 Pramid:\n ");
 scanf("%d", &n);

 /* Validate input */
 if (n < 2 || n > 10){
   return 0;
 }

 /* Make some space between input and Pramid */
 printf("\n\n");

 for (i=1; i<=n; i++) {
   /* move cursor to center to make pramid view */
   for (k=1; k<=(n-i); k++) printf(" ");

   for (j=1; j<=i; j++){
     printf("%d ", i);
   }

   /* Move to next line */
   printf("\n");
 }

 /* Wait after print Pramid */
 getch();
 return 1;
}



Custom program of clrscr using gotoxy

Program #4

Description:
In this program, it will clear the screen and move the cursor to position 1,1 (1st row and 1st column)

Custom Header file: i.e. "extnc.h"

/* Custom function will do the same functionality of clrscr(); with help of gotoxy() */
#include <conio.h>
void extnclrscr(){
int i,j;
 for (i=1;i<=25;i++){
   for (j=1;j<=80;j++){
     printf(" ");
   }
   printf("\n");
 }

 /* Move cursor to 1st Row and 1st Column */
 gotoxy(1,1);
}


Main C Program file: i.e. "custclr.c"

#include<conio.h>
#include "extnc.h"
int main(){

/* Clear screen */
extnclrscr();

printf("Screen Cleared !!");
getch();

return 0;
}



 
 Note: If you notice in the main function "conio.h" included.  Is it correct?  In "extnc.h" file already "conio.h" included.  Now we are including the same header file once again.  Why the c compiler accepting this?.  Yes, It is not a issue.  Header files are local scope.

Thursday, December 3, 2015

Custom Header File in C

Program #3

Code inside custom header file i.e. "extn.h"

/* Custom function will do the same
functionality of clrscr(); */
void extnclrscr(){
 for (int i=1;i<=25;i++){
   for (int j=1;j<=80;j++){
     printf(" ");
   }
   printf("\n");
 }
}



Code inside main c file i.e. "customcl.c"

#include<stdio.h>
#include<conio.h>
#include "extnc.h"
int main(){
  void extnclrscr(void);

  /* Calling custom clrscr() */
  extnclrscr();

  printf("Welcome to Custom clrscr() program with the help of Custom Header file!!");
  getch();
  return 0;
}



Why should I Use Custom Header file:
You can keep your custom functions in this header file and we don't need to depend on OOB functions in C library.  In case you are planning to make a large program/project using C code this header will do a vital role.  During this time you will feel this difference.

Just like some common calculation, redundant functions which is required in many places.  In that circumstance you can opt in Custom Header file.

Before link the header (extn.h) file into main (customcl.c) file.  Compile the header file once.  Why we need to compile?  It is required to compile the file.  If the header file is not compiled then main function will not able to process and run as whole file.

Custom clrscr() function in C

Program #2

#include<stdio.h>
#include<conio.h>
int main(){
  void extnclrscr(void);
  /* Calling custom clrscr() */
  extnclrscr();

  printf("Welcome to Custom clrscr() program!!");
  getch();
  return 0;
}
/* Custom function will do the same
functionality of clrscr(); */
void extnclrscr(){
 for (int i=1;i<=25;i++){
   for (int j=1;j<=80;j++){
     printf(" ");
   }
   printf("\n");
 }
}

Fibonaccis Series

Program #1:
Fibonaccis Series

#include<stdio.h>
#include<conio.h>
int main(){
 int i,j,k;
 clrscr();
 printf("Please enter Nth value for Fibonacci's Series:\n");
 scanf("%d", &k);
 /* Validate input data */
 if (k > 24 || k < 3) {
   printf("Please enter validate data next time!!\nBye c u later");
   getch();
   return 0;
 }
 /*
 Initialize first two for Fibonaccis series values
 */
 i = 0;
 j = 1;
 printf("\nSeries 1:0");
 printf("\nSeries 2:1");
 for (int m=3; m<=k; m++){
   int r = i+j;
   i = j;
   j = r;
   printf("\nSeries %d:%d", m,r);
 }
 getch();
 return 0;
}

Explanation:
If users gives Fibonaccis series value greater than 24 then the result will cross 32768.  What 32768? it means maximum limit of int storage limit.  Because currently i, j and r variables as integer.  Integer storage limit is 2 byte i.e. 2 to the power 15 i.e. 32768.

If you notice, variable m and r are not declared on top of the program.  But how it is working?  In C program, it is not mandatory to declare the variables on top of the program.  You can declare it where ever you wish.  So, in this program declared inside the code.

If user gives, Fibonaccis series value as 28 (for example) then program will display error message and quit the execution using return 0.  So, how a C program can have two return 0??.  Yes it is possible.  You can add as many return 0 as you wish.  It will quit and stop the execution.

Popular Posts