Sunteți pe pagina 1din 17

Armstrong number c program

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> int main() { int number, sum = 0, temp, remainder; printf("Enter an integer\n"); scanf("%d",&number); temp = number; while( temp != 0 ) { remainder = temp%10; sum = sum + remainder*remainder*remainder; temp = temp/10; } if ( number == sum ) printf("Entered number is an armstrong number.\n"); else printf("Entered number is not an armstrong number.\n"); return 0; }

Output of program:

C program to print patterns of numbers and stars


These program prints various different patterns of numbers and stars. These codes illustrate how to create various patterns using c programming. Most of these c programs involve usage of nested loops and space. A pattern of numbers, star or characters is a way of arranging these in some logical manner or they may form a sequence. Some of these patterns are triangles which have special importance in mathematics. Some patterns are symmetrical while other are not. Please see the complete page and look at comments for many different patterns.
* *** ***** ******* *********

We have shown five rows above, in the program you will be asked to enter the numbers of rows you want to print in the pyramid of stars.

C programming code
#include <stdio.h> int main() { int row, c, n, temp; printf("Enter the number of rows in pyramid of stars you wish to see "); scanf("%d",&n); temp = n; for ( row = 1 ; row <= n ; row++ ) { for ( c = 1 ; c < temp ; c++ ) printf(" "); temp--; for ( c = 1 ; c <= 2*row - 1 ; c++ ) printf("*"); printf("\n"); } return 0; }

Output of program:

C program to check leap year


C program to check leap year: c code to check leap year, year will be entered by the user.

C programming code
#include <stdio.h> int main() { int year; printf("Enter a year to check if it is a leap year\n"); scanf("%d", &year); if ( year%400 == 0) printf("%d is a leap year.\n", year); else if ( year%100 == 0) printf("%d is not a leap year.\n", year); else if ( year%4 == 0 ) printf("%d is a leap year.\n", year); else printf("%d is not a leap year.\n", year); return 0; }

Output of program:

C program to sort a string in alphabetic order


C program to sort a string in alphabetic order: For example if user will enter a string "programming" then output will be "aggimmnoprr" or output string will contain characters in alphabetical order.

C programming code
#include <stdio.h> #include <stdlib.h> #include <string.h> void sort_string(char*); int main() { char string[100]; printf("Enter some text\n"); gets(string); sort_string(string); printf("%s\n", string); return 0; } void sort_string(char *s) { int c, d = 0, length; char *pointer, *result, ch; length = strlen(s); result = (char*)malloc(length+1); pointer = s;

for ( ch = 'a' ; ch <= 'z' ; ch++ ) { for ( c = 0 ; c < length ; c++ ) { if ( *pointer == ch ) { *(result+d) = *pointer; d++; } pointer++; } pointer = s; } *(result+d) = '\0'; strcpy(s, result); free(result); }

Output of program:

C program to transpose a matrix


This c program prints transpose of a matrix. It is obtained by interchanging rows and columns of a matrix. For example if a matrix is 12 34 56 then transpose of above matrix will be 135 246 When we transpose a matrix then the order of matrix changes, but for a square matrix order remains same.

C programming code
#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; }

Output of program:

C program to print diamond pattern

Diamond pattern in c: This code print diamond pattern of stars. Diamond shape is as follows:
* *** ***** *** *

C programming code
#include <stdio.h> int main() { int n, c, k, space = 1; printf("Enter number of rows\n"); scanf("%d", &n); space = n - 1; for (k = 1; k <= n; k++) { for (c = 1; c <= space; c++) printf(" "); space--; for (c = 1; c <= 2*k-1; c++) printf("*"); printf("\n"); } space = 1; for (k = 1; k <= n - 1; k++) { for (c = 1; c <= space; c++) printf(" "); space++; for (c = 1 ; c <= 2*(n-k)-1; c++) printf("*"); printf("\n"); } return 0; }

Output of program:

C program to Reverse a Sentence Using Recusrion


This program takes a sentence from user and reverses that sentence using recursion. This program does not use string to reverse the sentence or store the sentence.

Source code to reverse a sentence using recursion.


/* Example to reverse a sentence entered by user without using strings. */ #include <stdio.h> void Reverse(); int main() { printf("Enter a sentence: "); Reverse(); return 0;

} void Reverse() { char c; scanf("%c",&c); if( c != '\n') { Reverse(); printf("%c",c); } } Output Enter a sentence: margorp emosewa awesome program

C program to display its own Source Code


/* The file name of this C program should be sourcecode.c */ #include<stdio.h> int main(){ FILE *fp; char c; fp = fopen("sourcecode.c","r"); do{ c= getc(fp); putchar(c); } while(c!=EOF); fclose(fp); return 0; }

Source code to display its own source code Using Macro


A predefined macro __FILE__ contains the location of a C programming file it is working on. For example:

#include <stdio.h> int main(){ printf("%s",__FILE__); } The output of this program is the location of this C programming file.

C program to display its own source code using __FILE__


