C programming examples

C program to find hcf and lcm

C program to find hcf and lcm: The code below finds highest common factor and least common multiple of two integers. HCF is also known as greatest common divisor(GCD) or greatest common factor(gcf).

C programming code

#include <stdio.h>
 
int main() {
  int a, b, x, y, t, gcd, lcm;
 
  printf("Enter two integers\n");
  scanf("%d%d", &x, &y);
 
  a = x;
  b = y;
 
  while (b != 0) {
    t = b;
    b = a % b;
    a = t;
  }
 
  gcd = a;
  lcm = (x*y)/gcd;
 
  printf("Greatest common divisor of %d and %d = %d\n", x, y, gcd);

C program to merge two arrays

C program to merge two arrays into third array: Arrays are assumed to be sorted in ascending order. You enter two short sorted arrays and combine them to get a large array.

C programming code to merge two sorted arrays

#include <stdio.h>
 
void merge(int [], int, int [], int, int []);
 
int main() {
  int a[100], b[100], m, n, c, sorted[200];
 
  printf("Input number of elements in first array\n");
  scanf("%d", &m);
 
  printf("Input %d integers\n", m);
  for (c = 0; c < m; c++) {
    scanf("%d", &a[c]);
  }

assert in c

assert is a macro which is useful to check certain conditions at run time(when the program is under execution) and is very useful while debugging a program. To use it you must include the header file "assert.h" in your program.

Declaration: void assert(int expression);

Expression is any valid c language expression, mostly it's a condition. In the example we divide two integers or calculate a/b and you know that b can't be zero so we use

Reverse words in a string

C programming code to reverse words in a string or sentence, For example if the input string is "c++ programming language" then the output will be "++c gnimmargorp egaugnal" i.e. we will invert each word occurring of the input string. Algorithm is very simple just scan the string and keep on storing characters until a space comes. If a space is found then we have found one word so we append null terminator and then reverse the word and then copy the characters of original string with the string obtained on reversing. Then repeat the previous step until the string ends.

Print numbers without using loops

C program to print first n natural numbers without using any loop(do while, for or while). In the first program we will use recursion to achieve the desired output and in the second we use goto statement i.e. without creating any function other than main.

C programming code using recursion

#include <stdio.h>
 
void print(int);
 
int main()
{
  int n;
 
  scanf("%d", &n);
 
  print(n);
 
  return 0;
}
 
void print(int n)
{
  static int c = 1;
 
  if (c == n+1)
    return;
 
  printf("%d\n", c);
  c++;
  print(n);
}

Decimal to binary conversion

C program to convert decimal to binary: c language code to convert an integer from decimal number system(base-10) to binary number system(base-2). Size of integer is assumed to be 32 bits. We use bitwise operators to perform the desired task. We right shift the original number by 31, 30, 29, ..., 1, 0 bits using a loop and bitwise AND the number obtained with 1(one), if the result is 1 then that bit is 1 otherwise it is 0(zero).

C programming code

#include <stdio.h>
 
int main()
{
  int n, c, k;
 
  printf("Enter an integer in decimal number system\n");

C program to insert substring into a string

c program to insert substring into a string: This code inserts the target string into the source string. For example if the source string is "c programming" and target string is " is amazing" (please note there is space at beginning) and if we add target string to source string at position 14 then we obtain the string "c programming is amazing". In our c code we will make a function which perform the desired task and we pass three arguments to it the source string, target string and position. You can insert the string at any valid position.

C programming code

 

c program to find area of circle

c program to find area of circle: c programming code to calculate area of circle.

C program to find area of triangle

C program to find area of triangle: c programming code to calculate area of a triangle using Heron's or Hero's formula. User will enter the lengths of sides of triangle. Remember for a triangle to exist the sum of any two sides of a triangle must be greater than third side.

C programming code

/* Assuming Triangle exists - valid input from user */
 
#include<stdio.h>
#include<math.h>
 
main()
{
   double a, b, c, s, area;
 
   printf("Enter the sides of triangle\n");
 
   scanf("%lf%lf%lf",&a,&b,&c);
 
   s = (a+b+c)/2;
 

c program to check odd or even

c program to check odd or even: We will determine whether a number is odd or even by using different methods all are provided with a code in c language. As you have study in mathematics that in decimal number system even numbers are divisible by 2 while odd are not so we may use modulus operator(%) which returns remainder, For example 4%3 gives 1 ( remainder when four is divided by three). Even numbers are of the form 2*p and odd are of the form (2*p+1) where p is is an integer.

Pages

Subscribe to RSS - C programming examples