You are here

C programs

Sum of n numbers in C

Sum of n numbers C program: This program add n numbers which will be entered by the user. Firstly user will enter a number indicating how many numbers user wishes to add and then user will enter n numbers. In the first C program to add numbers we are not using an array, and using array in the second code.

C programming code

#include <stdio.h>

int main()
{
   int n, sum = 0, c, value;

   printf("Enter the number of integers you want to add\n");
   scanf("%d", &n);

   printf("Enter %d integers\n",n);

   for (c = 1; c <= n; c++)
   {
      scanf("%d",&value);

C program to add two numbers using pointers

This program performs addition of two numbers using pointers. In our program we have two two integer variables x, y and two pointer variables p and q. Firstly we assign the addresses of x and y to p and q respectively and then assign the sum of x and y to variable sum. Note that & is address of operator and * is value at address operator.

C programming code

#include<stdio.h>

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;

Armstrong number in C

Armstrong number C program: C programming code to check whether a number is armstrong or not. A number is armstrong if the sum of cubes of individual digits of a number is equal to the number itself. For example,371 is an armstrong number as 33 + 73 + 13 = 371. Some other armstrong numbers are: 0, 1, 153, 370, 407.

C programming code

#include <stdio.h>

main()
{
   int number, sum = 0, temp, remainder;

   printf("Enter a number\n");      
   scanf("%d",&number);
   
   temp = number;

   while( temp != 0 )
   {

String compare in C

This C program compares two strings using strcmp, without strcmp and using pointers. For comparing strings without using library function see another code below.

C program to compare two strings using strcmp

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

main()
{
   char a[100], b[100];

   printf("Enter the first string\n");
   gets(a);

   printf("Enter the second string\n");
   gets(b);

   if( strcmp(a,b) == 0 )
      printf("Entered strings are equal.\n");
   else

String concatenation in C

This program concatenates strings, for example,if the first string is "c " and second string is "program" then on concatenating these two strings we get the string "c program". To concatenate two strings we use strcat function of string.h, to concatenate without using library function see another code below which uses pointers.

C programming code

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

main()
{
   char a[100], b[100];

   printf("Enter the first string\n");
   gets(a);
   
   printf("Enter the second string\n");
   gets(b);

   strcat(a,b);

C program to delete a file

This C program deletes a file which is entered by the user, the file to be deleted should be present in the directory in which the executable file of this program is present. Extension of the file should also be entered, also note that deleted file doesn't go to recycle bin, remove macro is used to delete the file. If there is an error in deleting the file then an error will be displayed using perror function.

C programming code

#include<stdio.h>

main()
{
   int status;
   char file_name[25];

   printf("Enter the name of file you wish to delete\n");

C program to print Armstrong numbers

armstrong number in c: This program prints armstrong number. In our program we ask the user to enter a number and then we use a loop from one to the entered number and check if it is an armstrong number and if it is then the number is printed on the screen. Remember a number is armstrong if the sum of cubes of individual digits of a number is equal to the number itself. For example,371 is an armstrong number as 33 + 73 + 13 = 371. Some other armstrong numbers are 0, 1, 153, 370, 407.

C code

#include<stdio.h>
#include<conio.h>

main()
{

C program to copy a file

C program to copy files: This program copies a file, firstly you will specify the file to copy and then you will enter the name of target file, you will have to mention the extension of file also. We will open the file that we wish to copy in read mode and target file in write mode.

C programming code

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

main()
{
   char ch, source_file[20], target_file[20];
   FILE *source, *target;

   printf("Enter name of file to copy\n");
   gets(source_file);

   source = fopen(source_file, "r");

   if( source == NULL )
   {

C program to merge two files

This C program merges two files and store their contents in an another file. The files which are to be merged are opened in read mode and the file which contains content of both the files is opened in write mode. To merge two files first we open a file and read it character by character and store the read contents in another file then we read the contents of another file and store it in file, we read two files until EOF ( end of file ) is reached.

C programming code

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

main()
{
   FILE *fs1, *fs2, *ft;

Addition of two numbers in C

C program to add two numbers: This c language program perform the basic arithmetic operation of addition on two numbers and then prints the sum on the screen. For example,if the user entered two numbers as 5, 6 then 11 ( 5 + 6 ) will be printed on the screen.

C programming code

#include<stdio.h>
 
main()
{
   int a, b, c;
   
   printf("Enter two numbers to add\n");
   scanf("%d%d",&a,&b);
   
   c = a + b;
   
   printf("Sum of entered numbers = %d\n",c);
   
   return 0;
}

Pages

Subscribe to RSS - C programs