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

Monday, 12 August 2013

series2

 

/* C program to print the following series:

*
***
*****
*******
*********

 */


Wednesday, 27 February 2013

Recursion to reverse a string




/*  C program to Reverse a string using Recursion  */



# include <stdio.h>
# include <string.h>
void revFunc(char*,int,int); // function declaration/prototype

void main()
{
   char str[25];
   int len;
   printf("www.techterabyte.com");
   printf("\nEnter string: ");

   // for reading string from stdin
   gets(str);

   // calculating length of string
   len = strlen(str);
   revFunc(str,0,len -1);
   printf("String after reversing:");
   printf("%s\n",str);
   getch();
}

void revFunc(char *x, int start, int end)
{
    char a,b,c;
    if(start >= end)
       return;

    c = *(x + start);
    *(x + start) = *(x + end);
    *(x + end) = c;

    // recursive call
    revFunc(x,++start,--end);
}




Output

www.techterabyte.com
Enter string: blog belongs to AMIT
String after reversing:TIMA ot sgnoleb golb




preprocessor define directive



/* C program to show preprocessor directive # define */



/* # define gives a name to constant value before program compilation */


# include <stdio.h>
# define A(i,j) i+j     // i+j will be inserted where we will find A(i.j)

int main()
{
   int x = 100;
   int y = 210;

  // i+j will get inserted here
   printf("%d \n", A(x, y));
   return 0;
}


Output
310



Tuesday, 19 February 2013

string-length-using-pointers




/* Program to print string length using pointers */



# include <stdio.h>
int main()
{
    char a[]="Amit";
    int l1, l2;
 
    // finding length
    l1 = functionLen(a);
    l2 = functionLen("amtdw.blogspot.in");
 
    // display string1 with it's length
    printf("string1 = %s\n", a);
    printf("length of string1 = %d\n", l1);

    // display string2 with it's length
    printf("string2 = %s\n", "techterabyte.com");
    printf("length of string2 = %d", l2);
}

// function to find length 
functionLen(char *str)
{
    int len = 0;
    // loop till end of string
    while(*str!= '\0') {
       len++;
       str++;
    }
    return(len);
}



Output:
string1 = Amit
length of string1 = 4
string2 = techterabyte.com
length of string2 = 16





Trace-of-a-MATRIX




/*  Function to find TRACE of a matrix in C Language  */


/*
TRACE of a matrix is the sum of it's diagonal elements
*/

void findTrace(int arr[][10], int M, int N)  {

    int i, j , t = 0;

    //  loop calculating trace of matric
    for ( i = 0; i < M; i++)  {
       // inner loop
       for ( j = 0;j < N;j++)   {
           // equality checking for row equal to column i.e.i==j
           if ( i==j) {
              t = trace + arr[i][j];
           }
       }
    }

    printf("Trace of matrix is = %d \n", t);
}



/* ASSUME MATRIX TO BE: */



The trace of the above shown matrix is = 2 + 3 + 4
                                                 Trace = 9



Output:
Trace of matrix is = 9







GCD-LCM-of-2-INTEGERS



/*  Program to print GCD and LCM of two integers */



# include <stdio.h>
int main()
{
    int n1, n2, gcd, lcm, rem, numerator, denominator;
 
    printf("www.techterabyte.com\n");
    // enter two integer values
    printf("Enter two integers: ");
    scanf("%d %d", &n1, &n2);
 
    // comparing both integers
    if(n1>n2) {
       numerator =  n1;
       denominator = n2;
    }
    else {
       numerator = n2;
       denominator = n1;
    }

    // find remainder
    rem = n1%n2;

    while(rem!=0) {
       numerator = denominator;
       denominator = rem;
       rem = numerator % denominator;
    }

    gcd = denominator;
    lcm = (n1*n2)/gcd;

    // display GCD and LCM
    printf("\nGreatest Common Divisor...\n");
    printf("GCD of %d and %d = %d\n", n1, n2, gcd);
    printf("Least Common Multiple...\n");
    printf("LCM of %d and %d = %d\n", n1, n2, lcm);
    return 0;
}



Output:
www.techterabyte.com
Enter two integers: 120 30
Greatest Common Divisor...
GCD of 120 and 30 = 30
Least Common Multiple...
LCM of 120 and 30 = 120





Saturday, 25 August 2012

DOSshellTURBOC

Using command prompt and accessing DOS shell in Turbo C compiler

 

 

This post is related to running a simple c/c++ program using DOS shell. 

 

All these years, we've used TurboC/C++ compiler to run our c/c++ program by using:
-> COMPILE->alt+f9
-> RUN->run+f9.

Follow the below steps to run your c/c++ program using DOS shell->
  1. Open turboc

  2. File-> New-> SaveAs-> AMIT1.C
(click on the image)



 

  3. Write any program, add two numbers program is shown here for simplicity.    



 

