C programming code
#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);
Max in
min
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;
}
C programming code to find maximum using
function
Our function returns index at which
maximum element occur.
#include <stdio.h>
int find_maximum(int a[], int n) {
int c, max, index;
max = a[0];
index = 0;
int find_maximum(int[], int);
for (c = 1; c < n; c++) {
if (a[c] > max) {
index = c;
max = a[c];
}
}
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;
return index;
}
C programming code using pointers
#include <stdio.h>
int main()
{
long array[100], *maximum, size, c, location = 1;
printf("Enter the number of elements in array\n");
scanf("%ld", &size);
printf("Enter %ld integers\n", size);
for ( c = 0 ; c < size ; c++ )
scanf("%ld", &array[c]);
maximum = array;
*maximum = *array;
for (c = 1; c < size; c++)
{
if (*(array+c) > *maximum)
{
*maximum = *(array+c);
location = c+1;
}
}
printf("Maximum element found at location %ld and it's value is %ld.\n", location, *maximum);
return 0;
}
//------------Header Files-------------//
#include<stdio.h>
#include<conio.h>
main() //------main function-----//
{
//------Variable Declaration--------//
int a[10],i,n,max,min;
//-----Enter the input value using printf() and scanf() function------//
printf("Enter the Value of N:");
scanf("%d",&n);
printf("\n");
//----Input N Element using For Loop----//
for(i=0;i<n;i++)
{
printf("Enter the %d Element",i+1);
scanf("%d",&a[i]);
}
min=a[0]; //----Minimum Number Initialization-----//
max=a[0]; //----Maximum Number Initialization-----//
//----Minimum and Maximum Number Calculation using For Loop-----//
for(i=0;i<n;i++)
{
if(a[i]>max)
max=a[i];
if(a[i]<min)
min=a[i];
}
//---------- Dispaly Result-------//
printf("\nThe Maximum Number is %d\n",max);
printf("the Minimum Number is %d\n",min);
getch(); //---Function for Hold Output Screen--//
return 0; }
Prime number program in c language
#include<stdio.h>
int main()
{
int n, i = 3, count, c;
printf("Enter the number of prime numbers required\n");
scanf("%d",&n);
if ( n >= 1 )
{
printf("First %d prime numbers are :\n",n);
printf("2\n");
}
for ( count = 2 ; count <= n ; )
{
for ( c = 2 ; c <= i - 1 ; c++ )
{
if ( i%c == 0 )
break;
}
if ( c == i )
{
printf("%d\n",i);
count++;
}
i++;
}
return 0;
}
Prime num
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;
}
C programming code
#include <stdio.h>
int main()
{
int n, c, d, a[100], b[100];
printf("Enter the number of elements in array\n");
scanf("%d", &n);
printf("Enter the array elements\n");
for (c = 0; c < n ; c++)
scanf("%d", &a[c]);
/*
* Copying elements into array b starting from end of array a
*/
for (c = n - 1, d = 0; c >= 0; c--, d++)
b[d] = a[c];
/*
* Copying reversed array into original.
* Here we are modifying original array, this is optional.
*/
for (c = 0; c < n; c++)
a[c] = b[c];
printf("Reverse array is\n");
for (c = 0; c < n; c++)
printf("%d\n", a[c]);
return 0;
}
Reverse array
C program to check odd or even
using modulus operator
#include <stdio.h>
int main()
{
int n;
printf("Enter an integer\n");
scanf("%d", &n);
if (n%2 == 0)
printf("Even\n");
else
printf("Odd\n");
return 0;
}
Odd or even
C program for prime number or not
#include<stdio.h>
main()
{
int n, c = 2;
printf("Enter a number to check if it is prime\n");
scanf("%d",&n);
for ( c = 2 ; c <= n - 1 ; c++ )
{
if ( n%c == 0 )
{
printf("%d is not prime.\n", n);
break;
}
}
if ( c == n )
printf("%d is prime.\n", n);
return 0;
}
#include <stdio.h>
int main()
{
int n;
printf("Enter an integer\n");
scanf("%d", &n);
if (n & 1 == 1)
printf("Odd\n");
else
printf("Even\n");
return 0;
}
Find odd or even using conditional operator
#include <stdio.h>
int main()
{
int n;
printf("Input an integer\n");
scanf("%d", &n);
n%2 == 0 ? printf("Even\n") : printf("Odd\n");
return 0;
}
C program to check odd or even without using bitwise or modulus operator
#include <stdio.h>
int main()
{
int n;
printf("Enter an integer\n");
scanf("%d", &n);
if ((n/2)*2 == n)
printf("Even\n");
else
printf("Odd\n");
return 0;
}
C programming code
#include <stdio.h>
int main()
{
int row, c, n, temp;
printf("Enter the number of rows in pyramid of stars you wish to see ");
scanf("%d",&n);
temp = n;
for ( row = 1 ; row <= n ; row++ )
{
for ( c = 1 ; c < temp ; c++ )
printf(" ");
temp--;
for ( c = 1 ; c <= 2*row - 1 ; c++ )
printf("*");
printf("\n");
}
return 0;
}
Pattern of num
and stars
#include <stdio.h>
int main()
{
int n, c, k;
printf("Enter number of rows\n");
scanf("%d",&n);
for ( c = 1 ; c <= n ; c++ )
{
for( k = 1 ; k <= c ; k++ )
printf("*");
printf("\n");
}
return 0;
}
*
**
***
****
*****
#include<stdio.h>
main()
{
int n, c, d, num = 1, space;
scanf("%d",&n);
space = n - 1;
for ( d = 1 ; d <= n ; d++ )
{
num = d;
for ( c = 1 ; c <= space ; c++ )
printf(" ");
space--;
for ( c = 1 ; c <= d ; c++ )
{
printf("%d", num);
num++;
}
num--;
num--;
for ( c = 1 ; c < d ; c++)
{
printf("%d", num);
num--;
}
printf("\n");
}
return 0;
}
1
232
34543
4567654
567898765
diamond
C program to print diamond
using recursion
#include <stdio.h>
void print (int r) {
int c, space;
static int stars = -1;
if (r <= 0)
return;
void print (int);
space = r - 1;
stars += 2;
int main () {
int rows;
for (c = 0; c < space; c++)
printf(" ");
scanf("%d", &rows);
for (c = 0; c < stars; c++)
printf("*");
print(rows);
printf("\n");
return 0;
}
print(--r);
space = r + 1;
stars -= 2;
for (c = 0; c < space; c++)
printf(" ");
for (c = 0; c < stars; c++)
printf("*");
printf("\n");
}
Print string
#include <stdio.h>
int main()
{
char array[20] = "Hello World";
Input string containing spaces
#include <stdio.h>
int main()
{
char a[80];
printf("%s\n",array);
gets(a);
return 0;
printf("%s\n", a);
return 0;
}
We print string using for loop
by printing individual
characters of string.
#include <stdio.h>
#include <string.h>
int main() {
char s[100];
int c, l;
gets(s);
l = strlen(s);
for (c = 0; c < l; c++)
printf("%c", s[c]);
return 0;
}
C program to print string using recursion
#include <stdio.h>
void print(char*);
int main() {
char s[100];
gets(s);
print(s);
return 0;
}
void print(char *t) {
if (*t == '\0')
return;
printf("%c", *t);
print(++t);
}
String length
#include <stdio.h>
#include <string.h>
int main()
{
char a[100];
int length;
printf("Enter a string to calculate it's
length\n");
gets(a);
Without strlen
#include <stdio.h>
int string_length(char []);
int main()
{
char s[1000];
int length;
printf("Input a string\n");
gets(s);
length = string_length(s);
length = strlen(a);
printf("Length of entered string is =
%d\n",length);
return 0;
}
printf("Length of \"%s\" = %d\n", s, length);
return 0;
}
int string_length(char s[]) {
int c = 0;
while (s[c] != '\0')
c++;
return c;
}
#include <stdio.h>
int main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];
printf("Enter the number of rows and columns of
matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);
printf("Enter the elements of second matrix\n");
for (c = 0; c < m; c++)
for (d = 0 ; d < n; d++)
scanf("%d", &second[c][d]);
printf("Sum of entered matrices:-\n");
for (c = 0; c < m; c++) {
for (d = 0 ; d < n; d++) {
sum[c][d] = first[c][d] + second[c][d];
printf("%d\t", sum[c][d]);
}
printf("\n");
}
return 0;
}
Add matrices
/* insertion sort ascending order */
#include <stdio.h>
int main()
{
int n, array[1000], c, d, t;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++) {
scanf("%d", &array[c]);
} for (c = 1 ; c <= n - 1; c++) {
d = c;
while ( d > 0 && array[d] < array[d-1]) {
t
= array[d];
array[d] = array[d-1];
array[d-1] = t;
d--;
}
}
printf("Sorted list in ascending order:\n");
for (c = 0; c <= n - 1; c++) {
printf("%d\n", array[c]);
}
return 0;
}
sort
Selection sort algorithm implementation in c
#include <stdio.h>
int main()
{
int array[100], n, c, d, position, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d", &array[c]);
for ( c = 0 ; c < ( n - 1 ) ; c++ )
{
position = c;
for ( d = c + 1 ; d < n ; d++ )
{
if ( array[position] > array[d] )
position = d;
}
if ( position != c )
{
swap = array[c];
array[c] = array[position];
array[position] = swap;
}
}
printf("Sorted list in ascending order:\n");
for ( c = 0 ; c < n ; c++ )
printf("%d\n", array[c]);
return 0;
}
#include <stdio.h>
int main()
{
int n, reverse = 0;
printf("Enter a number to
reverse\n");
scanf("%d", &n);
while (n != 0)
{
reverse = reverse * 10;
reverse = reverse + n
%10;
n
= n/10;
}
printf("Reverse of entered
number is = %d\n", reverse);
return 0;
Reverse num
Addition by pointer
#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;
#include <stdio.h>
long add(long *, long *);
int main()
{
long first, second, *p, *q, sum;
sum = *p + *q;
printf("Input two integers to add\n");
scanf("%ld%ld", &first, &second);
printf("Sum of entered numbers = %d\n",sum);
sum = add(&first, &second);
return 0;
}
C program to add numbers using call by
reference
printf("(%ld) + (%ld) = (%ld)\n", first,
second, sum);
return 0;
}
long add(long *x, long *y) {
long sum;
sum = *x + *y;
return sum;