100% found this document useful (1 vote)
156 views

Program To Reverse The Entered String

The document contains programs in C/C++ for various tasks like reversing a string, finding the day of the week for January 1st of any given year, checking if a triangle is valid, finding the binary equivalent of a decimal number, finding the absolute value of a number, generating Armstrong numbers between 1-500, checking if given points are collinear, determining if a point lies on the x-axis, y-axis, or origin, determining if a point lies inside, on, or outside a circle, performing linear search, binary search, reversing an array, inserting an element into an array, deleting an element from an array, implementing bubble sort, insertion sort, and adding two matrices.

Uploaded by

Priyanka Rai
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
156 views

Program To Reverse The Entered String

The document contains programs in C/C++ for various tasks like reversing a string, finding the day of the week for January 1st of any given year, checking if a triangle is valid, finding the binary equivalent of a decimal number, finding the absolute value of a number, generating Armstrong numbers between 1-500, checking if given points are collinear, determining if a point lies on the x-axis, y-axis, or origin, determining if a point lies inside, on, or outside a circle, performing linear search, binary search, reversing an array, inserting an element into an array, deleting an element from an array, implementing bubble sort, insertion sort, and adding two matrices.

Uploaded by

Priyanka Rai
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 30

//Program to reverse the entered string #include<stdio.h> #include<conio.h> #include<string.

h> void main() { char*str,*str1; int l,i,k=0; clrscr(); puts("Enter a string\n"); gets(str); l=strlen(str); for(i=l-1;i>=0;i--) { str1[k]=str[i]; k++; } str1[k]=NULL; printf("The reverse is %s",str1); getch(); }

Program in C to calculate or find the day on 1st january of any year #include<stdio.h> #include<conio.h> void main() { int leapdays,firstday,yr; long int normaldays,totaldays; clrscr(); printf("Enter year "); scanf("%d",&yr); normaldays=(yr-1)*365L; leapdays=(yr-1)/4-(yr-1)/100+(yr-1)/400; totaldays=normaldays+leapdays; firstday=totaldays%7; if(firstday==0) printf("\nMonday"); if(firstday==1) printf("\Tuesday"); if(firstday==2) printf("\nWednesday"); if(firstday==3) printf("\nThrusday"); if(firstday==4) printf("\nFriday"); if(firstday==5) printf("\nSaturday"); if(firstday==6) printf("\nSunday");

printf("\n\n\n\n\nPress any key to exit...."); getch(); }

Program in C to check whether given triangle is valid or not by entering three angles #include<stdio.h> #include<conio.h> void main() { float angle1,angle2,angle3; clrscr(); printf("Enter three angles of triangle: "); scanf("%f%f%f",&angle1,&angle2,&angle3); if((angle1+angle2+angle3)==180) printf("The triangle is valid"); else printf("The triangle is Invalid"); printf("\n\n\n\n\n\nPress any key to exit......."); getch(); }

//Binary equivalent of a binary number #include<stdio.h> #include<conio.h> int binary (int); void main() { int num; clrscr(); printf("\nEnter The Number: "); scanf("%d",&num); binary(num); printf("\n\n\n\n\nPress any key to exit....."); getch(); } //function to convert deciaml to binary int binary (int n) { int r; r=n%2;

n=n/2; if (n==0) { printf("\nThe binary equivalent is %d",r); return(r); } else binary(n); printf("%d",r); }

//To find absolute value of number entered through keyboard #include<stdio.h> #include<conio.h> void main() { int no; clrscr(); printf("\nEnter any number:"); scanf("%d",&no); no=no*-1; printf("\nThe absolute value of the given number is %d", no); printf("\n\n\n\n\nPress any key to exit..."); getch(); }

Generate all ARMSTRONG numbers between 1 to 500 #include <stdio.h> #include <conio.h> void main() { int i=1,a,b,c; clrscr(); printf("ARMSTRONG NUMBERS BETWEEN 1 to 500 ARE \n"); while (i<=500) { a=i%10; /*Extract Last Digit */ b=i%100; b=(b-a)/10; /*Extract First Digit */ c=i/100;/*Extract first Digit*/ if ((a*a*a)+(b*b*b)+(c*c*c)==i) printf("%d\n",i);

i++; } printf("\n\n\n\n\nPress anyKey To Exit..."); getch(); }

