C Tutorial For The Beginners With Example And Source Code

July 4th, 2012 | 3 Comments | Posted in General, Programming

C programming examples for beginners

 The Programmings are done step by step based on the syllabus. The C programming examples  for beginners will help the beginners to understand the C programming , operators, loops, arrays, strings, file etc easily.The C programming examples  for beginners is very easy code to understand for the beginners

C programming codes

  • Hello world
  • Print Integer
  • Addition
  • Odd or Even
  • Add, subtract, multiply and divide
  • Check vowel
  • Leap year
  • Add digits
  • Factorial
  • HCF and LCM
  • Decimal to binary conversion
  • ncR and nPr
  • Add n numbers
  • Swapping
  • Reverse number
  • Palindrome number
  • Print Pattern
  • Diamond
  • Prime numbers
  • Find armstrong number
  • Generate armstrong number
  • Fibonacci series
  • Print floyd’s triangle
  • Print pascal triangle
  • Addition using pointers
  • Maximum element in array
  • Minimum element in array
  • Linear search
  • Binary search
  • Reverse array
  • Insert element in array
  • Delete element from array
  • Merge arrays
  • Bubble sort
  • Insertion sort
  • Selection sort
  • Add matrices
  • Subtract matrices
  • Transpose matrix
  • Multiply two matrices
  • Print string
  • String length
  • Compare strings
  • Copy string
  • Concatenate strings
  • Reverse string
  • Find palindrome
  • Delete vowels
  • C substring
  • Sort a string
  • Remove spaces
  • Change case
  • Swap strings
  • Character’s frequency
  • Anagrams
  • Read file
  • Copy files
  • Merge two files
  • List files in a directory
  • Delete file
  • Random numbers
  • Add complex numbers
  • Print date
  • Get IP address
  • Shutdown computer

C hello world example

#include 
#include 
 
int void main()
{
  printf("Hello world\n");
  getch();
}

Hello world program in C using character Array

#include 
#include 
int main()
{
  char string[] = "Hello World";

  printf("%s\n", string);

getch();
}

C program print integer

 This c program first inputs an integer and then prints it. Input is done using scanf function and number is printed on screen using printf.
#include 

int main()
{
  int a;

  printf("Enter an integer\n");
  scanf("%d", &a);

  printf("Integer that you have entered is %d\n", a);

  return 0;
}

c program print integer

This  program takes an input as integer type and prints it. Input is done using scanf function and number is printed on screen using printf.

C programming code

#include 
# include 
 void main()
{
  int a;
  printf("Enter an integer\n");
  scanf("%d", &a);
  printf("Integer that you have entered is %d\n", a);
getch();
}

C program to add two numbers

C program to add two numbers: This c language program perform the basic arithmetic operation of addition on two numbers and then prints the sum on the screen. For example if the user entered two numbers as 5, 6 then 11 ( 5 + 6 ) will be printed on the screen.

C programming code

#include
#include
Void main()
{
   int a, b, c;
   printf("Enter two numbers to add\n");
   scanf("%d%d",&a,&b);
   c = a + b;
   printf("Sum of entered numbers = %d\n",c);
getch();
}

Add two nos without using third variable

#include
#include
Void main()
{
   int a = 1, b = 2;
    /* Storing result of addition in variable a */
    a = a + b;
    /* Not recommended because original value of a is lost
    * and you may be using it some where in code considering it
    * as it was entered by the user.
    */
    printf("Sum of a and b = %d\n", a);
 getch();
}

C program to add two numbers repeatedly

#include
#include
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;
   }
getch();
}

Adding numbers in c using function

We have used long data type as it can handle large numbers.

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

C program to check odd or even

c program to check odd or even: We will determine whether a number is odd or even by using different methods all are provided with a code in c language. As you have study in mathematics that in decimal number system even numbers are divisible by 2 while odd are not so we may use modulus operator(%) which returns remainder, For example 4%3 gives 1 ( remainder when four is divided by three). Even numbers are of the form 2*p and odd are of the form (2*p+1) where p is is an integer.

We can use bitwise AND (&) operator to check odd or even, as an example consider binary of 7 (0111) when we perform 7 & 1 the result will be one and you may observe that the least significant bit of every odd number is 1, so ( odd_number & 1 ) will be one always and also ( even_number & 1 ) is zero.

