Tuesday, June 25, 2013

Pattern in C

Pattern: Print the following pattern.
Pattern in C
Write a program to print the following pattern
Logo

Patten


#include<stdio.h>
int main()
{
int a[10][10],i,j,low=0,top=9,n=1;
for(i=0;i<5;i++,low++,top--)
{
for(j=low;j<=top;j++,n++)
a[i][j]=n;
for(j=low+1;j<=top;j++,n++)
a[j][top]=n;
for(j=top-1;j>=low;j--,n++)
a[top][j]=n;
for(j=top-1;j>low;j--,n++)
a[j][low]=n;
}
for(i=0;i<10;i++)
{
printf("\n\n\t");
for(j=0;j<10;j++)
{
printf("%5d",a[i][j]);
}
}
printf("\n\n");
return 0;
}


Friday, June 7, 2013

Project Euler- Problem Number 2 Solution

Project Euler 2: Even Fibonacci numbers
Project Euler- Problem Number 2 Project Euler-Problem Number 2 Solution Project Euler-Problem Number 2 Solution in C  
Even Fibonacci numbers
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.


My first suggestion to solve this problem, read more carefully the problem and think that how should we have to do to solve it. well. At first we have to know what is Fibonacci number or Fibonacci series? What is Fibonacci formula? If we had already known any Fibonacci C program then it would be easy to us. well, Fibonacci sequence is defined as F1 = F2 = 1 .
By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two.
In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation
F_n = F_{n-1} + F_{n-2},\!\,
with seed values
F_0 = 0,\; F_1     = 1.

Well Now we know what is Fibonacci Number. Look at our problem. We have to generate all Fibonacci number under 4000000 and sum of all number. We use 4 variables.  A loop which is help us us generate fibonacci number under 4 millions times. Another two variables are one=1 & two=2; 
Now, C program do just a simple job that is, in three (variable name) variable it add the vaule of one & two and total variable added the value of two of it's own memory for every time. Then C program replace the value of one and two variable. One variable contains the values of two variable and two variable contains the values of three variable. Below is the C program source code of this problem:

#include<stdio.h>
#include<conio.h>
int main()
{
    long total=0,one=1,two=2,three=0;
    while(two<=4000000)
    {
        three=one+two;
        if(two%2==0)
        total+=two;
        one=two;
        two=three;
    }
    printf("%ld",total);
    return 0;
}
After completing the loop function the C program will excite it's final result. To solve this problem I use some modulo operator such as while, % ,||,if etc.While loop help us to loop the C program from 1 to 4000000 times.

Project Euler- Problem Number 1 Solution

Project Euler 1: Multiples of 3 and 5
Project Euler- Problem Number 1
Project Euler-Problem Number 1 Solution
Project Euler-Problem Number 1 Solution in C
Multiples of 3 and 5 Problem in Project Euler
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.

My first suggestion to solve this problem, read more carefully the problem and think that how should we have to do to solve it. well. Now we need to iterate/count the numbers from 1 to 1000. Now what should we have done? Don't worry.. Now we take a number  and check it if it's divisible by 3 or 5. .If any number is divisible by 3 or 5 then we will remark this number and go to the next step. 
After checking all numbers which is divisible by 3 or 5 with re-mark now  we will sum all numbers one by one which is divisible by 3 or 5.
The we will find out final result. I use C programming language to solve this problem. I think C programming is the best programming. Below is my C programming source code: 

//Find the sum of all the multiples of 3 or 5 below 1000
#include<stdio.h>
#include<conio.h>
void main ()
{
int a,b,i,Sum=0;
    for (i=1;i<1000;i++)
    {
       a=i%3;
       b=i%5;
        if(a==0||b==0)
        {
            Sum+=i;
        }
    }
    printf("\nThe sum of all the multiples of 3 or 5 below 1000: %d\n",Sum);


To solve this problem I use some modulo operator such as for, % ,||,if etc.
For loop help us to count number from 1 to 1000. 
In 2nd step C program check the number if it divisor by 3 (a) or 5 (b).
In 3rd option if the number is divisor by any one of 3 and 5 then the sum variable will add (sum) this number
After completing the for loop condition the C program will excite it's result.