/*program checking for collinear points*/ #include<stdio.h> #include<conio.h> #include<math.h> void main() { int x1,y1,x2,y2,x3,y3; int s1,s2,s3; clrscr(); printf("nEnter the value of x1 and y1 of first point:"); scanf("%d%d",&x1,&y1); printf("nEnter the value of x2 and y2 of second point:"); scanf("%d%d",&x2,&y2); printf("nEnter the value of x3 and y3 of third point:"); scanf("%d%d",&x3,&y3); /*calculate slope of line between each pair of point*/ s1=abs(x2-x1)/abs(y2-y1); s2=abs(x3-x1)/abs(y3-y1); s3=abs(x3-x2)/abs(y3-y2); if((s1==s2)&&(s1==s3)) printf("ntGiven points are collinear"); else printf("ntGiven points are NoT collinear"); printf("nnnnnnPress any key to exit...."); getch(); }

23) write a program in C to find out if the given point lies on x axis,y axis or on the origin

#include<stdio.h> #include<conio.h> void main() { int x,y; clrscr(); printf("nEnter the x and y coordinates of a point:n"); scanf("%d%d",&x,&y); if(x==0&&y==0) printf("point lies on origin"); else if(x==0&&y!=0) printf("nPoint lies on y-axis"); else if(x!=0&&y==0) printf("nPoint lies on x-axis"); else printf("nPoint doesn't lie on any axis,nor origin"); printf("nnnnPress any key to exit..."); getch(); } write a program in C which determine whether a given point lies inside the circle, on the circle or outside the circle #include<stdio.h> #include<conio.h> void main() { int x,y,r; int dis,d; clrscr(); printf("nEnter radius and coordinate of points of circle(x,y):n"); scanf("%d%d%d",&r,&x,&y); dis=x*x+y*y; d=r*r; if(dis==d) printf("nPoint is on the circle"); else { if(dis>d)

printf("Points is outside the circle"); else printf("Point is inside the circle"); } printf("nnnnnPress any key to exit...."); getch(); }

c program to generate and print armstrong numbers


main() { int r; long number = 0, c, sum = 0, temp; printf("Enter the maximum range upto which you want to find armstrong numbers "); scanf("%ld",&number); printf("Following armstrong numbers are found from 1 to %ld\n",number); for( c = 1 ; c <= number ; c++ ) { temp = c; while( temp != 0 ) { r = temp%10; sum = sum + r*r*r; temp = temp/10; } if ( c == sum ) printf("%ld\n", c); sum = 0; } getch(); return 0; } 1 1 1 1 2 1 1 3 3 1

Pascal triangle in c
#include<stdio.h> long factorial(int);

main() { int i, n, c; printf("Enter the number of rows you wish to see in pascal triangle\n"); scanf("%d",&n); for ( i = 0 ; i < n ; i++ ) { for ( c = 0 ; c <= ( n - i - 2 ) ; c++ ) printf(" "); for( c = 0 ; c <= i ; c++ ) printf("%ld ",factorial(i)/(factorial(c)*factorial(i-c))); printf("\n"); } } return 0;

long factorial(int n) { int c; long result = 1; for( c = 1 ; c <= n ; c++ ) result = result*c; return ( result ); }

Linear search c program


#include<stdio.h> main() { int array[100], search, c, number; printf("Enter the number of elements in array\n"); scanf("%d",&number); printf("Enter %d numbers\n", number); for ( c = 0 ; c < number ; c++ ) scanf("%d",&array[c]); printf("Enter the number to search\n"); scanf("%d",&search); for ( c = 0 ; c < number ; c++ ) { if ( array[c] == search ) /* if required element found */ {

printf("%d is present at location %d.\n", search, c+1); break; } } if ( c == number ) printf("%d is not present in array.\n", search); return 0;

C programming code for binary search


#include<stdio.h> main() { int c, first, last, middle, n, search, array[100]; printf("Enter number of elements\n"); scanf("%d",&n); printf("Enter %d integers\n", n); for ( c = 0 ; c < n ; c++ ) scanf("%d",&array[c]); printf("Enter value to find\n"); scanf("%d",&search); first = 0; last = n - 1; middle = (first+last)/2; while( first <= last ) { if ( array[middle] < search ) first = middle + 1; else if ( array[middle] == search ) { printf("%d found at location %d.\n", search, middle+1); break; } else last = middle - 1; middle = (first + last)/2; } if ( first > last ) printf("Not found! %d is not present in the list.\n", search); } return 0;

