You are here

C programs

Sum of digits in C

C program to calculate sum of digits of a number: we will use modulus operator(%) to extract individual digits of a number and keep on adding them.

C programming code

#include <stdio.h>
 
main()
{
   int n, sum = 0, remainder;
 
   printf("Enter an integer\n");
   scanf("%d",&n);
 
   while(n != 0)
   {
      remainder = n % 10;
      sum = sum + remainder;
      n = n / 10;
   }
 
   printf("Sum of digits of the number = %d\n",sum);
 
   return 0;
}

For example,if the input is 98, sum(variable) is 0 initially

String copy in C

This program copy string using library function strcpy, to copy string without using strcpy see source code below in which we have made our own function to copy string.

C program to copy a string

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

main()
{
   char source[] = "C program";
   char destination[50];

   strcpy(destination, source);

   printf("Source string: %s\n", source);
   printf("Destination string: %s\n", destination);

   return 0;
}

Division in C

C program to perform basic arithmetic operations which are addition, subtraction, multiplication and division of two numbers. Numbers are assumed to be integers and will be entered by the user.

C programming code

#include <stdio.h>

int main()
{
   int first, second, add, subtract, multiply;
   float divide;
 
   printf("Enter two integers\n");
   scanf("%d%d", &first, &second);
 
   add      = first + second;
   subtract = first - second;
   multiply = first * second;
   divide   = first / (float)second;   //typecasting
 
   printf("Sum = %d\n",add);

C program to print a string

C program to print a string: a string can be printed by using various functions such as printf, puts.

C programming code

#include <stdio.h>

main()
{
    char array[20] = "Hello World";
 
    printf("%s\n",array);

    return 0;
}

To input a string we use scanf function.

C programming code

#include <stdio.h>

main()
{
    char array[100];
   
    printf("Enter a string\n");
    scanf("%s", array);

    printf("You entered the string %s\n",array);
    return 0;
}

Input string containing spaces

#include <stdio.h>

C program to find the frequency of characters in a string

C program to find frequency of characters in a string: This program computes frequency of characters in a string i.e. which character is present how many times in a string. For example, in the string "code" each of the character 'c', 'o', 'd', and 'e' has occurred one time. Only lower case alphabets are considered, other characters (uppercase and special characters) are ignored. You can easily modify this program to handle uppercase and special symbols.

C programming code

#include<stdio.h>
#include<string.h>
 
main()
{
   char string[100], ch;
   int c = 0, count[26] = {0};
 
   printf("Enter a string\n");

C program to add, subtract, multiply and divide Complex Numbers, complex arithmetic

C program to add, subtract, multiply and divide complex numbers. This program performs basic operations on complex numbers i.e. addition, subtraction, multiplication and division. This is a menu driven program in which user will have to enter his/her choice to perform an operation. User can perform operations until desired. To easily handle a complex number a structure named complex has been used, which consists of two integers first integer is for real part of a complex number and second is for imaginary part.

C programming code

#include <stdio.h>
#include <stdlib.h>

Hello world program in C

C hello world program: C programming language code to print hello world. This program prints hello world, printf function is used to display text on screen, '\n' places cursor on the beginning of next line, stdio.h header file contains declaration of printf function. The code will work on all operating systems may be its Linux, Mac or any other and compilers. To learn a programming language you must start writing programs in it and may be your first c code while learning programming.

C hello world example

#include <stdio.h>

int main()
{
  printf("Hello world\n");

Pages

Subscribe to RSS - C programs