Tuesday, September 16, 2014

Program to find a factorial of a given number in C






#include <stdio.h>
int main()
{
    int fact=1, number,i;
    printf("Enter the number: ");
    scanf("%d", &number);
    
    for(i=1;i<=number;i++)
    {
  fact = fact * i;
  
 }
 
 printf("The Factorial of %d is %d", number, fact);
 return 0;
}

Output

Enter the number: 6
The factorial of 6 is 720


Factorial using Recursion




#include <stdio.h>
int fact = 1;
int factorial(int num)
{
 if (num == 0)
 {
  return fact;
 }
 else
 {
  fact = num * factorial(num-1);
 }
 return fact;
 
 }
int main()
{
 int number;
 printf("Enter the number to find factorial: ");
 scanf("%d",&number);
 number = factorial(number);
 printf("The Factorial is : %d", number);
 return 0;
}

Output
Enter the number to find factorial: 6
The factorial is : 720





-->

Tuesday, September 2, 2014

Program to find Sum of Even and Odd terms in the range in C







 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <stdio.h>
int main()
{
    int i=0,range,odd_sum=0, even_sum=0;
    printf("Enter the range for odd and even nos sum: ");
    scanf("%d", &range);
    for(i=1;i<=range;i++)
    {
        if(i%2 == 0)
        {
            even_sum = even_sum + i;
        }
        else if(i%2 == 1)
        {
            odd_sum = odd_sum + i;
        }
        else
        {
            printf("May be 0");
        }
    }
    printf("\nEven Sum: %d", even_sum);
    printf("\nOdd Sum: %d", odd_sum);
    return 0;

}

Output

Enter the range for odd and even nos sum: 10
Even Sum: 30
Odd Sum: 25

Now the same work can be done using the recursion , Here sum_all is a recursive function that is doing the required work of summing odd numerals and even numerals separately.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/* using recursion */
#include <stdio.h>
int sum_all(int range, int even_sum, int odd_sum)
{
    if(range == 0)
    {
        printf("\nEven Sum: %d", even_sum);
        printf("\nOdd Sum: %d", odd_sum);
        return 0;
    }
    else
    {
        if(range % 2 == 0)
        {
            even_sum= even_sum + range;
        }
        else if(range % 2 == 1)
        {
            odd_sum =  odd_sum + range;
        }
        else
        {
            printf("Invalid Operation");
        }
        sum_all(range - 1, even_sum, odd_sum);
    }
    return 0;
}
int main()
{
    int i=0,range,odd_sum=0, even_sum=0;
    printf("Enter the range for odd and even nos sum: ");
    scanf("%d", &range);
    i = sum_all(range, even_sum, odd_sum);
    printf("\nEven Sum: %d", even_sum);
    printf("\nOdd Sum: %d", odd_sum);
    return 0;

}

Output

Enter the range for odd and even nos sum: 10
Even Sum: 30
Odd Sum: 25






Wednesday, August 20, 2014

Different C programs to find the biggest of the 3 numbers




Biggest of the three behaviour can be achieved using nested if-else statement as shown below

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <stdio.h>
int main()
{
    int a,b,c;
    printf("Enter Three numbers: ");
    scanf("%d%d%d", &a, &b, &c);

    if(a>b)
    {
        if(a>c)
        {
            printf("A = %d is greatest",a);
        }
    }
    else if(b>a)
    {
        if(b>c)
        {
            printf("B = %d is greatest", b);
        }

    }
    else if(c>a)
    {
        if(c>b)
        {
            printf("C = %d is greatest", c);

        }

    }
    else if(a==b)
    {
        if(a==c)
        {
            printf("A = %d , B = %d and C = %d are equal", a,b,c);
        }

    }
    else
    {
        printf("Something else:");
   
    }

    return 0;
}

Output

Enter Three numbers: 12
76
23
B = 76 is greatest

Enter Three numbers: 12
12
12
A = 12 , B = 12 and C = 12 are equal

Enter Three numbers: 1
0
0
A = 1 is greatest

Biggest of the three using logical (&&) operator




 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
int main()
{
    int a, b, c;
    printf("Enter three numbers: ");
    scanf("%d%d%d", &a,&b,&c);
    if ((a>b) && (a>c))
    {
        printf("A = %d is greatest", a);
    }
    else if ((b>a) && (b>c))
    {
        printf("B = %d is greatest", b);
    }
    else if ((c>a) && (c>b))
    {
        printf("C = %d is greatest", c);
    }
    else
    {
        printf("All are equal");
    }
    return 0;
}

Output

Enter three numbers: 12
23
34
C = 34 is greatest

Enter three numbers: 54
2
43
A = 54 is greatest

Enter three numbers: 1
1
1
All are equal







Monday, August 18, 2014

C program to check the given number is positive or negative





 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