c program to reverse an array


#include<stdio.h> #include<conio.h> main() { int n, c, j, temp, a[100]; printf("Enter the number of elements in array\n"); scanf("%d",&n); printf("Enter the array elements\n"); for ( c = 0 ; c < n ; c++ ) scanf("%d",&a[c]); if( n%2 == 0 ) c = n/2 - 1; else c = n/2; for ( j = 0 ; j < c ; j++ ) { temp = a[j]; a[j] = a[n -j - 1]; a[n-j-1] = temp; } printf("Reverse array is\n"); for( c = 0 ; c < n ; c++ ) printf("%d\n", a[c]); getch(); return 0; }

c program to insert an element in an array


#include<stdio.h> main() {

int array[100], position, c, n, value; printf("Enter number of elements in array\n"); scanf("%d", &n); printf("Enter %d elements\n", n); for ( c = 0 ; c < n ; c++ ) scanf("%d", &array[c]);

printf("Enter the location where you wish to insert an element\n");

scanf("%d", &position); printf("Enter the value to insert\n"); scanf("%d", &value); for ( c = n - 1 ; c >= position - 1 ; c-- ) array[c+1] = array[c]; array[position-1] = value; printf("Resultant array is\n"); for( c = 0 ; c <= n ; c++ ) printf("%d\n", array[c]); return 0; }

c program to delete an element from an array


#include<stdio.h> main() {

int array[100], position, c, n; printf("Enter number of elements in array\n"); scanf("%d", &n); printf("Enter %d elements\n", n); for ( c = 0 ; c < n ; c++ ) scanf("%d", &array[c]); printf("Enter the location where you wish to delete element\n"); scanf("%d", &position); if ( position >= n+1 ) printf("Deletion not possible.\n"); else { for ( c = position - 1 ; c < n - 1 ; c++ ) array[c] = array[c+1]; printf("Resultant array is\n"); for( c = 0 ; c < n - 1 ; c++ ) printf("%d\n", array[c]);

} }

return 0;

Bubble sort algorithm in c


/* Bubble sort code */

#include<stdio.h> main() { int array[100], n, c, d, swap; printf("Enter number of elements\n"); scanf("%d", &n); printf("Enter %d integers\n", n); for ( c = 0 ; c < n ; c++ ) scanf("%d", &array[c]); for ( c = 0 ; c < ( n - 1 ) ; c++ ) { for ( d = 0 ; d < n - c - 1 ; d++ ) { if ( array[d] > array[d+1] ) { swap = array[d]; array[d] = array[d+1]; array[d+1] = swap; } } } printf("Sorted list in ascending order:\n"); for ( c = 0 ; c < n ; c++ ) printf("%d\n", array[c]); return 0; }

Insertion sort algorithm implementation in c


/* insertion sort ascending order */ #include<stdio.h> main() { int array[100], n, c, d, swap, k; printf("Enter number of elements\n"); scanf("%d", &n); printf("Enter %d integers\n", n); for ( c = 0 ; c < n ; c++ ) scanf("%d", &array[c]); for ( c = 1 ; c <= n - 1 ; c++ ) { for ( d = 0 ; d <= c - 1 ; d++ )

if ( array[c] < array[d] ) { swap = array[d]; array[d] = array[c]; for ( k = c ; k > d ; k-- ) array[k] = array[k-1]; array[k+1] = swap; }

} }

printf("Sorted list in ascending order:\n"); for ( c = 0 ; c < n ; c++ ) printf("%d\n", array[c]); return 0; }

c program to add two matrix


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

} getch(); return 0; }

Subtract matrices
C code to subtract matrices of any order. This program finds difference between corresponding elements of two matrices and then print the resultant matrix.

C code
#include<stdio.h> #include<conio.h> main() { int m, n, c, d, first[10][10], second[10][10], difference[10][10]; printf("Enter the number of rows and columns of matrix\n"); scanf("%d%d",&m,&n); printf("Enter the elements of first matrix\n"); for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) scanf("%d",&first[c][d]); printf("Enter the elements of second matrix\n"); for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) scanf("%d",&second[c][d]); for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) difference[c][d] = first[c][d] - second[c][d]; printf("difference of entered matrices:-\n"); for ( c = 0 ; c < m ; c++ ) { for ( d = 0 ; d < n ; d++ ) printf("%d\t",difference[c][d]); printf("\n"); } getch(); return 0; }

