#include <stdio.h>
long linear_search(long [], long, long);
int main()
{
long array[100], search, c, n, position;
printf("Enter number of elements in array\n");
scanf("%ld", &n);
printf("Enter %d numbers\n", n);
for (c = 0; c < n; c++)
scanf("%ld", &array[c]);
printf("Enter number to search\n");
scanf("%ld",&search);
position = linear_search(array, n, search);
if (position == -1)
printf("%d is not present in array.\n", search);
else
printf("%d is present at location %d.\n", search, position+1);
return 0;
}
long linear_search(long a[], long n, long find) {
long c;
for (c = 0 ;c < n ; c++ ) {
if (a[c] == find)
return c;
}
return -1;
}
Tuesday, 24 March 2015
C program for linear search using function
C Program for Linear search
#include <stdio.h>
int main()
{
int array[100], search, c, n;
printf("Enter the number of elements in array\n");
scanf("%d",&n);
printf("Enter %d integer(s)\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter the number to search\n");
scanf("%d", &search);
for (c = 0; c < n; c++)
{
if (array[c] == search) /* if required element found */
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if (c == n)
printf("%d is not present in array.\n", search);
return 0;
}
Output of program:
C Program of Linear search for multiple occurrences
#include <stdio.h>
int main()
{
int array[100], search, c, n, count = 0;
printf("Enter the number of elements in array\n");
scanf("%d", &n);
printf("Enter %d numbers\n", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d", &array[c]);
printf("Enter the number to search\n");
scanf("%d", &search);
for (c = 0; c < n; c++) {
if (array[c] == search) {
printf("%d is present at location %d.\n", search, c+1);
count++;
}
}
if (count == 0)
printf("%d is not present in array.\n", search);
else
printf("%d is present %d times in array.\n", search, count);
return 0;
}
Output of code:
Monday, 23 March 2015
C program to find minimum element in array using pointers
#include <stdio.h>
int main()
{
int array[100], *minimum, size, c, location = 1;
printf("Enter the number of elements in array\n");
scanf("%d",&size);
printf("Enter %d integers\n", size);
for ( c = 0 ; c < size ; c++ )
scanf("%d", &array[c]);
minimum = array;
*minimum = *array;
for ( c = 1 ; c < size ; c++ )
{
if ( *(array+c) < *minimum )
{
*minimum = *(array+c);
location = c+1;
}
}
printf("Minimum element found at location %d and it's value is %d.\n",
location, *minimum);
return 0;
}
Output of program:
C program to find minimum element in array using function
#include <stdio.h>
int find_minimum(int[], int);
int main() {
int c, array[100], size, location, minimum;
printf("Enter the number of elements in array\n");
scanf("%d", &size);
printf("Input %d integers\n", size);
for (c = 0; c < size; c++)
scanf("%d", &array[c]);
location = find_minimum(array, size);
minimum = array[location];
printf("Minimum element location = %d and value = %d.\n", location + 1, minimum);
return 0;
}
int find_minimum(int a[], int n) {
int c, min, index;
min = a[0];
index = 0;
for (c = 1; c < n; c++) {
if (a[c] < min) {
index = c;
min = a[c];
}
}
return index;
}
Output of program:
C program to find minimum element in array
#include <stdio.h>
int main()
{
int array[100], minimum, size, c, location = 1;
printf("Enter the number of elements in array\n");
scanf("%d",&size);
printf("Enter %d integers\n", size);
for ( c = 0 ; c < size ; c++ )
scanf("%d", &array[c]);
minimum = array[0];
for ( c = 1 ; c < size ; c++ )
{
if ( array[c] < minimum )
{
minimum = array[c];
location = c+1;
}
}
printf("Minimum element is present at location %d and it's value is %d.\n" ,
location, minimum);
return 0;
}
Output of program:![]()
Wednesday, 18 March 2015
C program to find maximum element in array using function
#include <stdio.h>
int find_maximum(int[], int);
int main() {
int c, array[100], size, location, maximum;
printf("Input number of elements in array\n");
scanf("%d", &size);
printf("Enter %d integers\n", size);
for (c = 0; c < size; c++)
scanf("%d", &array[c]);
location = find_maximum(array, size);
maximum = array[location];
printf("Maximum element location = %d and value = %d.\n", location + 1, maximum);
return 0;
}
int find_maximum(int a[], int n) {
int c, max, index;
max = a[0];
index = 0;
for (c = 1; c < n; c++) {
if (a[c] > max) {
index = c;
max = a[c];
}
}
return index;
}
Output of program:
Input number of elements in array
5
Enter 5 integers
8
6
9
3
2
Maximum element location = 3 and value = 9
C program to find maximum element in array
#include <stdio.h>
int main()
{
int array[100], maximum, size, c, location = 1;
printf("Enter the number of elements in array\n");
scanf("%d", &size);
printf("Enter %d integers\n", size);
for (c = 0; c < size; c++)
scanf("%d", &array[c]);
maximum = array[0];
for (c = 1; c < size; c++)
{
if (array[c] > maximum)
{
maximum = array[c];
location = c+1;
}
}
printf("Maximum element is present at location %d and it's value is %d.\n", location, maximum);
return 0;
}
Output of program:
Enter the number of elements in array
5
Enter 5 integers
4
5
6
8
1
Maximum element is present at location 4 and it's value is 8
C program to add numbers using call by reference
#include <stdio.h>
long add(long *, long *);
int main()
{
long first, second, *p, *q, sum;
printf("Input two integers to add\n");
scanf("%ld%ld", &first, &second);
sum = add(&first, &second);
printf("(%ld) + (%ld) = (%ld)\n", first, second, sum);
return 0;
}
long add(long *x, long *y) {
long sum;
sum = *x + *y;
return sum;
}
Output of program:
Input two integers to add
5
6
5+6=11
C program to add numbers using call by reference
#include <stdio.h>
long add(long *, long *);
int main()
{
long first, second, *p, *q, sum;
printf("Input two integers to add\n");
scanf("%ld%ld", &first, &second);
sum = add(&first, &second);
printf("(%ld) + (%ld) = (%ld)\n", first, second, sum);
return 0;
}
long add(long *x, long *y) {
long sum;
sum = *x + *y;
return sum;
}
Output of program:
Input two integers to add
4
5
4+5=9
C program to add two numbers using pointers
#include <stdio.h>
int main()
{
int first, second, *p, *q, sum;
printf("Enter two integers to add\n");
scanf("%d%d", &first, &second);
p = &first;
q = &second;
sum = *p + *q;
printf("Sum of entered numbers = %d\n",sum);
return 0;
}
Output of program:
Enter two integers to add
2
5
Sum of entered numbers =
7
C program to print Pascal triangle
Pascal Triangle in c:
C program to print Pascal triangle which you
might have studied in Binomial Theorem in Mathematics. Number of rows of
Pascal triangle to print is entered by the user. First four rows of
Pascal triangle are shown below :-
1 1 1 1 2 1 1 3 3 1
Program Code
#include <stdio.h>
long factorial(int);
int main()
{
int i, n, c;
printf("Enter the number of rows you wish to see in pascal triangle\n");
scanf("%d",&n);
for (i = 0; i < n; i++)
{
for (c = 0; c <= (n - i - 2); c++)
printf(" ");
for (c = 0 ; c <= i; c++)
printf("%ld ",factorial(i)/(factorial(c)*factorial(i-c)));
printf("\n");
}
return 0;
}
long factorial(int n)
{
int c;
long result = 1;
for (c = 1; c <= n; c++)
result = result*c;
return result;
}
Output
Enter the number of rows you wish to see in pascal triangle
4
1 1 1 1 2 1 1 3 3 1
C program to print Floyd's triangle using recursion
#include <stdio.h>
void print_floyd(int);
int main()
{
int n, i, c, a = 1;
printf("Input number of rows of Floyd's triangle to print\n");
scanf("%d", &n);
print_floyd(n);
return 0;
}
void print_floyd(int n) {
static int row = 1, c = 1;
int d;
if (n <= 0)
return;
for (d = 1; d <= row; ++d)
printf("%d ", c++);
printf("\n");
row++;
print_floyd(--n);
}
Output
Input number of rows of Floyd's triangle to print
4
1 2 3 4 5 6 7 8 9 10
C program to print Floyd's triangle
C program to print Floyd's triangle:- This program prints Floyd's
triangle. Number of rows of Floyd's triangle to print is entered by the
user. First four rows of Floyd's triangle are as follows :-
1
2 3
4 5 6
7 8 9 10
It's clear that in Floyd's triangle nth row contains n numbers.
Program Code
1
2 3
4 5 6
7 8 9 10
It's clear that in Floyd's triangle nth row contains n numbers.
Program Code
#include <stdio.h>
int main()
{
int n, i, c, a = 1;
printf("Enter the number of rows of Floyd's triangle to print\n");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
for (c = 1; c <= i; c++)
{
printf("%d ",a);
a++;
}
printf("\n");
}
return 0;
}
C program to Fibonacci series program using recursion
#include<stdio.h>
int Fibonacci(int);
main()
{
int n, i = 0, c;
scanf("%d",&n);
printf("Fibonacci series\n");
for ( c = 1 ; c <= n ; c++ )
{
printf("%d\n", Fibonacci(i));
i++;
}
return 0;
}
int Fibonacci(int n)
{
if ( n == 0 )
return 0;
else if ( n == 1 )
return 1;
else
return ( Fibonacci(n-1) + Fibonacci(n-2) );
}
Output Of Program:
5
Fibonacci series
0
1
1
2
3
C program to Fibonacci series using for loop
/* Fibonacci Series c language */ #include<stdio.h> int main() { int n, first = 0, second = 1, next, c; printf("Enter the number of terms\n"); scanf("%d",&n); printf("First %d terms of Fibonacci series are :-\n",n); for ( c = 0 ; c < n ; c++ ) { if ( c <= 1 ) next = c; else { next = first + second; first = second; second = next; } printf("%d\n",next); } return 0; }
Output
Enter the number of terms
5
First %d terms of Fibonacci series are :-
0
1123
C program to generate and print armstrong numbers
#include <stdio.h>
int check_armstrong(int);
int power(int, int);
int main () {
int c, a, b;
printf("Input two integers\n");
scanf("%d%d", &a, &b);
for (c = a; c <= b; c++) {
if (check_armstrong(c) == 1)
printf("%d\n", c);
}
return 0;
}
int check_armstrong(int n) {
long long sum = 0, temp;
int remainder, digits = 0;
temp = n;
while (temp != 0) {
digits++;
temp = temp/10;
}
temp = n;
while (temp != 0) {
remainder = temp%10;
sum = sum + power(remainder, digits);
temp = temp/10;
}
if (n == sum)
return 1;
else
return 0;
}
int power(int n, int r) {
int c, p = 1;
for (c = 1; c <= r; c++)
p = p*n;
return p;
}
Output:
Input two integers
100 400
153
370
371
C program to check Armstrong number using function
#include <stdio.h>
int check_armstrong(long long);
long long power(int, int);
int main () {
long long n;
printf("Input a number\n");
scanf("%lld", &n);
if (check_armstrong(n) == 1)
printf("%lld is an armstrong number.\n", n);
else
printf("%lld is not an armstrong number.\n", n);
return 0;
}
int check_armstrong(long long n) {
long long sum = 0, temp;
int remainder, digits = 0;
temp = n;
while (temp != 0) {
digits++;
temp = temp/10;
}
temp = n;
while (temp != 0) {
remainder = temp%10;
sum = sum + power(remainder, digits);
temp = temp/10;
}
if (n == sum)
return 1;
else
return 0;
}
long long power(int n, int r) {
int c;
long long p = 1;
for (c = 1; c <= r; c++)
p = p*n;
return p;
}
Input a number 35641594208964132 35641594208964132 is an Armstrong number.
C program for prime number using function
#include<stdio.h>
int check_prime(int);
main()
{
int n, result;
printf("Enter an integer to check whether it is prime or not.\n");
scanf("%d",&n);
result = check_prime(n);
if ( result == 1 )
printf("%d is prime.\n", n);
else
printf("%d is not prime.\n", n);
return 0;
}
int check_prime(int a)
{
int c;
for ( c = 2 ; c <= a - 1 ; c++ )
{
if ( a%c == 0 )
return 0;
}
if ( c == a )
return 1;
}
Output
Enter an integer to check whether it is prime or not.
5
5 is prime.
C program to print diamond pattern using recursion
Print Diamond shape is as follows Using Recursion
* *** ***** *** *
Program Code
#include <stdio.h>
void print (int);
int main () {
int rows;
scanf("%d", &rows);
print(rows);
return 0;
}
void print (int r) {
int c, space;
static int stars = -1;
if (r <= 0)
return;
space = r - 1;
stars += 2;
for (c = 0; c < space; c++)
printf(" ");
for (c = 0; c < stars; c++)
printf("*");
printf("\n");
print(--r);
space = r + 1;
stars -= 2;
for (c = 0; c < space; c++)
printf(" ");
for (c = 0; c < stars; c++)
printf("*");
printf("\n");
}
Subscribe to:
Posts (Atom)