Sunday 17 June 2012

C Language Basic Program code

c hello world program

C hello world program :- c programming language code to print hello world. This program prints hello world, printf function is used to display text on screen, '\n' places cursor on the beginning of next line, stdio.h header file contains declaration of printf function. The code will work on all operating systems may be it's Linux, Mac or any other and compilers. To learn a programming language you must start writing programs in it and may be your first c code while learning programming.

C hello world example

#include <stdio.h>
 
int main()
{
  printf("Hello world\n");
  return 0;
}

Hello world program in c

We may store "hello world" in a character array and then print it.

#include <stdio.h>
 
int main()
{
  char string[] = "Hello World";
 
  printf("%s\n", string);
 
  return 0;
}
Hello World  program.
Output of program:
hello world code outut
Watch video explaining how to use turbo c compiler for making your first c program, it shows you how to compile and run a program.



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.

C programming code

#include<stdio.h>
 
main()
{
    int a;
 
    printf("Enter an integer\n");
    scanf("%d", &a);
 
    printf("Integer that you have entered is %d\n", a);
 
    return 0;
}
Output:


input number

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<stdio.h>
 
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);
 
   return 0;
}

Add numbers program executable.

Output of program

add numbers

Addition without using third variable

#include<stdio.h>
 
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);
 
   return 0;
}

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

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

#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;
}


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<stdio.h>
 
main()
{
   int n;
 
   printf("Enter an integer\n");
   scanf("%d",&n);
 
   if ( n%2 == 0 )
      printf("Even\n");
   else
      printf("Odd\n");
 
   return 0;
}

C program to check odd or even using bitwise operator

#include<stdio.h>
 
main()
{
   int n;
 
   printf("Enter an integer\n");
   scanf("%d",&n);
 
   if ( n & 1 == 1 )
      printf("Odd\n");
   else
      printf("Even\n");
 
   return 0;
}

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

#include<stdio.h>
 
main()
{
   int n;
 
   printf("Enter an integer\n");
   scanf("%d",&n);
 
   if ( (n/2)*2 == n )
      printf("Even\n");
   else
      printf("Odd\n");
 
   return 0;
}

Find odd or even using conditional operator

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


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<stdio.h>
 
main()
{
   int n;
 
   printf("Enter an integer\n");
   scanf("%d",&n);
 
   if ( n%2 == 0 )
      printf("Even\n");
   else
      printf("Odd\n");
 
   return 0;
}

C program to check odd or even using bitwise operator

#include<stdio.h>
 
main()
{
   int n;
 
   printf("Enter an integer\n");
   scanf("%d",&n);
 
   if ( n & 1 == 1 )
      printf("Odd\n");
   else
      printf("Even\n");
 
   return 0;
}

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

#include<stdio.h>
 
main()
{
   int n;
 
   printf("Enter an integer\n");
   scanf("%d",&n);
 
   if ( (n/2)*2 == n )
      printf("Even\n");
   else
      printf("Odd\n");
 
   return 0;
}

Find odd or even using conditional operator

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








arithmetic operations program

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 <stdio.h>
 
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);
 
   return 0;
}

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. Arithmetic operations.
Output of program:


arithmetic operations program

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 <stdio.h>
 
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);
 
  return 0;
}
Output:
check character case output

Check vowel using switch statement

#include <stdio.h>
 
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);
  }              
 
  return 0;
}

Function to check vowel

int check_vowel(char a)
{
    if (a >= 'A' && a <= 'Z')
       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;
}

0 comments:

Post a Comment