C matrix multiplication program


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

main() { int m, n, p, q, c, d, k, sum = 0; int first[10][10], second[10][10], mul[10][10]; printf("Enter the number of rows and columns of first matrix\n"); scanf("%d%d",&m,&n); printf("Enter the elements of first matrix\n"); for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) scanf("%d",&first[c][d]); printf("Enter the number of rows and columns of second matrix\n"); scanf("%d%d",&p,&q); if ( n != p ) printf("Matrices with entered orders can't be multiplied with each other.\n"); else { printf("Enter the elements of second matrix\n"); for ( c = 0 ; c < p ; c++ ) for ( d = 0 ; d < q ; d++ ) scanf("%d",&second[c][d]); for ( c = 0 ; c < m ; c++ ) { for ( d = 0 ; d < n ; d++ ) { for ( k = 0 ; k < p ; k++ ) { sum = sum + first[c][k]*second[k][d]; } mul[c][d] = sum; sum = 0;

} }

printf("Product of entered matrices:-\n"); for ( c = 0 ; c < m ; c++ ) { for ( d = 0 ; d < q ; d++ ) printf("%d\t",mul[c][d]); printf("\n"); } }

getch(); return 0;

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 printf("Entered strings are not equal.\n"); } return 0;

C program to compare two strings without using strcmp


Here we create our own function to compare strings.
int compare(char a[], char b[]) { int c = 0; while( a[c] == b[c] ) { if( a[c] == '\0' || b[c] == '\0' ) break; c++; } if( a[c] == '\0' && b[c] == '\0' ) return 0; else return -1; }

C program to compare two strings using pointers


In this method we will make our own function to perform string comparison, we will use character pointers in our function to manipulate string.
#include<stdio.h> int compare_string(char*, char*); main() {

char first[100], second[100], result; printf("Enter first string\n"); gets(first); printf("Enter second string\n"); gets(second); result = compare_string(first, second); if ( result == 0 ) printf("Both strings are same.\n"); else printf("Entered strings are not equal.\n"); return 0; } int compare_string(char *first, char *second) { while(*first==*second) { if ( *first == '\0' || *second == '\0' ) break; first++; second++;

} if( *first == '\0' && *second == '\0' ) return 0; else return -1;

C substring code
#include<stdio.h> #include<malloc.h> char* substring(char*, int, int); main() { char string[100], *pointer; int position, length; printf("Enter a string\n"); gets(string); printf("Enter the position and length of substring\n"); scanf("%d%d",&position, &length); pointer = substring( string, position, length); printf("Required substring is \"%s\"\n", pointer);

free(pointer); return 0; } char *substring(char *string, int position, int length) { char *pointer; int c; pointer = malloc(length+1); if( pointer == NULL ) { printf("Unable to allocate memory.\n"); exit(EXIT_FAILURE); } for( c = 0 ; c < position -1 ; c++ ) string++; for( c = 0 ; c < length ; c++ ) { *(pointer+c) = *string; string++; } *(pointer+c) = '\0'; return pointer; }

C code for all substrings of a string


#include<stdio.h> #include<string.h> #include<malloc.h> char* substring(char*, int, int); main() { char string[100], *pointer; int position = 1, length = 1, temp, string_length; printf("Enter a string\n"); gets(string); temp = string_length = strlen(string); printf("Substring of \"%s\" are\n", string); while ( position <= string_length ) { while ( length <= temp ) { pointer = substring(string, position, length);

} }

} temp--; position++; length = 1;

printf("%s\n", pointer); free(pointer); length++;

return 0;

c program remove spaces, blanks from a string


C code to remove spaces or excess blanks from a string, For example consider the string
"c programming"

there are two spaces in this string, so our program will print a string "c programming". It will remove spaces when they occur more than one time consecutively in string anywhere.

C code
#include<stdio.h> #include<string.h> #include<malloc.h> #define SPACE ' ' main() { char string[100], *blank, *start, *end; int length; printf("Enter a string\n"); gets(string); length = strlen(string); blank = string; end = start = (char*)malloc(length+1); while(*blank) { if ( *blank == SPACE && *(blank+1) == SPACE ) {} else { *start = *blank; start++; } blank++;

} *start='\0'; start = end; printf("%s", start); return 0; }