In c programming language when we divide two integers we get an integer result, For example the result of 7/3 will be 2.So we can take advantage of this and may use it to find whether the number is odd or even. Consider an integer n we can first divide by 2 and then multiply it by 2 if the result is the original number then the number is even otherwise the number is odd. For example 11/2 = 5, 5*2 = 10 ( which is not equal to eleven), now consider 12/2 = 6 and 6 *2 = 12 ( same as original number). These are some logic which may help you in finding if a number is odd or not.

C program to check odd or even using modulus operator

#include
#include
Void main()
{
   int n;
   printf("Enter an integer\n");
   scanf("%d",&n);
   if ( n%2 == 0 )
      printf("Even\n");
   else
      printf("Odd\n");
 getch();
   }

C program to check odd or even using bitwise operator

#include
#include
main()
{
   int n;
   printf("Enter an integer\n");
   scanf("%d",&n);
   if ( n & 1 == 1 )
      printf("Odd\n");
   else
      printf("Even\n");
 getch();
}

C program to check odd or even without using bitwise or modulus operator

#include
#include
main()
{
   int n;
   printf("Enter an integer\n");
   scanf("%d",&n);
   if ( (n/2)*2 == n )
      printf("Even\n");
   else
      printf("Odd\n");
 getch();
}

Find odd or even using conditional operator

#include
#include
main()
{
   int n;
   printf("Enter an integer\n");
   scanf("%d",&n);
   n%2 == 0 ? printf("Even number\n") : printf("Odd number\n");
    getch();
}

C program to perform addition, subtraction, multiplication and division

C program to perform basic arithmetic operations i.e. addition , subtraction, multiplication and division of two numbers. Numbers are assumed to be integers and will be entered by the user.

C programming code

#include 
#include
int main()
{
   int first, second, add, subtract, multiply;
   float divide;
   printf("Enter two integers\n");
   scanf("%d%d", &first, &second);
   add      = first + second;
   subtract = first - second;
   multiply = first * second;
   divide   = first / (float)second;   //typecasting
   printf("Sum = %d\n",add);
   printf("Difference = %d\n",subtract);
   printf("Multiplication = %d\n",multiply);
   printf("Division = %.2f\n",divide);
   getch();
}

In c language when we divide two integers we get integer result for example 5/2 evaluates to 2. As a general rule integer/integer = integer and float/integer = float or integer/float = float. So we convert denominator to float in our program, you may also write float in numerator. This explicit conversion is known as typecasting.

c program to check whether input alphabet is a vowel or not

This code checks whether an input alphabet is a vowel or not. Both lower-case and upper-case are checked.

C programming code

#include 
#include
 main()
{
  char ch;
  printf("Enter a character\n");
  scanf("%c", &ch);
  if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch =='o' || ch=='O' || ch == 'u' || ch == 'U')
    printf("%c is a vowel.\n", ch);
  else
    printf("%c is not a vowel.\n", ch);
  getch();
}

Check vowel using switch statement

#include 
Void main()
{
  char ch;
  printf("Enter a character\n");
  scanf("%c", &ch);
  switch(ch)
  {
    case 'a':
    case 'A':
    case 'e':
    case 'E':
    case 'i':
    case 'I':
    case 'o':
    case 'O':
    case 'u':
    case 'U':
      printf("%c is a vowel.\n", ch);
      break;
    default:
      printf("%c is not a vowel.\n", ch);
  }
 getch();
  }

Function to check vowel

