Thursday, 5 June 2014
0
Thursday, 5 June 2014
IndianBooktuber
read more
C Program 16: To Print The Fibonacci Series Using Recursion
Aim: To print fibonacci series
Fibonacci series is the one in which every third number is the sum of first two.
The series is 1,1,3,4,7,11,18....
//To print the fibonacci series using recursion
#include<stdio.h>
#include<conio.h>
int fibo(int x);
void main()
{
int a=1, b=1, n,p;
printf("Enter the number of digits you want to print: ");
scanf("%d", &n);
printf("The fibonacci series is:\n%d\n%d", 1 , 1);
fibo(n);
getch();
}
int fibo(int x)
{
static int a =1 , b=1, temp, c;
if (x-2==0)
{
return 0;
}
else
{
c=a+b;
printf("\n%d", c);
temp=b;
b=c;
a=temp;
fibo(x-1); //using recursion
}
}
The code is fairly simple. We have printed the first two number using a simple printf statement in the main function. Then we have called the fibo()
Now, say we want to print the first 25 numbers in the series. that would mean value of n is 25. But we have already printed first two numbers in main function. So, in the fibo(), in if statement, we would put a condition for x-2 numbers i.e. 23 numbers.
Now, we would use simple arithmetic operation c=a+b;
This would give us the third number. Then we have performed swapping of the numbers. And then we have called the same fibo() function.
Fibonacci series is the one in which every third number is the sum of first two.
The series is 1,1,3,4,7,11,18....
//To print the fibonacci series using recursion
#include<stdio.h>
#include<conio.h>
int fibo(int x);
void main()
{
int a=1, b=1, n,p;
printf("Enter the number of digits you want to print: ");
scanf("%d", &n);
printf("The fibonacci series is:\n%d\n%d", 1 , 1);
fibo(n);
getch();
}
int fibo(int x)
{
static int a =1 , b=1, temp, c;
if (x-2==0)
{
return 0;
}
else
{
c=a+b;
printf("\n%d", c);
temp=b;
b=c;
a=temp;
fibo(x-1); //using recursion
}
}
The code is fairly simple. We have printed the first two number using a simple printf statement in the main function. Then we have called the fibo()
Now, say we want to print the first 25 numbers in the series. that would mean value of n is 25. But we have already printed first two numbers in main function. So, in the fibo(), in if statement, we would put a condition for x-2 numbers i.e. 23 numbers.
Now, we would use simple arithmetic operation c=a+b;
This would give us the third number. Then we have performed swapping of the numbers. And then we have called the same fibo() function.
0
IndianBooktuber
read more
C Program 15: To Find the Factorial of a number using for loop
Aim: To find factorial of a number
Factorial of a number like 4 would be 4*3*2*1=24
Following would be the code:
// To find the factorial of a given number using for loop
#include<stdio.h>
#include<conio.h>
void main()
{
int fact, a;
printf("Enter the number: ");
scanf("%d", &a);
fact=factorial (a);
printf("The factorial of the given number is: %d\n", fact);
getch();
}
int factorial(int x)
{
int f=1,i;
for(i=x; i>=1; i--)
{
f=f*i;
}
return f;
}
Factorial of a number like 4 would be 4*3*2*1=24
Following would be the code:
// To find the factorial of a given number using for loop
#include<stdio.h>
#include<conio.h>
void main()
{
int fact, a;
printf("Enter the number: ");
scanf("%d", &a);
fact=factorial (a);
printf("The factorial of the given number is: %d\n", fact);
getch();
}
int factorial(int x)
{
int f=1,i;
for(i=x; i>=1; i--)
{
f=f*i;
}
return f;
}
Just a for loop for(i=x; i>=1; i--) is used.
In case, you want to do the same through recursion.. check this link:
0
IndianBooktuber
read more
C Program 14: To Find The Factorial of a Number Using Recursion
Aim: To find the factorial using recursion.
What is Recursion? when a function calls itself, the phenomenon is referred to as recursion and it is one of the most powerful features of C. Most of the algorithms that are developed using C use recursion.
Code:
// To find the factorial of a given number using recursion
#include<stdio.h>
#include<conio.h>
void main()
{
int fact, a;
printf("Enter the number: ");
scanf("%d", &a);
fact=factorial (a);
printf("The factorial of the given number is: %d\n", fact);
getch();
}
int factorial(int x)
{
int f;
if (x==1)
{
return(1);
}
else
{
f=x*factorial(x-1); //using recursion
}
return f;
}
The real part is in the function factorial. The function takes x as a parameter which is the number whose factorial we want to find. Say, we want to find 4! that means x would be equal to 4.
Now, we would use if else statement. If the value of x is 1, the function would return 1.
Else, the following formula would be used:
f=x*factorial(x-1);
Note that we are using the same function once again. We are using factorial() inside factorial(). This is RECURSION.
But this time the value passed to the function isn't 4. It is 3 i.e. 4-1
thus, on successive calls the function would call itself and pass parameters like - 4,3,2,1. When function has 1 as parameter, if statement would be executed and control would return to the second last function i.e. the one with parameter 2.
Thus, we would get 4*3*2*1 = 24
What is Recursion? when a function calls itself, the phenomenon is referred to as recursion and it is one of the most powerful features of C. Most of the algorithms that are developed using C use recursion.
Code:
// To find the factorial of a given number using recursion
#include<stdio.h>
#include<conio.h>
void main()
{
int fact, a;
printf("Enter the number: ");
scanf("%d", &a);
fact=factorial (a);
printf("The factorial of the given number is: %d\n", fact);
getch();
}
int factorial(int x)
{
int f;
if (x==1)
{
return(1);
}
else
{
f=x*factorial(x-1); //using recursion
}
return f;
}
Now, we would use if else statement. If the value of x is 1, the function would return 1.
Else, the following formula would be used:
f=x*factorial(x-1);
Note that we are using the same function once again. We are using factorial() inside factorial(). This is RECURSION.
But this time the value passed to the function isn't 4. It is 3 i.e. 4-1
thus, on successive calls the function would call itself and pass parameters like - 4,3,2,1. When function has 1 as parameter, if statement would be executed and control would return to the second last function i.e. the one with parameter 2.
Thus, we would get 4*3*2*1 = 24
Tuesday, 3 June 2014
0
Tuesday, 3 June 2014
IndianBooktuber
read more
C Program 13: To Swap Two Numbers Using Pointers
Aim: Swap two numbers using pointers.
Following is the code:
//To swap two numbers using pointers
#include<stdio.h>
#include<conio.h>
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a=*b;
*b=temp;
// printf ("The value of a is: %d \n ", *a );
//printf("The value of b is: %d\n ", * b);
}
void main()
{
int *a, *b;
printf("enter the two numbers: ");
scanf("%d%d", &a, &b);
swap (&a,&b);
printf ("The value of a is: %d \n", a );
printf("The value of b is: %d \n ", b);
getch();
}
Theory: We will perform the swapping in function swap. So, the function swap has two arguments - *a and *b. An * before a means a is actually the value at address of a. So, if the value at address of a is 53, *a would take up that value.
Now, we use the basic swapping operation. We will put the *a into a temporary variable and then *b into *a and again temp into *b. This concludes the swapping. Now, if we want to print the resulting numbers in the swap function itself, we will need to use *a and *b in printf statement instead of a and b. This is because we want to print the value at a and b. If we don't do that, address of the a and b would be printed or some garbage value.
Now, coming back to main function, we must make sure that we pass address of a and b (&a, &b) to the swap function else we won't receive the correct output.
Following is the code:
//To swap two numbers using pointers
#include<stdio.h>
#include<conio.h>
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a=*b;
*b=temp;
// printf ("The value of a is: %d \n ", *a );
//printf("The value of b is: %d\n ", * b);
}
void main()
{
int *a, *b;
printf("enter the two numbers: ");
scanf("%d%d", &a, &b);
swap (&a,&b);
printf ("The value of a is: %d \n", a );
printf("The value of b is: %d \n ", b);
getch();
}
Now, we use the basic swapping operation. We will put the *a into a temporary variable and then *b into *a and again temp into *b. This concludes the swapping. Now, if we want to print the resulting numbers in the swap function itself, we will need to use *a and *b in printf statement instead of a and b. This is because we want to print the value at a and b. If we don't do that, address of the a and b would be printed or some garbage value.
Now, coming back to main function, we must make sure that we pass address of a and b (&a, &b) to the swap function else we won't receive the correct output.
0
IndianBooktuber
read more
C Program 12: To Print The Given Rectangle Pattern
Aim: To print this pattern
*-***********
***-*********
*****-*******
*******-*****
*********-***
***********-*
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, k;
for (i=0; i<=5; i++)
{
for (j=1; j<=i+(i+1); j++)
{
printf("*");
}
printf("-");
for (k=1; k<=11-(i+i); k++)
{
printf("*");
}
printf("\n");
}
getch();
}
For this pattern we will need to use three for loops. First loop would be for the number of lines in our pattern. Those are 6 and that's why the condition in the first for loop is i<=5
Now, we will try to divide our pattern in our two parts - one on the left side of dash and other on the right side.
The one at left side is printed in series like 1,3,5,7,9,11. So, all we need to do is to try to use a condition on the second for loop which tries to print this series. This is done through the condition j<=i+(i+1)
The condition may obviously vary depending upon an individual's logical ability.
Similarly, in the third for loop, we would try to reverse the series - 11,9,7,5,3,1 And hence we will get the desired pattern.
Output:
In case, you still have doubts.. feel free to contact me.
Monday, 2 June 2014
0
Monday, 2 June 2014
IndianBooktuber
read more
C Program 11: To Print a Right Angled Triangle
Aim: To print this pattern
*
**
***
****
*****
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, n;
printf("Click on number of lines you want to print: ");
scanf("%d", &n);
for (i=1; i<=n; i++)
{
for (j=1; j<=i; j++)
{
printf("*");
}
printf("\n");
}
getch();
}
Theory: The pattern is the simplest pattern. One thing you should remember while creating patterns is that the first for loop is always for the number of lines you want to cover with your pattern. Here we have asked the user to enter how many lines he wants to print the pattern in.
Then we have used the condition for (i=1; i<=n; i++) to print the stars according to the way we want. The choice is condition depends upon your logical capabilities and may differ.
This is the output:
*
**
***
****
*****
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, n;
printf("Click on number of lines you want to print: ");
scanf("%d", &n);
for (i=1; i<=n; i++)
{
for (j=1; j<=i; j++)
{
printf("*");
}
printf("\n");
}
getch();
}
Theory: The pattern is the simplest pattern. One thing you should remember while creating patterns is that the first for loop is always for the number of lines you want to cover with your pattern. Here we have asked the user to enter how many lines he wants to print the pattern in.
Then we have used the condition for (i=1; i<=n; i++) to print the stars according to the way we want. The choice is condition depends upon your logical capabilities and may differ.
This is the output:
0
IndianBooktuber
read more
C Program 10: To Find The Smallest Number in an Array
Aim: To Find the smallest number in an array
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int i, arr[5], smallest;
printf("Enter the values in an array: ");
for (i=0; i<=5; i++) /* enter values into array */
{
scanf("%d", &arr[i]);
}
/* checking the smallest no. */
smallest = arr[0];
for (i=0; i<=5; i++)
{
if (arr[i] < smallest )
{
smallest = arr[i];
}
}
printf("The smallest number is: %d", smallest);
getch();
}
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int i, arr[5], smallest;
printf("Enter the values in an array: ");
for (i=0; i<=5; i++) /* enter values into array */
{
scanf("%d", &arr[i]);
}
/* checking the smallest no. */
smallest = arr[0];
for (i=0; i<=5; i++)
{
if (arr[i] < smallest )
{
smallest = arr[i];
}
}
printf("The smallest number is: %d", smallest);
getch();
}
First we will ask the user to enter the values into the array. Now, we will take an integer smallest and put the initial value of array arr[0] into it. Now, we have to compare each value present in array with the smallest. If this value is less than the value in smallest we will replace the value of the latter with the value present in array at that time.
We will repeat that procedure 6 times for our array contains six values.
Following is the sample output:
0
IndianBooktuber
read more
C Program 9: To Calculate Sum And Average of Array
Aim: to calculate the sum and average of an array
Theory: We will ask the user to input the values into the array and then we would find out the sum and average.
Following is the code:
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[5], sum= 0, i=0, j;
float avg;
printf("enter the values into the array: ");
scanf("%d%d%d%d%d%d", &arr[0], &arr[1], &arr[2], &arr[3], &arr[4], &arr[5], &arr[6]);
printf("The Values you entered are: ");
for (j=0; j<=5; j++)
{
printf("%d\n", arr[j]);
}
//finding the sum
while (i<=5)
{
sum = sum + arr[i];
i++;
}
printf("The sum is of the array is: %d \n", sum);
//finding average
avg= sum/5;
printf("The average of the array is: %f", avg);
getch();
}
First, as already mentioned, we would ask the user to input the values into array. One should prefer to use a for loop for the same purpose. However, I have stuck to the very basic form of inputting values into array. Then we will use for loop to print the values entered by the user.
Now, we will find the sum. This is also possible using for loop but we can accomplish this with while loop as well. We can find the sum using this statement sum = sum + arr[i];
Once we find the sum, we will display the output. Then we will apply formula for finding the average of an array which is very simple avg = sum/5
This way we will get the following output:
This is a very basic program using arrays. In case, you have any doubts, contact me.
Theory: We will ask the user to input the values into the array and then we would find out the sum and average.
Following is the code:
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[5], sum= 0, i=0, j;
float avg;
printf("enter the values into the array: ");
scanf("%d%d%d%d%d%d", &arr[0], &arr[1], &arr[2], &arr[3], &arr[4], &arr[5], &arr[6]);
printf("The Values you entered are: ");
for (j=0; j<=5; j++)
{
printf("%d\n", arr[j]);
}
//finding the sum
while (i<=5)
{
sum = sum + arr[i];
i++;
}
printf("The sum is of the array is: %d \n", sum);
//finding average
avg= sum/5;
printf("The average of the array is: %f", avg);
getch();
}
Now, we will find the sum. This is also possible using for loop but we can accomplish this with while loop as well. We can find the sum using this statement sum = sum + arr[i];
Once we find the sum, we will display the output. Then we will apply formula for finding the average of an array which is very simple avg = sum/5
This way we will get the following output:
Subscribe to:
Posts (Atom)