strupr in c
#include<stdio.h> #include<string.h> main() { char string[] = "strupr in c"; printf("%s\n",strupr(string)); } return 0;

Change string to upper case without strupr


#include<stdio.h> void upperString(char*); main() { char string[100]; printf("Enter a string to convert it into upper case\n"); gets(string); upperString(string); printf("Entered string in upper case is \"%s\"\n", string); return 0; } void upperString(char *string) { while(*string) { if ( *string >= 'a' && *string <= 'z' ) { *string = *string - 32; } string++; } }

Change string to lower case without strlwr

#include<stdio.h> void lowerString(char*); main() { char string[100]; printf("Enter a string to convert it into lower case\n"); gets(string); lowerString(string); printf("Entered string in lower case is \"%s\"\n", string); return 0; } void lowerString(char *string) { while(*string) { if ( *string >= 'A' && *string <= 'Z' ) { *string = *string + 32; } string++; } }

C programming code
#include<stdio.h> #include<string.h> #include<malloc.h> #include<conio.h> main() { char first[100], second[100], *temp; printf("Enter the first string "); gets(first); printf("Enter the second string "); gets(second); printf("\nBefore Swapping\n"); printf("First string: %s\n",first); printf("Second string: %s\n\n",second); temp = (char*)malloc(100); strcpy(temp,first); strcpy(first,second); strcpy(second,temp);

printf("After Swapping\n"); printf("First string: %s\n",first); printf("Second string: %s\n",second); getch(); return 0; }

c program to find frequency of characters in a string


#include<stdio.h> #include<string.h> main() { char string[100], ch; int c = 0, count[26] = {0}; printf("Enter a string\n"); gets(string); while ( string[c] != '\0' ) { /* * Considering characters from 'a' to 'z' only */ if ( string[c] >= 'a' && string[c] <= 'z' ) count[string[c]-'a']++; c++; } for ( c = 0 ; c < 26 ; c++ ) { if( count[c] != 0 ) printf("%c occurs %d times in the entered string.\n",c+'a',count[c]); } return 0; }

C anagram programming code


#include<stdio.h> int check(char [], char []); main() { char a[100], b[100]; int flag; printf("Enter first string\n"); gets(a);

printf("Enter second string\n"); gets(b); flag = check(a, b); if ( flag == 1 ) printf("\"%s\" and \"%s\" are anagrams.\n", a, b); else printf("\"%s\" and \"%s\" are not anagrams.\n", a, b); return 0; } int check(char a[], char b[]) { int first[26] = {0}, second[26] = {0}, c = 0; while ( a[c] != '\0' ) { first[a[c]-'a']++; c++; } c = 0; while ( b[c] != '\0' ) { second[b[c]-'a']++; c++; } for ( c = 0 ; c < 26 ; c++ ) { if( first[c] != second[c] ) return 0; } } return 1;

Selection sort algorithm implementation in c


#include<stdio.h> main() { int array[100], n, c, d, swap; printf("Enter number of elements\n"); scanf("%d", &n); printf("Enter %d integers\n", n); for ( c = 0 ; c < n ; c++ ) scanf("%d", &array[c]);

for ( c = 0 ; c < ( n - 1) ; c++ ) { for ( d = ( c + 1 ) ; d <= ( n - 1 ) ; d++ ) { if ( array[c] > array[d] ) { swap = array[c]; array[c] = array[d]; array[d] = swap; } } } printf("Sorted list in ascending order:\n"); for ( c = 0 ; c < n ; c++ ) printf("%d\n", array[c]); } return 0;

c program to sort a string in alphabetic order


#include<stdio.h> #include<stdlib.h> #include<string.h> main() { char string[100], ch, *pointer, *result, *temp; int c, length; printf("Enter a string\n"); gets(string); length = strlen(string); temp = result = (char*)malloc(length+1); pointer = string; for ( ch = 'a' ; ch <= 'z' ; ch++ ) { for ( c = 0 ; c < length ; c++ ) { if ( *pointer == ch ) { *result = *pointer; result++; } pointer++; } pointer = string; } *result = '\0'; result = temp;

strcpy(string, result); free(result); printf("%s\n", string); return 0; }

c program to read a file


