0% found this document useful (0 votes)
197 views

1) Program in C Using Recursion: #Include

The document contains 14 code snippets showing C programs that demonstrate various programming concepts: 1) Factorial program using recursion 2) Reversing a number 3) Checking for palindrome numbers 4) Finding largest element in array 5) Prime number program 6) Transposing a matrix 7) Copying a string using pointers 8) Concatenating strings 9) Reversing a string using pointers 10) Opening a file 11) Copying files 12) Merging two files 13) Storing information in dynamically allocated structures 14) Accessing structure members through a pointer.

Uploaded by

Ranjeet Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
197 views

1) Program in C Using Recursion: #Include

The document contains 14 code snippets showing C programs that demonstrate various programming concepts: 1) Factorial program using recursion 2) Reversing a number 3) Checking for palindrome numbers 4) Finding largest element in array 5) Prime number program 6) Transposing a matrix 7) Copying a string using pointers 8) Concatenating strings 9) Reversing a string using pointers 10) Opening a file 11) Copying files 12) Merging two files 13) Storing information in dynamically allocated structures 14) Accessing structure members through a pointer.

Uploaded by

Ranjeet Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 13

1) program in c using recursion

#include<stdio.h>

long factorial(int);
int main()
{
int n;
long f;
printf("Enter an integer to find factorial\n");
scanf("%d", &n);
if (n < 0)
printf("Negative integers are not allowed.\n");
else
{
f = factorial(n);
printf("%d! = %ld\n", n, f);
}
return 0;
}
long factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}

2) C program to reverse a number


#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;
}

3) Palindrome number program c


#include <stdio.h>
int main()
{
int n, reverse = 0, temp;
printf("Enter a number to check if it is a palindrome
or not\n");
scanf("%d",&n);

temp = n;
while( temp != 0 )
{
reverse = reverse * 10;
reverse = reverse + temp%10;
temp = temp/10;
}
if ( n == reverse )
printf("%d is a palindrome number.\n", n);
else
printf("%d is not a palindrome number.\n", n);
return 0;
}

4) C program to find the largest element in an


array
#include<stdio.h>
int main(){
int a[50],size,i,big;
printf("\nEnter the size of the array: ");
scanf("%d",&size);
printf("\nEnter %d elements in to the array: , size);
for(i=0;i<size;i++)
scanf("%d",&a[i]);
big=a[0];
for(i=1;i<size;i++){
if(big<a[i])
big=a[i];
}
printf("\nBiggest: %d",big);
return 0;
}

5) 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;
}

6) C program to transpose a matrix


#include <stdio.h>
int main()
{
int m, n, c, d, matrix[10][10], transpose[10][10];
printf("Enter the number of rows and columns of matrix
");
scanf("%d%d",&m,&n);
printf("Enter the elements of matrix \n");
for( c = 0 ; c < m ; c++ )
{
for( d = 0 ; d < n ; d++ )
{
scanf("%d",&matrix[c][d]);
}
}
for( c = 0 ; c < m ; c++ )
{
for( d = 0 ; d < n ; d++ )
{
transpose[d][c] = matrix[c][d];
}
}
printf("Transpose of entered matrix :-\n");

for( c = 0 ; c < n ; c++ )


{
for( d = 0 ; d < m ; d++ )
{
printf("%d\t",transpose[c][d]);
}
printf("\n");
}
return 0;
}

7) c program to copy a string using pointers


#include<stdio.h>
void copy_string(char*, char*);
main()
{
char source[100], target[100];
printf("Enter source string\n");
gets(source);
copy_string(target, source);
printf("Target string is \"%s\"\n", target);
return 0;

}
void copy_string(char *target, char *source)
{
while(*source)
{
*target = *source;
source++;
target++;
}
*target = '\0';
}

8) C program to concatenate strings


#include <stdio.h>
void concatenate_string(char*, char*);
int main()
{
char original[100], add[100];
printf("Enter source string\n");
gets(original);
printf("Enter string to concatenate\n");
gets(add);
concatenate_string(original, add);
printf("String after concatenation is \"%s\"\n",
original);
return 0;
}
void concatenate_string(char *original, char *add)
{
while(*original)
original++;
while(*add)
{

*original = *add;
add++;
original++;
}
*original = '\0';
}

