Category Archives: Programming

PHP program to print nth number of Fibonacci Series.

Fibonacci Series: 0 1 1 2 3 5 8 13 21 …

Here are 2 PHP programs to print nth number of Fibonacci Series:

Iterative Program to find nth number of Fibonacci Series:

<?php

fibonacci(1);
fibonacci(2);
fibonacci(3);
fibonacci(4);
fibonacci(5);

function fibonacci($n) 
{
        $first_num = 0;
	$second_num = 1;

	if(1 == $n)
	{
		echo $first_num;
	}
	elseif(2 == $n)
	{
		echo $second_num;
	}
	else
	{
		$next_num = 0;
		for($i=1; $i<=$n-2; $i++)
		{
			$next_num = $first_num + $second_num;
			$first_num = $second_num;
			$second_num = $next_num;
		}
		echo $next_num;
	}
}

?>

Recursive Program to find nth number of Fibonacci Series:

<?php

echo fibonacci(1);
echo fibonacci(2);
echo fibonacci(3);
echo fibonacci(4);
echo fibonacci(5);

function fibonacci($n) 
{
        if(1 == $n)
	{
		$result = 0;
	}
	elseif(2 == $n)
	{
		$result = 1;
	}
	else
	{
		$result = fibonacci($n-1) + fibonacci($n-2);
	}
	return($result);
}

?>

C programs to print various patterns.

Question 1(a): Write a program to print the following pattern:

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Answer:

#include<stdio.h>
#include<conio.h>

void main()
{
	int i,j,num_lines;
	clrscr();

	printf("\nEnter the number of lines to print: ");
	scanf("%d", &num_lines);

	for(i=1; i<=num_lines; i++)
	{
		for(j=1; j<=i; j++)
		{
			printf("%d ", j);
		}
		printf("\n");
	}

	getch();
}

Question 1(b): Write a program to print the following pattern:

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

Answer:

#include<stdio.h>
#include<conio.h>

void main()
{
	int i,j,num_lines;
	clrscr();

	printf("\nEnter the number of lines to print: ");
	scanf("%d", &num_lines);

	for(i=1; i<=num_lines; i++)
	{
		for(j=1; j<=i; j++)
		{
			printf("%d ", i);
		}
		printf("\n");
	}

	getch();
}

Automatically generate .htaccess code for your website.

Greetings,

It is cumbersome and tedious to write .htaccess redirection rules.

Moreover due to its overall impact on the behaviour of the website, most developers refrain from writing code into htaccess. They usually take the help of their server support department to achieve this.

However now you can also generate .htaccess code depending on your requirement by visiting URL:
http://www.htaccessredirect.net/index.php

It has helped me a lot. Hope it helps you as well.

Thanks.

C program to find intersection and union of 2 arrays.

C program to find intersection and union of 2 arrays is as follows:

#include<stdio.h>
#include<conio.h>

#define SIZE 5

void get_data(int arr[]);
void print_data(int arr[], int n);
void bubble_sort(int arr[]);
int find_intersection(int array_1[], int array_2[], int intersect_result[]);
int find_union(int array_1[], int array_2[], int union_result[]);

void main()
{
	int array_1[SIZE], array_2[SIZE], intersect_result[SIZE], union_result[SIZE*2];
	int num_elements;
	clrscr();

	//Get the elements of Array1
	printf("\nEnter the elements of Array 1: \n");
	get_data(array_1);

	printf("\n\nElements of Array 1: ");
	print_data(array_1, SIZE);

	//Sort array 1
	bubble_sort(array_1);
	printf("\n\nSorted elements of Array 1: ");
	print_data(array_1, SIZE);

	//Get the elements of Array2
	printf("\n\nEnter the elements of Array 2: \n");
	get_data(array_2);

	printf("\n\nElements of Array 2: ");
	print_data(array_2, SIZE);

	//Sort array 2
	bubble_sort(array_2);
	printf("\n\nSorted elements of Array 2: ");
	print_data(array_2, SIZE);

	//Find Intersection and print the result
	num_elements = find_intersection(array_1, array_2, intersect_result);	
	printf("\n\nIntersection is: ");
	print_data(intersect_result, num_elements);

	//Find Union
	num_elements = find_union(array_1, array_2, union_result);
	printf("\n\nUnion is: ");
	print_data(union_result, num_elements);

	getch();
}

