You are here

C programs

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

C program to reverse words in a string

C program 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 in 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 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 program 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 in C

Decimal to binary conversion
Decimal to binary in C to convert an integer from decimal number system (base-10) to binary number system (base-2). The size of an integer is assumed to be 32 bits. We use the bitwise operator "AND" to perform the desired task. We right shift the original number by 31, 30, 29, ..., 1, 0 bits using a for loop and bitwise AND the number obtained with 1(one) if the result is 1, then that bit is one otherwise zero (0).

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

 

Area of a circle in C

Area of Circle

C program to find the area of a circle: C programming code to calculate the area of a circle. In the program, we use 3.14159 as the value of Pi (ϖ). To compute it, we need to know the radius. If we know the diameter or circumference, we find the radius from it. Let's write the program now.

Area of a triangle in C

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;
   

Even or odd program in C

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.

C program to count number of vowels in a string

C program to count vowels in a string: C programming code to find how many number of vowels are present in a string, For example,in the string "c programming" there are three vowels 'o', 'a' and 'i'. In the program both lower and upper case are considered i.e 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O' and 'U'. In our code we scan the complete string and check every character in it if it's a vowel then counter is incremented by one, consonants and special characters are ignored.

C programming code

#include<stdio.h>

int count_vowels(char []);
int check_vowel(char);

C program for pattern matching

Pattern matching in C: C program to check if a string is present in an another string, for example, the string "programming" is present in the string "C programming". If it is present, then its location (i.e. at which position it is present) is printed. We create a function match which receives two character arrays and returns the position if matching occurs otherwise returns -1. We are implementing naive string search algorithm in this program.

C programming code

#include <stdio.h>
#include <string.h>

int match(char [], char []);

int main() {

Pages

Subscribe to RSS - C programs