This is the code I wrote in my college days and dont quote me on this if something does not go as expected. Code: #include<stdio.h> #include<ctype.h> #include<conio.h> void main() { int c,i,letter[26]; for(i=0;i<26;i++) letter[i]=0; while((c=getchar())!='~') { if(isupper(c)) ++letter[c-'A']; if(islower(c)) ++letter[c-'a']; } for(i=0;i<26;i++) { if(i%6==0) printf("\n"); printf("%5c %4d",'A'+i,letter[i]); } printf("\n\n"); getch(); }
Here is the C program to find a prime number. Code: #include <stdio.h> void main(void) { unsigned int n,half,i,flag=1; printf("Enter the number : "); scanf("%d",&n); half = n/2; for(i=2;i<=half;i++) { if(n%i==0) flag = 0; } if(flag) printf("The number is a prime number"); else printf("The number is not a prime number"); }
Setting is the flag to 0 means that the number is not a prime number. You can set it to boolean also.
To print stars Code: #include <stdio.h> void main(void) { unsigned int n,j,i; /* n has to be a even no. */ printf("Enter the number of rows : "); scanf("%d",&n); for(i=0;i<n/2;i++) { for(j=0;j<i;j++) printf("*"); printf("\n"); } for(i=n/2;i>0;i--) { for(j=0;j<i;j++) printf("*"); printf("\n"); } }