void get_data(int arr[])
{
	int i,j;
	for(i=0; i<SIZE; i++)
	{
		j = i+1;
		printf("\nEnter element %d: ",j);
		scanf("%d", &arr[i]);
	}
}

void print_data(int arr[], int n)
{
	int i;
	printf("{ ");
	for(i=0; i<n; i++)
	{
		printf("%d ",arr[i]);
	}
	printf("}");
}

void bubble_sort(int arr[])
{
	int i,j,temp,swapped;

	for(i=1; i<SIZE; i++)
	{
		swapped = 0;
		for(j=0; j<SIZE-i; j++)
		{
			if(arr[j] > arr[j+1])
			{
				temp = arr[j];
				arr[j] = arr[j+1];
				arr[j+1] = temp;
				swapped = 1;
			}
		}

		if(swapped == 0)
		{
			break;
		}
	}
}

int find_intersection(int array_1[], int array_2[], int intersect_result[])
{
	int i = 0, j = 0, k = 0;
	while((i<SIZE) && (j<SIZE))
	{
		if(array_1[i] < array_2[j])
		{
			i++;
		}
		else if(array_1[i] > array_2[j])
		{
			j++;
		}
		else
		{
			intersect_result[k] = array_1[i];
			i++;
			j++;
			k++;
		}
	}

	return(k);
}

int find_union(int array_1[], int array_2[], int union_result[])
{
	int i = 0, j = 0, k = 0;	
	while((i<SIZE) && (j<SIZE))
	{
		if(array_1[i] < array_2[j])
		{
			union_result[k] = array_1[i];
			i++;
			k++;
		}
		else if(array_1[i] > array_2[j])
		{
			union_result[k] = array_2[j];
			j++;
			k++;
		}
		else
		{
			union_result[k] = array_1[i];
			i++;
			j++;
			k++;
		}
	}

	if(i == SIZE)
	{
		while(j<SIZE)
		{
			union_result[k] = array_2[j];
			j++;
			k++;
		}
	}
	else
	{
		while(i<SIZE)
		{
			union_result[k] = array_1[i];
			i++;
			k++;
		}
	}

	return(k);
}

C program to encrypt a string.

Question: Write a program to crypt its input according to a specified transformation scheme. The transformation scheme will consist of two strings: a string of characters and then a string of replacement characters. The idea is that your program replaces every instance of the ith character in the initial string with the (i+2) character (of English alphabets) in the replacement string. When no substitution is defined for a character, the program just passes it through to the output unchanged. Blank spaces and the other symbols remains the same. The program should inform the user of any errors in the transformation scheme. Your program should display the phrase before and after the substitutions have been made.
Example:
Original String: I know C programming.
String after the transformation: K mpqy E rtqitcookpi.

Answer:

#include<stdio.h>
#include<conio.h>

void read_string(char str[]);
void crypt(char str[], int transform_scheme);

void main()
{
	int transform_scheme;
	char str[100];

	clrscr();

	printf("\nEnter a string (Not more than 100 chars): ");

	//Read data from the user
	read_string(str);
	printf("\nOriginal String is: %s\n", str);

	//Read transformation scheme from the user
	printf("\nEnter the transaformation scheme (In Integer): ");
	scanf("%d", &transform_scheme);

	//Logic to convert the input string to encrypted form
	crypt(str, transform_scheme);
	printf("\nConverted String is: %s\n", str);
	getch();
}

void read_string(char str[])
{
	int i, temp;

	i = 0;
	while((temp = getchar()) != '\n')
	{
		str[i] = temp;
		i = i+1;
	}
	str[i] = '\0';
}

void crypt(char str[], int transform_scheme)
{
	int i,len;
	len = strlen(str);

	for(i=0; i<len; i++)
	{
		if(str[i] >= 'A' && str[i] <= 'Z')
		{
			str[i] = str[i] + transform_scheme;

			if(str[i] > 'Z')
			{
				str[i] = 'A' + ((str[i] - 'Z') - 1);
			}

			if(str[i] < 'A')
			{
				str[i] = 'Z' - (('A' - str[i]) - 1);
			}
		}
		if(str[i] >= 'a' && str[i] <= 'z')
		{
			str[i] = str[i] + transform_scheme;

			if(str[i] > 'z')
			{
				str[i] = 'a' + ((str[i] - 'z') - 1);
			}

			if(str[i] < 'a')
			{
				str[i] = 'z' - (('a' - str[i]) - 1);
			}
		}
	}
}