int check_vowel(char a)
{
    if (a >= 'A' && a 
       a = a + 'a' - 'A';   /* Converting to lower case or use a = a + 32 */
    if (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')
       return 1;
    return 0;
}

c program to check leap year

c program to check leap year: c code to check leap year, year will be entered by the user. Please read theleap year article before reading the code, it will help you to understand the program.

C programming code

#include 
#include 
int main()
{
  int year;
  printf("Enter a year to check if it is a leap year\n");
  scanf("%d", &year);
  if ( year%400 == 0)
    printf("%d is a leap year.\n", year);
  else if ( year%100 == 0)
    printf("%d is not a leap year.\n", year);
  else if ( year%4 == 0 )
    printf("%d is a leap year.\n", year);
  else
    printf("%d is not a leap year.\n", year);
  getch();
}

add digits of number in c

C program to add digits of a number: Here we are using modulus operator(%) to extract individual digits of number and adding them.

C programming code

#include 
main()
{
   int n, sum = 0, remainder;
   printf("Enter an integer\n");
   scanf("%d",&n);
   while(n != 0)
   {
      remainder = n % 10;
      sum = sum + remainder;
      n = n / 10;
   }
   printf("Sum of digits of entered number = %d\n",sum);
   return 0;
}

For example if the input is 98, sum(variable) is 0 initially
98%10 = 8 (% is modulus operator which gives us remainder when 98 is divided by 10).
sum = sum + remainder
so sum = 8 now.
98/10 = 9 because in c whenever we divide integer by another integer we get an integer.
9%10 = 9
sum = 8(previous value) + 9
sum = 17
9/10 = 0.
So finally n = 0, loop ends we get the required sum.

Output of program:

Add digits using recursion

#include 
#include
int add_digits(int);
int main() {
  int n, result;
   scanf("%d", &n);
   result = add_digits(n);
   printf("%d\n", result);
 getch();
}
int add_digits(int n) {
  static int sum = 0;
  if (n == 0) {
    return 0;
  }
   sum = n%10 + add_digits(n/10);
   return sum;
}

Factorial program in c

Factorial program in c: c code to find and print factorial of a number, three methods are given, first one uses a for loop, second uses a function to find factorial and third using recursion. Factorial is represented using !, so five factorial will be written as 5!, n factorial as n!. Also
n! = n*(n-1)*(n-2)*(n-3)…3.2.1 and zero factorial is defined as one i.e. 0!=1.

Factorial program in c using for loop

: Here we find factorial using for loop.

#include 
#include
int main()
{
  int c, n, fact = 1;
   printf("Enter a number to calculate it's factorial\n");
  scanf("%d", &n);
   for (c = 1; c 
    fact = fact * c;
   printf("Factorial of %d = %d\n", n, fact);
   getch();
}

Factorial
Output of code:

Factorial program in c using function

#include 
#include 
long factorial(int);
int main()
{
  int number;
  long fact = 1;
   printf("Enter a number to calculate it's factorial\n");
  scanf("%d", &number);
   printf("%d! = %ld\n", number, factorial(number));
   return 0;
}
long factorial(int n)
{
  int c;
  long result = 1;
   for (c = 1; c 
    result = result * c;
   return result;
}

Factorial program in c using recursion

#include
#include
long factorial(int);
 int main()
{
  int num;
  long f;
   printf("Enter a number to find factorial\n");
  scanf("%d", &num);
   if (num 
    printf("Negative numbers are not allowed.\n");
  else
  {
    f = factorial(num);
    printf("%d! = %ld\n", num, f);
  }
getch();
}
long factorial(int n)
{
  if (n == 0)
    return 1;
  else
    return(n * factorial(n-1));
}

c program to find hcf and lcm

C program to find hcf and lcm: The code below finds highest common factor and least common multiple of two integers. HCF is also known as greatest common divisor(GCD) or greatest common factor(gcf).

C programming code

#include 
#include
 int main() {
  int a, b, x, y, t, gcd, lcm;
   printf("Enter two integers\n");
  scanf("%d%d", &x, &y);
   a = x;
  b = y;
   while (b != 0) {
    t = b;
    b = a % b;
    a = t;
  }
   gcd = a;
  lcm = (x*y)/gcd;
  printf("Greatest common divisor of %d and %d = %d\n", x, y, gcd);
  printf("Least common multiple of %d and %d = %d\n", x, y, lcm);
 getch();
}

C program to find hcf and lcm using recursion

#include 
#include
long gcd(long, long);
int main() {
  long x, y, hcf, lcm;
   printf("Enter two integers\n");
  scanf("%ld%ld", &x, &y);
   hcf = gcd(x, y);
  lcm = (x*y)/hcf;
   printf("Greatest common divisor of %ld and %ld = %ld\n", x, y, hcf);
  printf("Least common multiple of %ld and %ld = %ld\n", x, y, lcm);
   getch();
}
long gcd(long a, long b) {
  if (b == 0) {
    return a;
  }
  else {
    return gcd(b, a % b);
  }
}

C program to find hcf and lcm using function

#include 
#include
long gcd(long, long);
int main() {
  long x, y, hcf, lcm;
   printf("Enter two integers\n");
  scanf("%ld%ld", &x, &y);
   hcf = gcd(x, y);
  lcm = (x*y)/hcf;
   printf("Greatest common divisor of %ld and %ld = %ld\n", x, y, hcf);
  printf("Least common multiple of %ld and %ld = %ld\n", x, y, lcm);
   getch();
}
 long gcd(long x, long y) {
  if (x == 0) {
    return y;
  }
   while (y != 0) {
    if (x > y) {
      x = x - y;
    }
    else {
      y = y - x;
    }
  }
  return x;
}

Decimal to binary conversion

C program to convert decimal to binary: c language code to convert an integer from decimal number system(base-10) to binary number system(base-2). Size of integer is assumed to be 32 bits. We use bitwise operators to perform the desired task. We right shift the original number by 31, 30, 29, …, 1, 0 bits using a loop and bitwise AND the number obtained with 1(one), if the result is 1 then that bit is 1 otherwise it is 0(zero).

C programming code

#include 
#include 
 int main()
{
  int n, c, k;
   printf("Enter an integer in decimal number system\n");
  scanf("%d", &n);
   printf("%d in binary number system is:\n", n);
   for (c = 31; c >= 0; c--)
  {
    k = n >> c;
     if (k & 1)
      printf("1");
    else
      printf("0");
  }
   printf("\n");
   return 0;
}

Above code only prints binary of integer, but we may wish to perform operations on binary so in the code below we are storing the binary in a string. We create a function which returns a pointer to string which is the binary of the number passed as argument to the function.

C programm to store decimal to binary conversion in a string

#include 
#include 
#include
char *decimal_to_binary(int);
 main()
{
   int n, c, k;
   char *pointer;
    printf("Enter an integer in decimal number system\n");
   scanf("%d",&n);
    pointer = decimal_to_binary(n);
   printf("Binary string of %d is: %s\n", n, t);
    free(pointer);
 getch();
}
char *decimal_to_binary(int n)
{
   int c, d, count;
   char *pointer;
    count = 0;
   pointer = (char*)malloc(32+1);
    if ( pointer == NULL )
      exit(EXIT_FAILURE);
    for ( c = 31 ; c >= 0 ; c-- )
   {
      d = n >> c;
       if ( d & 1 )
         *(pointer+count) = 1 + '0';
      else
         *(pointer+count) = 0 + '0';
      count++;
   }
   *(pointer+count) = '\0';
    return  pointer;
}

c program to find ncr and npr

c program to find nCr and nPr: This code calculate nCr which is n!/(r!*(n-r)!) and nPr = n!/(n-r)!

C program to find nCr using function

#include
#include
long factorial(int);
long find_ncr(int, int);
long find_npr(int, int);
main()
{
   int n, r;
   long ncr, npr;
    printf("Enter the value of n and r\n");
   scanf("%d%d",&n,&r);
    ncr = find_ncr(n, r);
   npr = find_npr(n, r);
    printf("%dC%d = %ld\n", n, r, ncr);
   printf("%dP%d = %ld\n", n, r, npr);
    getch();
}
long find_ncr(int n, int r)
{
   long result;
    result = factorial(n)/(factorial(r)*factorial(n-r));
    return result;
}
long find_npr(int n, int r)
{
   long result;
    result = factorial(n)/factorial(n-r);
    return result;
}
long factorial(int n)
{
   int c;
   long result = 1;
    for( c = 1 ; c 
      result = result*c;
    return ( result );
}

 

Leave a Reply 1127 views, 1 so far today |

Most Commented Posts

Follow Discussion

3 Responses to “C Tutorial For The Beginners With Example And Source Code”

  1. tutu skirt Says:

    Awesome blog! Is your theme custom made or did you download it from somewhere? A theme like yours with a few simple tweeks would really make my blog shine. Please let me know where you got your theme. Thank you

  2. Blogger Says:

    Thank you sir, it is a customized theme but taken idea from different blog themes

  3. winkelschleifer parkside Says:

    We’re a bunch of volunteers and starting a new scheme in our community. Your site offered us with helpful info to work on. You have performed an impressive process and our whole neighborhood shall be grateful to you.

Leave a Reply

CAPTCHA Image
Refresh Image
*