int main()
{
    int num;
    printf("Enter any number to check whether it is positive or negative: ");
    scanf("%d", &num);
    if (num == 0)
    {
        printf("The number is %d \n", num);
    }
    
    if (num < 0)
    {
        printf("The number %d is negative \n", num);
    }
    else
    {
        printf("The number %d is positive", num);
    }
    return 0;

}

Output

Enter any number to check whether it is positive or negative: 1
The number 1 is positive

Enter any number to check whether it is positive or negative: -10
The number -10 is negative

Enter any number to check whether it is positive or negative: 0
The number is 0

Using Conditional/Ternary Operators


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#include <stdio.h>
int main()
{
    int num;
    printf("Enter any number to check whether it is positive or negative: ");
    scanf("%d", &num);
    if (num == 0)
    {
        printf("The number is %d", num);
    }
    (num < 0 ) ? printf("The number %d is negative \n", num) : printf("The number %d is positive \n", num);
    return 0;

}

Output

Enter any number to check whether it is positive or negative: 1
The number 1 is positive

Enter any number to check whether it is positive or negative: -10
The number -10 is negative

Enter any number to check whether it is positive or negative: 0
The number is 0



Monday, August 4, 2014

C program to check a given number is odd or even





 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include <stdio.h>
int main()
{
    int number;
    printf("Enter the number to find even or odd: ");
    scanf("%d", &number);
    if(number % 2 == 0)
    {
        printf("The number %d is even",number);
    }
    else
    {
        printf("The number %d is odd",number);
    }
    return 0;
}

Output

Enter the number to find even or not: 4
The number 4 is even

or

Enter the number to find even or not 5
The number 5 is odd


Using Conditional/Ternary Operator program can be re written as:


1
2
3
4
5
6
7
8
9
#include <stdio.h>
int main()
{
    int number;
    printf("Enter the number to find even or odd: ");
    scanf("%d", &number);
    (number % 2 == 0) ? printf("The number %d is even \n", number) : printf("The number %d is odd \n", number);
    return 0;
}

Output

Enter the number to find even or not: 4
The number 4 is even

or

Enter the number to find even or not 5
The number 5 is odd

Tuesday, July 8, 2014

Operator Precedence in Arithmetic and Comments in C programming



Hierarchy of Operations: The computer scans an expression from left to right and performs only one operation at a time. The order in which various operations are performed is referred to hierarchy of operations

Order of evaluation
Arithmetic Operators
1
*,/,%
2
+ -


  1. Library function used in the expression are executed before the precedence is executed.
  2. Parenthesis may be used to overcome the precednce order. When parenthesis are used , one written on the left side will be executed first. E.g (a+b) - (a-b) , so (a+b) will be executed first
  3. When nested parenthesis are used, the innermost parenthesis will be executed first. 
e.g ((a+b) * c) so here (a+b) will be executed first.

Have a look at this video for more information






Sunday, July 6, 2014

Arithmetic Operators and scanf() to get user input in C programming



Arithmetic Operators are used to perform Arithmetic operations in C programming.

Operator
Meaning
Example
Result
+Addition5 + 27
-Subtraction5 - 23
*Multiplication5 * 210
/Division5 / 22
%modulus operator to get remainder in integer division5 % 21


Remember when two operands (5 and 2) are integers, the result obtained is an integer. To obtain real value in the result, at least one of the operators must be float or double . Consider the following example.

5.0/2.0 or 5/2.0 or 5.0/2 will produce the result 2.5 .

Also consider the following examples to get the remainder on modulus operations.

7 % 4 produces the result 3
4 % 10 produces the result 4

An expression consisting of numeric variables, constants and arithmetic operators is called Arithmetic expression. The value obtained by evaluating an arithmetic expression is always numeric , i.e. integer or float/double type.

for b^2 - 4ac the equivalent expression in C would be b*b - 4*a*c

Please have a look at this video

scanf() function 

scanf() function is used to read/input values of the variables using the standard input device (keyboard)
for example:

scanf("format string", &v1, &v2, ...., &vn);

where v1, v2,...., vn are variables whose values are to read from keyboard, (basically these are addresses but we will see that later)

"format string" is the control string which represents the format specification refer the below Table

The symbol '&' Ampersand represents the memory address where the variable value is to be stored.

Example


scanf("%d %f", &a, &b);

or

scanf("%d %d", &a, &b);

or

scanf("%c %c", &a, &b);


When these functions are executed , the computer will wait for the values of the variable listed in scanf which are to be entered using the keyboard.

Format Specifiers
Data Type
Format
Meaning
int
%d
%u
%o
%x
Represents a decimal integer value
Represents a unsigned integer value
Represents a unsigned octal value
Represents a unsigned hexadecimal value
float/double
%f
%e
Represents a decimal integer value
Represents a decimal integer value in decimal or exponential form
Char
%c
%s
Represents a single character value
Represents a string of value of characters


%hd, %ld, %lf are used for short int, long int and long double respectively.







-->