9) C program to reverse a string using pointers


#include<stdio.h>
int string_length(char*);
void reverse(char*);
main()
{
char string[100];
printf("Enter a string\n");
gets(string);
reverse(string);
printf("Reverse of entered string is \"%s\".\n",
string);
return 0;
}
void reverse(char *string)
{
int length, c;
char *begin, *end, temp;
length = string_length(string);
begin = string;
end = string;
for ( c = 0 ; c < ( length - 1 ) ; c++ )
end++;
for ( c = 0 ; c < length/2 ; c++ )

{
temp = *end;
*end = *begin;
*begin = temp;
begin++;
end--;
}
}
int string_length(char *pointer)
{
int c = 0;
while( *(pointer+c) != '\0' )
c++;
return c;
}

10)

C program to open a file

#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch, file_name[25];
FILE *fp;
printf("Enter the name of file you wish to see\n");
gets(file_name);
fp = fopen(file_name,"r"); // read mode
if( fp == NULL )
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
printf("The contents of %s file are :\n", file_name);
while( ( ch = fgetc(fp) ) != EOF )

printf("%c",ch);
fclose(fp);
return 0;
}

11) C program to copy files


#include <stdio.h>
#include <stdlib.h>
int 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 )
{
printf("Press any key to exit...\n");
exit(EXIT_FAILURE);
}
printf("Enter name of target file\n");
gets(target_file);
target = fopen(target_file, "w");
if( target == NULL )
{
fclose(source);
printf("Press any key to exit...\n");
exit(EXIT_FAILURE);
}
while( ( ch = fgetc(source) ) != EOF )
fputc(ch, target);
printf("File copied successfully.\n");

fclose(source);
fclose(target);
return 0;
}

12) C program to merge two files


#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fs1, *fs2, *ft;
char ch, file1[20], file2[20], file3[20];
printf("Enter name of first file\n");
gets(file1);
printf("Enter name of second file\n");
gets(file2);
printf("Enter name of file which will store contents
of two files\n");
gets(file3);
fs1 = fopen(file1,"r");
fs2 = fopen(file2,"r");
if( fs1 == NULL || fs2 == NULL )
{
perror("Error ");
printf("Press any key to exit...\n");
getch();
exit(EXIT_FAILURE);
}
ft = fopen(file3,"w");
if( ft == NULL )
{
perror("Error ");
printf("Press any key to exit...\n");

exit(EXIT_FAILURE);
}
while( ( ch = fgetc(fs1) ) != EOF )
fputc(ch,ft);
while( ( ch = fgetc(fs2) ) != EOF )
fputc(ch,ft);
printf("Two files were merged into %s file
successfully.\n",file3);
fclose(fs1);
fclose(fs2);
fclose(ft);
return 0;
}

13) C Program to Store Information Using


Structures for n Elements Dynamically
#include <stdio.h>
#include<stdlib.h>
struct name {
int a;
char c[30];
};
int main(){
struct name *ptr;
int i,n;
printf("Enter n: ");
scanf("%d",&n);
/* Allocates the memory for n structures with pointer ptr
pointing to the base address. */
ptr=(struct name*)malloc(n*sizeof(struct name));
for(i=0;i<n;++i){
printf("Enter string and integer
respectively:\n");
scanf("%s%d",&(ptr+i)->c, &(ptr+i)->a);
}

printf("Displaying Infromation:\n");
for(i=0;i<n;++i)
printf("%s\t%d\t\n",(ptr+i)->c,(ptr+i)->a);
return 0;
}

C program for access structure's

14)

member through pointer (Structure using


pointer).
#include <stdio.h>
struct name{
int a;
float b;
};
int main(){
struct name *ptr,p;
ptr=&p;
/* Referencing pointer to memory
address of p */
printf("Enter integer: ");
scanf("%d",&(*ptr).a);
printf("Enter number: ");
scanf("%f",&(*ptr).b);
printf("Displaying: ");
printf("%d%f",(*ptr).a,(*ptr).b);
return 0;
}

You might also like