c Program Nigga
-
Upload
rajiv-vaidya -
Category
Documents
-
view
223 -
download
0
Transcript of c Program Nigga
-
7/22/2019 c Program Nigga
1/17
Armstrong number c programArmstrong 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
intmain(){
intnumber,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");
return0;}
Output of program:
-
7/22/2019 c Program Nigga
2/17
C program to print patterns ofnumbers and starsThese 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
intmain(){
introw,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
-
7/22/2019 c Program Nigga
3/17
Output of program:
C program to check leap yearC program to check leap year: c code to check leap year, year will be entered by the user.
C programming code#include
intmain(){
intyear;
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);
elseif(year%100==0)printf("%d is not a leap year.\n",year);
elseif(year%4==0)printf("%d is a leap year.\n",year);
elseprintf("%d is not a leap year.\n",year);
return0;}
-
7/22/2019 c Program Nigga
4/17
Output of program:
C program to sort a string inalphabetic orderC 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 #include #include
voidsort_string(char*);
intmain(){
charstring[100];
printf("Enter some text\n");gets(string);
sort_string(string);printf("%s\n",string);
return0;}
voidsort_string(char*s){
intc,d =0,length;char*pointer,*result,ch;
length =strlen(s);
result =(char*)malloc(length+1);
pointer =s;
-
7/22/2019 c Program Nigga
5/17
for(ch ='a';ch
-
7/22/2019 c Program Nigga
6/17
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
-
7/22/2019 c Program Nigga
7/17
Diamond pattern in c: This code print diamond pattern of stars. Diamond shape is as follows:
*
***
*****
***
*
C programming code#include
intmain(){
intn,c,k,space =1;
printf("Enter number of rows\n");scanf("%d",&n);
space =n -1;
for(k =1;k
-
7/22/2019 c Program Nigga
8/17
Output of program:
C program to Reverse a SentenceUsing Recusrion
This program takes a sentence from user and reverses that s entence using recursion. This
program does not use st ring 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
voidReverse() ;
intmain()
{
pr intf("Enter a sentence: ");
Reverse() ;
return0;
-
7/22/2019 c Program Nigga
9/17
}
voidReverse()
{
charc ;
scanf("%c",&c);
if(c != '\n')
{
Reverse() ;
pr in tf("%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 */
#includeintmain(){
FILE *fp;
charc ;fp =fopen("sourcecode.c","r ");
do{c=getc(fp);
putcha r(c);
}while(c!=EOF);
fclose(fp );
return0;
}
Source code to display its own source code Using
Macro
A predefined macro __FILE__ contains the location of a C programming fi le it is working
on. For example:
-
7/22/2019 c Program Nigga
10/17
#include
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
intmain(){
FILE *fp;
charc ;fp =fopen(__FILE__,"r");
do{
c=getc(fp);
putcha r(c);
}
while(c!=EOF);
fclose(fp );
return0;
}
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
#includevoid main()
{
int i=0;
char ch;
clrscr();
-
7/22/2019 c Program Nigga
11/17
for(i=0;i
-
7/22/2019 c Program Nigga
12/17
2. * C Program to Find the Number of Non Repeated Elements in an Array
3. */
4. #include
5. intmain()
6. {7. intarray[50];
8. int*ptr;
9. inti,j,k,size,n;
10.
11. printf("\nEnter size of the array: ");
12. scanf("%d",&n);
13. printf("\nEnter %d elements of an array: ",n);
14. for(i =0;i
-
7/22/2019 c Program Nigga
13/17
41. {
42. printf(" %d",array[i]);
43. }
44. return0;
45. }
$ ccpgm76.c
$ a.out
Enter sizeof the array: 6
Enter 6elements of an array: 12
104
10
12
56
The array after removing duplicates is: 1210456
C Program to identify missing Numbers ina 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
-
7/22/2019 c Program Nigga
14/17
5.
6. voidmain()
7. {
8. intn,i,j,c,t,b;
9.10. printf("Enter size of array : ");
11. scanf("%d",&n);
12. intarray[n -1]; /* array size-1 */
13. printf("Enter elements into array : \n");
14. for(i =0;i
-
7/22/2019 c Program Nigga
15/17
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
5.
6. voidcopy(char[],char[],int);
7.
8. intmain()
9. {
10. charstr1[20],str2[20];
11.
12. printf("Enter string to copy: ");
13. scanf("%s",str1);
14. copy(str1,str2,0);
15. printf("Copying success.\n");
16. printf("The first string is: %s\n",str1);
17. printf("The second string is: %s\n",str2);
18. return0;
19. }
20.
21. voidcopy(charstr1[],charstr2[],intindex)
22. {
23. str2[index]=str1[index];
24. if(str1[index]=='\0')
25. return;
26. copy(str1,str2,index +1);
27. }
$ ccpgm10.c
$ a.out
Enter string to copy: sanfoundry
-
7/22/2019 c Program Nigga
16/17
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
5. #include
6.
7. voidmain()
8. {
9. intcount =0,c =0,i,j =0,k,l,space =0;
10. charstr[100],p[50][100],str1[20],ptr1[50][100],cmp[50];
11.
12. printf("Enter the string\n");
13. scanf(" %[^\n]s",str);
14. for(i =0;i
-
7/22/2019 c Program Nigga
17/17
26. i++;
27. k =0;
28. }
29. else
30. p[i][k++]=str[j];31. }
32. for(i =0;i