Saturday, 5 April 2014

How to Convert an Upper Case String to a Lower Case?

To convert an upper case string to a lower case, I am using arrays along with for loop. This is an easy method you can do it very easily.

 

#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
    clrscr();
    char mystring[30];
   int i;

   printf("\nEnter any String in UPPER Case:\n\n");
   scanf("%s",&mystring);

   printf("\nYour Entered String is: %s",mystring); 
   for(i=0;i<=strlen(mystring);i++)
   {
          if(mystring[i]>=97 && mystring[i]<=122)
          mystring[i]=mystring[i]+32;
   }
   printf("\nThe string in LOWER CASE ::\n%s",mystring);
   getch();
}

How to Convert Lower Case String to Upper Case?

To convert a lower case string to an upper case, I am using arrays along with for loop. This is an easy method you can do it very easily.

 

#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
    clrscr();
    char mystring[30];
   int i;

   printf("\nEnter any String in Lower Case:\n\n");
   scanf("%s",&mystring);

   printf("\nYour Entered String is: %s",mystring); 
   for(i=0;i<=strlen(mystring);i++)
   {
          if(mystring[i]>=97 && mystring[i]<=122)
          mystring[i]=mystring[i]-32;
   }
   printf("\nThe string in UPPER CASE ::\n%s",mystring);
   getch();
}

How to Concatenate Two Strings with STRCAT

Like numbers you can also concatenate strings or paragraphs. There are several methods for concatenation but I am simplifying two out of all. One is with strcat function and other is by using arrays.

/*Concatenate Two Strings with StrCat Function*/

#include<stdio.h>
#include<conio.h>
main()
{
    clrscr();
    char a[100], b[100];
    printf("\nEnter First String:\n");
    gets(a);
    printf("\nEnter Second String:\n");
   gets(b);

   strcat(a,b);
   printf("\nString After Concatenation:: \n\n%s",a);
  getch();
}

/*Concatenate Two Strings without StrCat*/

 #include<stdio.h>
 #include<string.h>
 #include<conio.h>
int main()
{
  clrscr();
  int i=0,j=0;
  char str1[20],str2[20];
  puts("Enter first string");
  gets(str1);
  puts("Enter second string");
  gets(str2);
  printf("Before concatenation the strings are\n");
  puts(str1);
  puts(str2);
  while(str1[i]!='\0'){
      i++;
  }
  while(str2[j]!='\0'){
      str1[i++]=str2[j++];
  }
  str1[i]='\0';
  printf("After concatenation the strings are\n");
  puts(str1);
 return 0;
}

Friday, 4 April 2014

How to Check a Number is Even or Odd?

It is very simple to check that a given number is even or odd by dividing the number by 2. But it is not possible to check by division every number. So it is very easy that we can do it by writing a C Program with different methods.

/*Check Even or ODD with Modulus Operator*/

#include<stdio.h>
#include<conio.h>
main()
{
    clrscr();
    int numb;

    printf("\nPlease Enter Number:= ");
    scanf("%d",&numb);

    if(numb%2==0)
    {
         printf("\nEven Number");
    else
         printf("\nOdd Number");
    }
getch();
}

/*Check Even or ODD with Bitwise Operator*/

#include<stdio.h>
#include<conio.h>
main()
{
    clrscr();
    int numb;

    printf("\nPlease Enter Number:= ");
    scanf("%d",&numb);

    if(numb & 1==1)
    {
         printf("\nOdd Number");
    else
         printf("\nEven Number");
    }
getch();
}

/*Check Even or ODD with Conditional Operator*/

#include<stdio.h>
#include<conio.h>
main()
{
    clrscr();
    int numb;

    printf("\nPlease Enter Number:= ");
    scanf("%d",&numb);

    numb%2==0 ? printf("\nEven") : printf("\nODD");
    return 0;
}

If you have programming skills then you can develop other methods of finding whether given number is even or odd. And if you have any queries do get back to me, I will respond to your queries within 24 hours with the positive solution.

How To Swap Two Numbers in c Language Program?

Swapping two numbers in C Language is very easy. There are 5 (Five) ways to swap two numbers, you can choose anyone you like. I am mentioning  all one by one below. 
  1. Swap Numbers With Temp (Third) Variable

  2. Swap Numbers Without Temp (Third) Variable

  3. Swap Numbers With Pointers

  4. Swap Numbers With Call By Reference

  5. Swap Numbers With bitwise XOR

Method One  //swap two numbers with third variable

