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.







-->

Thursday, June 26, 2014

Google Maps recorded image of UFO in Trout Creet, Montana USA



Hello all , I thought I must share this to all the people. I saw a video today on YouTube about the recent traces of UFO in Trout Creek Montana USA recorded as satellite images by Google Maps.  So I tried to confirm myself looking inside the Google Earth/Maps and the results are very interesting.

1) Open the Google Maps and Searched for Trout Creek Montana USA.


2) The I zoomed to this position please note down the coordinates.


3) Drag the Street view (Person Icon) from the right hand side vertical toolbar of Google Earth


4) Now this would enable Street view in Google Earth/Maps. and you would be able to see something like this.


5) Now Move up to the sky and within a certain range you would find an Orange Saucer like structure and this might be UFO.


6) Zoom it more and you would be able to see a perfect shape that has been described by many discoverers, wanderers, scientists and people around.



So this is a trace and this object captured unintentionally might be the UFO. Keep looking at the sky sitting at your home, may be you can see UFO with naked eyes.





-->

Saturday, June 21, 2014

Puzzles, Interaction and Games Development using Scratch for kids an initiative by MIT



In this growing technological world, education has also changed a lot with more competitiveness and everyone looking to solve the world's toughest problem using Computations. Computer Science has a very important aspect of communicating with Machines a.k.a Computers , that is Computer Programming. Computer Programming can be done using Machine Language ( consisting of 0's and 1's) , Assembly Language ( consist of Mnemonics like ADD 1001,1002) and High Level Programming Language ( offers set of keywords, operators and syntax).

In the coming age we want our next generation to be ready to deal with the basics of what we are working as a professional, but this learning should be done with much more fun and fascination in it. Therefore Scratch project is made by MIT to teach the basics of programming , using the attachment of puzzle pieces, therefore you can build Puzzles like tic-tac-toe or interactions like When Johnny was called by dad what does Johnny answered ( i.e. "Yes papa"), then again after Johnny's answer what does his father replied.

You can find a very addictive environment of game development in scratch . I have made an Interaction and a small game, that would let you know the goodness and ease of scratch very well.

Please share this great app with your little brothers and sisters so that they can learn things well and support rapid growth of Humans :D.

My Scratch Interaction : As there is Football fever all round.



and my game Shootatsight, shoot the creatures come in your way.





-->