4.  File-> Save (or press F2)  

 

5.  Now, compile this program first to create it’s exe file.      press->alt+f9 

 

6.  The most important step, how to reach DOS shell->   

      File -> DOS shell

 7.  DOS shell will open (below)

 


 



8. Now enter->  

       AMIT1.exe (name_of_the_file.exe, as we compiled it before) , press ENTER.

      You can see the ouput here i.e. 5 + 10 = 15    

 

 

 


  11.  Write EXIT->       Press ENTER

  12. You’ll be returned to the same blue screen(where your program is visible)..
         Please give your views…Enjoy….

 


__________________________________________________________________________
Please comment and forward to others if you like this.
Other posts with video tutorial yet to come.....
"I am sure that blogs really help people around the world.
Let us follow this simple thinking of sharing knowledge . . ."

Saturday, 11 August 2012

series1

 /*  C Program to print following series:

*
**
***
****
*****

 */



 #include <stdio.h>

 int main()
   {
    int i, n , m;
   
    printf("!! www.techterabyte.com !!");

    // enter rows
    printf("\nEnter number of rows: ");
    scanf("%d\n",&n);
   
    // loop till number of rows
    for(i=1;i<=n;i++) {
   
       for(m=1;m<=i;m++) {      
           printf("*");
       }

       // new line
       printf("\n");   
    }

    return(0);
   }
  


Output

!! www.techterabyte.com !!
Enter number of rows: 5
*
**
***
****
*****

FindingSameAlphabetsClanguage

 

 

/*  Program in C language to find common Alphabets!!  */

 

/*

c1 = {'$','8','5','a','7','t','8','e'};
c2 = {'1','t','6','0','%','a','7','9'};

*/


#include <stdio.h>
int main()
 {
   // c1 and c2 character array
   char c1[10] = {'$','8','5','a','7','t','8','e'};
   char c2[10] = {'1','t','6','0','%','a','7','9'};
   int i, j;

   // looping till number of elements
   for(i=0;i<8;i++) {

       for(j=0;j<8;j++) {

         // comparing both
         if(c1[i]==c2[j]) {

             // comparing alphabets under c1 and c2
            printf("www.techterabyte.com");
             printf("Elements common in both arrays:");
             if(c1[i]>='a'&&c1[i]<='z') {

                  printf("\nElement = %c",c[i]);
             }
         }
      }
   }

   return(0);
 }



Output

www.techterabyte.com
Elements common in both arrays:
Element = a
Element = t






Friday, 10 August 2012

StringReverseClanguage

 

 

C Program to Reverse String !!!



/*  Program to reverse string  */

#include <stdio.h>
#include <string.h>
int main()
  {
      int i, len, a;
      char str[25] = "techterabyte.com";
      char revstr[25] = "\0";
     
      // finding length of string str
      len = strlen(str);
      a = len-1;

      // loop till string length
      for(i=0;i<=len;i++)   {
           revstr[a] = str[i];
           a--;
       }
      printf(" Reversed String = %s", revstr);
      return(0);
 }


Output

Reversed String = moc.etybarethcet






Thursday, 31 May 2012

C code to shutdown your computer



C language code to shutdown your computer...


This post will show a simple example to create a small shutdown application in C Programming.
The snippet shows a simple code to create .exe by compiling it under TurboC/C++ compiler.
I shut down my system in the same way by double-clicking  on  the exe file generated by compiling this program under C language.

//
// -- CODE –
//

// ***********************************************************************
// www.techterabyte.com C example!!!
// -- start of code
// header files
#include<stdio.h>
#include<stdlib.h>
void main()
{
   char choice;
   // enter  yes/no
   printf("Shutdown your computer now(y/n)?");
   printf("\t Yes:y/n),\n");
   printf("\t No:n/n) \n");
   scanf("%c",&choice);
   if( choice == 'y' || choice == 'Y' ) {
      system("C:\\WINDOWS\\System32\\shutdown -s");
   }
  getch();
}
// www.techterabyte.com C example!!!
// --end of code
// ***********************************************************************

Just compile it by using any compiler and create exe file.
I am using  Turbo C/C++ compiler

Below screenshot shows the code:

C programming - techterabyte.com
C program code to shutdown system



The screenshot shows the output which can be seen after -> Ctrl + F9
Enter y or Y and press enter--
C programming - techterabyte.com
enter value

After successfully running the program, copy the exe file from:
  •    TC\BIN\SHUTDOWN.exe
  •    To any location on the computer(or desktop)
  •    Just double click, enter y or Y
  •    Successful shutdown… 



___________________________________________________________________
Please comment and forward to others if you like this.
Other posts with video tutorial yet to come.....

"I am sure that blogs really help people around the world.
Let us follow this simple thinking of sharing knowledge . . ."