#include<stdio.h> int main(){ FILE *fp; char c; fp = fopen(__FILE__,"r"); do{ c= getc(fp); putchar(c); } while(c!=EOF); fclose(fp); return 0; } This program displays the content of this particular C programming file(source code) because __FILE__ contains the location of this C programming file in a string.

Program to Print All ASCII Values in C Programming Program :


#include<stdio.h> #include<conio.h> void main() { int i=0; char ch; clrscr();

for(i=0;i<256;i++) { printf("%c ",ch); ch = ch + 1; } }

Output :
! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~

C Program to Find the Number of Non Repeated Elements in an Array


'; This C Program finds the number of non repeated elements in an array. Here is source code of the C Program to find the number of non repeated elements in an array. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
1. /*

2. * C Program to Find the Number of Non Repeated Elements in an Array 3. */ 4. #include <stdio.h> 5. int main() 6. { 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. } printf("\n The array after removing duplicates is: "); for (i = 0; i < size; i++) } } } j = 0; } else if (*(ptr + i) == *(ptr + j)) { k = j; size--; while (k < size) { *(ptr + k) = *(ptr + k + 1); k++; printf("\n Enter size of the array: "); scanf("%d", &n); printf("\n Enter %d elements of an array: ", n); for (i = 0; i < n; i++) scanf("%d", &array[i]); size = n; ptr = array; for (i = 0; i < size; i++) { for (j = 0; j < size; j++) { if (i == j) { continue; int array[50]; int *ptr; int i, j, k, size, n;

41. 42. 43. 44. 45. }

{ printf(" %d", array[i]); } return 0;

$ cc pgm76.c $ a.out Enter size of the array: 6 Enter 6 elements of an array: 12 10 4 10 12 56 The array after removing duplicates is: 12 10 4 56

C Program to identify missing Numbers in a given Array


'; This C Program identifies missing numbers in a given array. Here is source code of the C Program to identify missing numbers in a given array. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
1. /* 2. * C Program to identify missing numbers in a given array 3. */ 4. #include <stdio.h>

5. 6. void main() 7. { 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. } printf("Enter size of array : "); scanf("%d", &n); int array[n - 1]; /* array size-1 */ printf("Enter elements into array : \n"); for (i = 0; i < n - 1; i++) scanf("%d", &array[i]); b = array[0]; for (i = 1; i < n - 1; i++) b = b ^ array[i]; for (i = 2, c = 1; i <= n; i++) c = c ^ i; c = c ^ b; printf("Missing element is : %d \n", c); int n, i, j, c, t, b;

$ cc bit30.c $ a.out Enter size of array : 6 Enter elements into array : 1 2 3 5 6 Missing element is : 4

C Program to Copy One String to Another using Recursion


';

This C Program uses recursive function & copies a string entered by user from one character array to another character array. Here is the source code of the C program to copy string using recursion. The C Program is successfully compiled and run on a Linux system. The program output is also shown below.
1. /* 2. * C Program to Copy One String to Another using Recursion 3. */ 4. #include <stdio.h> 5. 6. void copy(char [], char [], int); 7. 8. int main() 9. { 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. } void copy(char str1[], char str2[], int index) { str2[index] = str1[index]; if (str1[index] == '\0') return; copy(str1, str2, index + 1); } printf("Enter string to copy: "); scanf("%s", str1); copy(str1, str2, 0); printf("Copying success.\n"); printf("The first string is: %s\n", str1); printf("The second string is: %s\n", str2); return 0; char str1[20], str2[20];

$ cc pgm10.c $ a.out Enter string to copy: sanfoundry

Copying success. The first string is: sanfoundry The second string is: sanfoundry

C Program to Sort Word in String


'; This C Program Sort Word in String. Here is source code of the C Program to Sort Word in String. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
1. /* 2. * C Program to Sort Word in String 3. */ 4. #include <stdio.h> 5. #include <string.h> 6. 7. void main() 8. { 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. } for (i = 0, j = 0, k = 0;j < strlen(str);j++) { if ((str[j] == ' ')||(str[j] == 44)||(str[j] == 46)) { p[i][k] = '\0'; } printf("Enter the string\n"); scanf(" %[^\n]s", str); for (i = 0;i < strlen(str);i++) { if ((str[i] == ' ')||(str[i] == ', ')||(str[i] == '.')) { space++; int count = 0, c = 0, i, j = 0, k, l, space = 0; char str[100], p[50][100], str1[20], ptr1[50][100], cmp[50];

26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. } } } } } } else

i++; k = 0;

p[i][k++] = str[j]; for (i = 0;i < space;i++) { for (j = i + 1;j <= space;j++) { if ((strcmp(p[i], p[j]) > 0)) { strcpy(cmp, p[i]); strcpy(p[i], p[j]); strcpy(p[j], cmp); } //loop for sorting

printf("After sorting string is \n"); for (i = 0;i <= space;i++) { printf("%s ", p[i]);

$ cc string18.c $ a.out Enter the string welcome to sanfoundry's c programming class, welcome to c class again After sorting string is again c c class class programming sanfoundry's to to welcome welcome

S-ar putea să vă placă și