Monday, September 29, 2014

MOOC (Online Course) to Watch out: Intro to Computer Science 101 CS101 Udacity






Hello, everyone after moving to university from Schools we need some good exposure to the field we are studying. I am studying Computer Applications and I found a good resource to get people started with computer science from the beginning. If the people have taken the introductory course of computer science in college earlier and still do not feel confident about what they have learnt and want to develop themselves. They must do it.

Intro to Computer Science CS101 offered on udacity introduces computer science to the people. The basic stuff like software , hardware and the most important to instruct the computer is the programming language and in this course they have used python 2.7 to introduce the newbees programming and one more benefit is that at the end of the course you would finish up making your own web crawler or web spider written in python. Web crawler is a program search engine like Google uses to find pages and index them according to the page attributes (page rank, most visited pages, max occurrence of the word on the page) of the webpage. So have a look at that I have completed the course and received a nice certificate and I am very happy to share it with everyone.






-->

Tuesday, September 16, 2014

Program to print fibonacci series in C





#include <stdio.h>
int main()
{
    int number,i, f1=0, f2=1, f3=0;
    printf("Enter the number: ");
    scanf("%d", &number);
    printf("The following Series is: ");
    for(i=1;i<=number;i++)
    {
  printf("\n %d", f3);
  f1=f2;
  f2=f3;
  f3=f1+f2;
  
 }
 return 0;
}

Output
Enter the number: 5
The following Series is:
1
1
2
3
5

Fibonacci Series using Recursion



#include<stdio.h>

void printFibonacci(int);

int main(){

    int n;
    

    printf("Enter the range of the Fibonacci series: ");
    scanf("%d",&n);

    printf("Fibonacci Series: ");
    printf("%d %d ",0,1);
    printFibonacci(n);

    return 0;
}

void printFibonacci(int n){

    static long int first=0,second=1,sum;

    if(n>0){
         sum = first + second;
         first = second;
         second = sum;
         printf("%ld ",sum);
         printFibonacci(n-1);
    }

}

Output
Enter the range of the Fibonacci series: 5
Fibonacci Series: 0 1 1 2 3 5 8




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