Sunday, November 29, 2015

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 <stdio.h>
 
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;
}
Download Decimal binary program.
Output of program:
Decimal to binary c program
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 code to store decimal to binary conversion in a string

#include <stdio.h>
#include <stdlib.h>
 
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);
 
return 0;
}
 
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;
}
Memory is allocated dynamically because we can't return a pointer to a local variable (character array in this case). If we return a pointer to local variable then program may crash or we get incorrect result.

Saturday, November 28, 2015

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.

C program to check odd or even using modulus operator

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

C program to check odd or even using bitwise operator

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

Find odd or even using conditional operator

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

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

#include <stdio.h>
 
int 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;
}
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 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>
 
int 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;
}
Output of program:
add numbers
Download Add numbers program executable.
In the expression (c = a + b) overflow may occur if sum of a and b is larger than maximum value which can be stored in variable c.

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 somewhere 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>
 
int main()
{
int a, b, c;
char ch;
 
while (1) {
printf("Inut two integers\n");
scanf("%d%d", &a, &b);
getchar();
 
c = a + b;
 
printf("(%d) + (%d) = (%d)\n", a, b, 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;
}
Output of program:
Inut two integers
2 6
(2) + (6) = (8)
Do you wish to add more numbers (y/n)
y
Inut two integers
2 -6
(2) + (-6) = (-4)
Do you wish to add more numbers (y/n)
y
Inut two integers
-5 3
(-5) + (3) = (-2)
Do you wish to add more numbers (y/n)
y
Inut two integers
-5 -6
(-5) + (-6) = (-11)
Do you wish to add more numbers (y/n)
n

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;
}
We 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.

C program print integer

This c program ask user to input 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>
 
int main()
{
int a;
 
printf("Enter an integer\n");
scanf("%d", &a);
 
printf("Integer that you have entered is %d\n", a);
 
return 0;
}
Download integer program.
Output of program:
input number
In c language we have data type for different types of data, for integer data it is int, for character date char, for floating point data it's float and so on. For large integers you can use long or long long data type. To store integers which are greater than 2^18-1 which is the range of long long data type you may use strings. In the below code we store an integer in a string and then display it.

C program to store integer in a string

#include <stdio.h>
 
int main ()
{
char n[1000];
 
printf("Input an integer\n");
scanf("%s", n);
 
printf("%s", n);
 
return 0;
}
Output of program:
Input an integer
13246523123156432123154131212341564313219
13246523123156432123154131212341564313219
Advantage of using string is that we can store very very large integers. C programming language does not has a built in type to handle large numbers.

C hello world program

C hello world program: c programming language code to print hello world. This program prints hello world, printf library 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 its 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.

Hello world in C language

//C hello world example
#include <stdio.h>
 
int main()
{
printf("Hello world\n");
return 0;
}
Purpose of Hello world program may be to say hello to people or the users of your software or application.
Output of program:
C hello world program output
Download Hello world program.

Hello world program in c

We may store "hello world" in a character array as a string constant and then print it.
#include <stdio.h>
 
int main()
{
char string[] = "Hello World";
 
printf("%s\n", string);
 
return 0;
}
Don't worry if you didn't understand above code as you may not be familiar with arrays yet.

Printing hello world indefinitely

Using loop we can print "Hello World" a desired number of time or indefinitely.
#include <stdio.h>
#define TRUE 1
 
int main()
{
while (TRUE)
{
printf("Hello World\n");
}
 
return 0;
}
While loop will execute forever until it is terminated, to terminate press (Ctrl + C) in windows operating system.