#include<stdio.h>
main()
{
    int valX, valY, valTemp;
    printf("\nEnter the value of X:= ");
    scanf("%d",&valX);
    printf("\nEnter the value of Y:= " );
    scanf("%d",&valY);

    printf("\nBefore Swap\nX=%d\nY=%d",valX,valY);

    valTemp=valX;
    valX=valY;
    valY=valTemp;

    printf("\nAfter Swap\nX=%d\nY=%d",valX,valY);
}

Output of above program is as pasted below: 

Output of Swapping Values





Method Two   //swap two numbers without third variable

#include<stdio.h>
#include<conio.h>
main()
{
     clrscr();
     int x,y;
    printf("\nEnter the value of X:= ");
    scanf("%d",&x);

    printf("\nEnter the value of Y:= " );
    scanf("%d",&y);

    x=x+y;
    y=x-y;
    x=x-y;
    
    printf("\nValues of X, Y After Swap Are:-\n");
    printf("\nValue of  X=%d\nValue of Y=%d",x,y);
    getch();
}

Method Three     //swap two numbers with pointers

#include <stdio.h>
#include <conio.h>
swap(int *valX , int *valY)
{
    int temp;

    temp=*valX;
    *valX=*valY;
    *valY=temp;
}
main()
{
    int a,b;
    clrscr();
    printf("Enter value of A= ");
    scanf("%d", &a);

    printf("\nEnter number B= ");
    scanf("%d", &b);

    printf("\nBefore Swapping\n");
    printf("\nValue of A:= %d ",a);
    printf("\nValue of B:= %d ",b);

    swap(&a,&b);
    printf("\nAfter Swapping\n");
   
printf("\nValue of A:= %d ",a);
    printf("\nValue of B:= %d ",b);

    getch();
}

Method Four  //swap two numbers with call byreference

#include <stdio.h>
#include<conio.h>
void swap(int*, int*);
int main()
{
   clrscr();
   int x, y;

   printf("\nEnter the value of X= %d",x);
   scanf("%d",&x);
   printf("Enter the value of Y= %d",y);
   scanf("%d",&y);

   printf("Before Swapping\nX = %d\nY = %d\n", x, y);
   swap(&x, &y);
   printf("After Swapping\nX = %d\nY = %d\n", x, y);
   return 0;
}
void swap(int *a, int *b)
{
   int temp;
   temp = *b;
   *b   = *a;
   *a   = temp;
}

How to Divide Two Numbers in C Language

Division in C Programs is very easy as it is in Addition, subtraction and multiplication because pattern is same and also syntax is same. Just one operation is different and that is division. I have put all my efforts while creating this blog. However there are lots of other blogs websites are available for online learning of C Language programs but almost all are based on same complex method of teaching. But my teaching method id quite different as you can see

C Program to Divide Two Numbers

#include<stdio.h>
main()
{
     int var1,var2,answer;
     printf("\nEnter First Value:= ");
     scanf( "%d",&var1);

     printf("\nEnter Second Value:= ");
     scanf( "%d",&var2);

     answer=var1/var2;
     printf("\Answer Is:= %d ",answer);
}

Output of the program

Output of Division

How to Multiply Two Numbers in C Program

Multiplication in C Programs is very easy as it is in Addition, subtraction because pattern is same and also syntax is same. Just one operation is different and that is multiply. I have put all my efforts while creating this blog. However there are lots of other blogs websites are available for online learning of C Language programs but almost all are based on same complex method of teaching. But my teaching method id quite different as you can see

C Program to Multiply Two Numbers

#include<stdio.h>
main()
{
     int var1,var2,answer;
     printf("\nEnter First Value:= ");
     scanf( "%d",&var1);

     printf("\nEnter Second Value:= ");
     scanf( "%d",&var2);

     answer=var1*var2;
     printf("\Answer Is:= %d ",answer);
}

Output of the program

Output of Multiply



How to Subtract Two Numbers in C Language Program

Subtraction in C Programs is very easy as it is in Addition because both are on same pattern and syntax for both is same. Just one operation is different and that is minus. I have put all my efforts while creating this blog. However there are lots of other blogs websites are available for online learning of C Language programs but almost all are based on same complex method of teaching. But my teaching method id quite different as you can see

C Program to Subtract Two Numbers

#include<stdio.h>
main()
{
     int var1,var2,answer;
     printf("\nEnter First Value:= ");
     scanf( "%d",&var1);

     printf("\nEnter Second Value:= ");
     scanf( "%d",&var2);

     answer=var1-var2;
     printf("\Answer Is:= %d ",answer);
}

Output of the program

Output for Subtraction

Thursday, 3 April 2014

How to Add Two Numbers in c Language Using Different Methods?

This c language program is used to add two numbers. It uses the basis arithmetic operation for adding two numbers and then prints the sum on output screen. It is as simple as your First C Language Program.