#include<stdio.h> #include<conio.h> #include<stdlib.h> main() { char ch, fname[25]; FILE *fp; printf("Enter the name of file you wish to see "); gets(fname); fp = fopen(fname,"r"); if( fp == NULL ) { perror("Error "); printf("Press any key to exit..."); getch(); exit(1); } printf("The contents of %s file are :- \n\n",fname); while( ( ch = fgetc(fp) ) != EOF) printf("%c",ch); getch(); fclose(fp); return 0;

c program to copy files


#include<stdio.h> #include<conio.h> #include<stdlib.h> int main() { char ch, file1[20], file2[20]; FILE *fs,*ft; printf("Enter name of file to copy ");

gets(file1); fs = fopen(file1,"r"); if( fs == NULL ) { perror("Error "); printf("Press any key to exit...\n"); getch(); exit(EXIT_FAILURE); } printf("Enter name of target file "); gets(file2); ft = fopen(file2,"w"); if( ft == NULL ) { perror("Error "); fclose(fs); printf("Press any key to exit...\n"); getch(); exit(EXIT_FAILURE); } while( ( ch = fgetc(fs) ) != EOF ) fputc(ch,ft); printf("File copied successfully.\n"); getch(); fclose(fs); fclose(ft); return 0;

c program to merge two files


#include<stdio.h> #include<conio.h> #include<stdlib.h> main() { FILE *fs1, *fs2, *ft; char ch, file1[20], file2[20], file3[20]; printf("Enter name of first file "); gets(file1); printf("Enter name of second file "); gets(file2); printf("Enter name of file which will store contents of two files "); 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); getch(); return 0;

c program to list files in directory


#include<stdio.h> #include<conio.h> #include<dir.h> main() { int done; struct ffblk a; printf("Press any key to view the files in the current directory\n"); getch(); done = findfirst("*.*",&a,0); while(!done) { printf("%s\n",a.ff_name); done = findnext(&a); }

getch(); return 0;

c program to delete a file


#include<stdio.h> #include<conio.h> int main() { int status; char fname[25]; printf("Enter the name of file you wish to delete "); gets(fname); status = remove(fname); if( status == 0 ) printf("%s file deleted successfully\n",fname); else { printf("Unable to delete the file\n"); perror("Error "); } getch(); return 0;

c program to generate random numbers


#include<stdio.h> #include<conio.h> #include<stdlib.h> main() { int n, max, num, c; printf("Enter the number of random numbers you want "); scanf("%d",&n); printf("Enter the maximum value of random number "); scanf("%d",&max); printf("%d random numbers from 0 to %d are :-\n",n,max); randomize(); for ( c = 1 ; c <= n ; c++ ) { num = random(max); printf("%d\n",num); }

getch(); return 0;

c program to add two complex numbers


#include<stdio.h> #include<conio.h> #include<stdlib.h> struct complex { int real; int img; }; main() { struct complex a, b, c; printf("Enter a and b where a + ib is the first complex number."); printf("\na = "); scanf("%d", &a.real); printf("b = "); scanf("%d", &a.img); printf("Enter c and d where c + id is the second complex number."); printf("\nc = "); scanf("%d", &b.real); printf("d = "); scanf("%d", &b.img); c.real = a.real + b.real; c.img = a.img + b.img; if ( c.img >= 0 ) printf("Sum of two complex numbers = %d + %di",c.real,c.img); else printf("Sum of two complex numbers = %d %di",c.real,c.img); getch(); return 0;

c program to print date


#include<stdio.h> #include<conio.h> #include<dos.h> main() { struct date d; getdate(&d);

printf("Current system date is %d/%d/ %d",d.da_day,d.da_mon,d.da_year); getch(); return 0; }

c program to get ip address


#include<stdlib.h> main() { system("C:\\Windows\\System32\\ipconfig"); system("pause"); } return 0;

C program to shutdown or turn off computer


#include<stdio.h> #include<stdlib.h> main() { char ch; printf("Do you want to shutdown your computer now (y/n) "); scanf("%c",&ch); if( ch == 'y' || ch == 'Y' ) system("C:\\WINDOWS\\System32\\shutdown -s"); return 0; }

C programming code for Windows 7


#include<stdio.h> #include<stdlib.h> main() { char ch; printf("Do you want to shutdown your computer now (y/n)\n"); scanf("%c",&ch); if( ch == 'y' || ch == 'Y' ) system("C:\\WINDOWS\\System32\\shutdown /s"); } return 0;

You might also like