C program for converting distance from one unit to another.

Question: Write an interactive program called “DISTANCE CONVERTER” that accepts the distance in millimetres / feet / miles / yards / kilometres and displays its equivalent in metres.

Relationship between various units of distance is as follows:
– 1 meter = 1000 mm
– 1 meter = 3.28 feet
– 1 mile = 1609 meter
– 1 meter = 1.0936 yard
– 1 kilometer = 1000 meter

C program to convert distance from one unit to meter is as follows:

#include<stdio.h>
#include<conio.h>

void main()
{
	float distance_unit, distance_meter;
	int choice, cont = 1;
	clrscr();

	printf("\nDistance Converter\n");

	while(cont == 1)
	{
		clrscr();
		printf("\n1.Enter distance in Millimeters.\n");
		printf("\n2.Enter distance in Feet.\n");
		printf("\n3.Enter distance in Miles.\n");
		printf("\n4.Enter distance in Yards.\n");
		printf("\n5.Enter distance in Kilometers.\n");

		printf("\nEnter your choice: ");
		scanf("%d", &choice);

		printf("\nEnter distance: ");
		scanf("%f", &distance_unit);

		switch(choice)
		{
			case 1:
				//1 meter = 1000 mm
				distance_meter = distance_unit / 1000;
				break;
			case 2:
				//1 meter = 3.28 feet
				distance_meter = distance_unit / 3.28;
				break;
			case 3:
				//1 mile = 1609 meter
				distance_meter = distance_unit * 1609;
				break;
			case 4:
				//1 meter = 1.0936 yard
				distance_meter = distance_unit / 1.0936;
				break;
			case 5:
				//1 kilometer = 1000 meter
				distance_meter = distance_unit * 1000;
				break;
			default:
				printf("\nInvalid choice selected.\n");
				break;
		}

		printf("\nEquivalent distance in Metres is: %f\n", distance_meter);

		printf("\nDo you want to continue (1/0)? ");
		scanf("%d", &cont);
	}

	getch();
}

C program to generate progress report of the students.

Question: Write an interactive program to generate progress reports for the students of class XII (Science group)

Answer: Interactive C program to generate progress reports of the students is as follows:

#include<stdio.h>
#include<conio.h>

#define STU_NUM 3
#define NAME_LEN 100
#define SUB_NUM 3

struct student
{
	int id;
	char name[NAME_LEN];
	char group;
	int subject_marks[SUB_NUM];
	int total;
	int percentage;
};

void main()
{
	int i, j, k, l, total, percentage;
	struct student stud[STU_NUM];
	clrscr();

	//Get the required data from user.
	for(i=0; i<STU_NUM; i++)
	{
		j = i+1;

		printf("\n\nEnter the ID of student %d: ",j);
		scanf("%d",&stud[i].id);
		fflush(stdin);

		printf("\nEnter the name of student %d: ",j);
		gets(stud[i].name);
		fflush(stdin);

		printf("\nEnter the group selected by student %d (A/B): ",j);
		scanf("%c",&stud[i].group);
		fflush(stdin);

		for(k=0; k<SUB_NUM; k++)
		{
			l = k+1;
			printf("\nEnter the marks of subject %d for student %d: ",l,j);
			scanf("%d",&stud[i].subject_marks[k]);
		}
		fflush(stdin);
	}

	//Calculate total & percentage of the student.
	for(i=0; i<STU_NUM; i++)
	{
		total = 0;
		percentage = 0;
		for(k=0; k<SUB_NUM; k++)
		{
			total = total + stud[i].subject_marks[k];
		}
		percentage = total / SUB_NUM;

		stud[i].total = total;
		stud[i].percentage = percentage;
	}

	//Display the progress report of the students.
	clrscr();
	for(i=0; i<STU_NUM; i++)
	{
		printf("ID: %d \t Name: %s \t Group: %c \n", stud[i].id, stud[i].name, stud[i].group);

		for(k=0; k<SUB_NUM; k++)
		{
			l = k+1;
			printf("Subject %d: %d / 100 \n", l, stud[i].subject_marks[k]);
		}
		printf("Total: %d \t Percentage: %d \n", stud[i].total, stud[i].percentage);
		printf("\n----------------------------------------------------------------------\n\n");
	}

	getch();
}