C Language Program to Enter 2 numbers by entering values

#include<stdio.h>
main()
{
   int a, b, c;   //Declaration of variables
   printf("Enter two numbers to add\n");
   scanf("%d%d",&a,&b);

   c = a + b; 
//Addition of variable
printf("Sum of entered numbers = %d\n",c);
}

Below is the output of the above program.





Program to ADD 2 numbers by Declaring values

#include<stdio.h>
main()
{
   int a = 1, b = 2;

   /* Storing result of addition in variable a */

   a = a + b;
   printf("Sum of a and b = %d\n", a);
}






C program to add two numbers repeatedly


#include<stdio.h>

main()
{
   int a, b, c;
   char ch;

   while(1)
   {
      printf("Enter values of a and b\n");
      scanf("%d%d",&a,&b);

      c = a + b;

      printf("a + b = %d\n", c);

      printf("Do you wish to add more numbers(y/n)\n");
      scanf(" %c",&ch);

      if ( ch == 'y' || ch == 'Y' )
         continue;
      else
        break;
   }

   return 0;
}


Adding numbers in c using function


#include<stdio.h>
 long addition(long, long);
 main()
{
   long first, second, sum;
   scanf("%ld%ld", &first, &second);
   sum = addition(first, second);
   printf("%ld\n", sum);
   return 0;
}
long addition(long a, long b)
{
   long result;
   result = a + b;
   return result;
}



I have used long data type as it can handle large numbers, if you want to add still larger numbers which doesn't fit in long range then use array, string or other data structure.

How to Write First Program in C Language?

If you have heard or read about the history and syntax of programming in C Language then you might be able to write a first program to print welcome message on output screen.

SYNTAX OF C LANGUAGE

Before you write any c language program, you must learn it syntax i.e. where to add comments, semi column, braces, header files etc. You must try to learn about its syntax vertically as it is describe below:

1. Header Files
2. Main Function
3. Start Curly Brace
4. PrintF Function (if printing on screen is required)
5. Scanf Function (if value entering is required)
6. Close Curly Brace
7. Comments, where is it required to tell anything important

/*This is my first program to print Welcome Message*/
/*Program is written by RapidForceAds*/
#include<stdio.h>
main()
{
       printf(“Welcome to C Language\n”);
       printf(“This is the output of your First Program”);
}


When you compile and run this program then it will show an output console with the output of this program.



Output Screen

What is C Programming ?

In computing, 'C' is a general purpose programming language and initially developed by Dennis Ritchie between the year 1969 and 1973 at AT&T Bell Laboratories. Like most other languages, C has facilities for structured programming and allows lexical variable scope and recursion, while a static type system prevents many unintended operations. Its design provides constructs that map efficiently to typical machine instructions, and therefore it has found lasting use in applications that had formerly been coded in assembly language, most notably system software like the Unix computer operating system.


If you notice carefully, you will see that 'C' is one of the most widely used programming languages of all time and C compilers are available for the majority of available computer architectures and operating systems. Many other programming languages adopt the features from C Language. Current version of C Language is C11 which was approved in December 2011.

 

Have a look on this blog because we are now implementing online study of  C Programming with an easy way to learn and understand the concept behind the use of it.

Wednesday, 2 April 2014

Syntax of C Language Programs

Every programming language has its own syntax for writing programs and C language is the mother language of all other languages which are developed after C language. Most of the time, I see and feel that writing a program is depends on your logical skills but other factor of knowing programming syntax can not be ignored. So I decided to write something about the syntax of C Language step by step. Just have a look to understand the syntax.

 

Almost all programming languages use same type of syntax i.e. starting part, declaration part, logical or conditional part, main part, modules part and closing part. Give a close look on the syntax of the C Language Program. First I am writing component being used for programming.

 

Components of C Program

//comments - wherever it is required

//header files - at lease one required

//main function - at least one is required

//start curly brace - immediate after main function

//variable declaration - required as per program

//function for display -  required for display

//function for data input - required for input data

//logical or conditional loops - wherever required

//function for output - required for output data

//end curly brace - at the end of program


Syntax of C Program

#include<stdio.h>   /*standard input-output header file */
int main()                /*main function of the program*/
{                                /*start of curly brace required*/
      int a;                 /*variable declarations if required*/
      printf("Any Message"); /*function to display message*/
      scanf("%d",&a);             /*if required to input value*/
}                                 /*close curly brace required*/  

If you memorize this simple syntax of C Program, I am sure learning C language will be very easy for you all. Do you have any question or suggestion, if yes, let me know and I will definitely give reply within 48 hours.