Related Posts Plugin for WordPress, Blogger...

About

Follow Us

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

Monday, 23 March 2015

Introduction: In this article i will explain how to create  a program to find/get/calculate factorial of entered number using For loop in C language.
Factorial:   Mathematically, the formula for the factorial is as follows. If n is an integer greater than or equal to 1, then
n ! = n ( n - 1)( n - 2)( n - 3) ... (3)(2)(1)
Rule: n! = n*(n-1)!

   So 1! = 1
        2! = 2×1 = 2
        3! = 3×2×1 = 6
        4! = 4×3×2×1 = 24
        5! = 5×4×3×2×1 = 120
        6!=  6×5×4×3×2×1 = 720


 or we can say if n=6 then factorial of 6 will be calculated as 1*2*3*4*5*6= 720 or 6*5*4*3*2*1=720.  So 6 != 720.

Implementation: Let's create a program to calculate Factorial of a number.

 #include<conio.h>
#include<stdio.h>
int main()
{
  unsigned long long int fact=1; 
  int i,n;
  clrscr();
  printf("Enter any positive number to calculate its factorial: ");
  scanf("%d",&n);
  for(i=1;i<=n;i++)
  {
    fact=fact*i;
  }
   printf("\nFactorial of %d = %llu",n,fact);
   getch();
   return 0;
}

Note:  We can also use 
  for(i=n;i>=1;i--)
  {
    fact=fact*i;
  }
instead of 
  for(i=1;i<=n;i++)
  {
    fact=fact*i;
  }

Now Run the program using Ctrl+F9

Monday, 16 February 2015

Introduction: 
In this article I will explain how to check the character you entered is Vowel or Consonant using If-else Ladder in C.
A,E,I,O,U is known as Vowel and other alphabets are called Consonants.

#include<conio.h>
#include<stdio.h>
main()
{
       char ch;
      printf("Enter  any character between a to z : ");
      scanf("  %c",&ch);

                      if(ch=='a')
                            {
                            printf("%c  is vowel”,ch);
                            }
                      else if(ch=='e')
                            {
                            printf("%c  is vowel”,ch);
                            }
                      else if(ch=='i')
                            {
                            printf("%c  is vowel”,ch);
                            }
                      else if(ch=='o')
                            {
                            printf("%c  is vowel”,ch);
                            }
                        else if(ch=='u')
                            {
                            printf("%c  is vowel”,ch);
                            }                                
                    else
                           {
                            printf("%c  is consonant”,ch);
                           }
                    getch();
                    }
                   
 Run the program using Ctrl+F9
                   
Output:

First Run
 Enter any character between a to z : u
 u is vowel 
Second Run
 Enter any number between a to z : p

 p is consonant

Thursday, 12 February 2015

Introduction: 

Now in this article i will explain how to find greater between two numbers using conditional operator in C Language. Conditional operator is also known as ternary operator and its symbol is?
 The syntax for conditional operator is: condition? expression1:expression2;

Now in this article we will learn how to find greatest of three numbers using nested if else statement in c language.

Implementation: Let's create a program to understand better.
#include<conio.h>
#include<stdio.h>
main()
{

Monday, 9 February 2015

Swap two numbers without using temporary or Third Variable in C Language.

Introduction:
 In this article i will explain How to swap/interchange the values of two numbers using C language with an example program. In this program I’ll not use the third variable to swap numbers.
Implementation: let's create a program to swap the value of two numbers without using third variable.

Saturday, 7 February 2015

Introduction: 
What is C Language?
In computing C is a general purpose programming language developed by Dennis Ritchie at “AT & T’s Bell Laboratories” of USA in 1972.
This language was created for a specific purpose i.e. to design the UNIX operating system. The UNIX operating system and virtually all UNIX applications are written in the C language. C has now become a widely used professional language for various reasons which are:
·         Easy to learn
·         Structured language
·         It produces efficient programs.
·         It can handle low-level activities.
·         It can be compiled on a variety of computers.
Some Facts about C Language
·         C was basically invented to write an operating system called UNIX.
·         C language is a successor of B language which was introduced around 1970
·         The language was formalized in 1988 by the American National Standard Institute (ANSI).
·         C is a procedural language.
·         Today C is the most widely used System Programming Language.
·         C language influenced most of the modern world programming languages which include the mighty C++ and Java as well.

Now let’s create a program to calculate sum of two numbers using C language
#include<stdio.h>
#include<conio.h>
void main()
{
  int a,b,sum;
  clrsrc();
  printf("Enter value of a=");
  scanf("%d",&a);
  printf(" Enter value of b=");
  scanf("%d",&b);
  sum=a+b;
  printf("Sum of %d and %d = %d"a,b,sum);
  getch();
}

Run the program using ctrl+F9

Output will be:
Enter value of a= 50
Enter value of b= 30
Sum of 50 and 30 = 80




Difference b