Sie sind auf Seite 1von 16

STAND-ALONE PROGRAMS

Alpha, Beta, Gamma: /* Ask user to enter n(integer) and c(character). Generate output according to given conditions */ #include <stdio.h> #include <conio.h> void main(void) { clrscr(); int n; char c; //USER INPUT printf("Enter n: "); scanf("%d", &n); printf("\nEnter c: "); c=getche(); //CONDITION if(n==100 && c=='P' || c=='p') printf("\n\nALPHA"); else if(n>=20 && n<=30 && c=='q' || c=='Q') printf("\n\nBETA"); else if(n>100 && c!='r') printf("\n\nGAMMA"); getch(); }

Consonants, Vowels, Numbers, Upper & Lower case, and Spaces Counter: /* This program will count the vowels, consonants, upper case, lower case, numbers and space in a string entered by the user. */ #include <stdio.h> #include <conio.h> #include <string.h>

void main(void) { clrscr(); int y; char d[255]; char t[255]; char up[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"; char low[]="abcdefghijklmnopqrstuvwxyz"; char con[]="bcdfghjklmnpqrstvwxyz"; //USER INPUT printf("Enter the string you wish to analyze: "); gets(d); //PREPARING... int l=strlen(d); strcpy(t, d); strlwr(t); int vow=0, cons=0, sp=0, num=0, upc=0, lwc=0; //COMPUTING VOWELS, SPACES & NUMBERS... for(y=0; y<l; y++) { //VOWELS if(t[y]=='a' || t[y]=='e' || t[y]=='i' || t[y]=='o' || t[y]=='u') vow++; //SPACES if(t[y]==' ') sp++; //NUMBERS if(t[y]=='0' || t[y]=='1' || t[y]=='2' || t[y]=='3' || t[y]=='4' || t[y]=='5' || t[y]=='6' || t[y]=='7' || t[y]=='8' || t[y]=='9') num++; } //CHECKING CASES...

for(int x=0; x<26; x++) { for(int z=0; z<l; z++) { if(d[z]==up[x]) upc++; if(d[z]==low[x]) lwc++; } } //CONSONANTS for(int b=0; b<21; b++) { for(int f=0; f<l; f++) { if(t[f]==con[b]) cons++; } } printf("\n\nVowels = %d", vow); printf("\nConsonants = %d", cons); printf("\nUpper Case = %d", upc); printf("\nLower Case = %d", lwc); printf("\nNumbers = %d", num); printf("\nSpaces = %d", sp); printf("\nOther Characters = %d", l-vow-cons-num-sp); getch(); }

Simple Calculator: /* This program will take input of two numbers and computer their addition, subtration, multiplaction, or division. */ #include <stdio.h> #include <conio.h> void main(void) { clrscr();

double a, b, ans; char c, l; //USER INPUT do { printf("Enter the first number: "); scanf("%lf", &a); printf("\nEnter the second number: "); scanf("%lf", &b); //MENU printf("\nFollowing functions can be performed on these numbers:\n"); printf("\nAddition (A)\nSubtraction (S)\nMultiplication (M)\nDivision (D)"); printf("\n\nEnter your choice: "); c=getche(); //CALCULATION switch(c) { case 'A': case 'a': ans=a+b; break; case 'S': case 's': ans=a-b; break; case 'M': case 'm': ans=a*b; break; case 'D': case 'd': ans=a/b; break;

} printf("\n\nThe required answer is %g.", ans);

//LOOPING printf("\n\nPress Y to perform more calculations OR any other key to exit: "); l=getche(); printf("\n\n"); } while(l=='Y' || l=='y'); }

Day, Months, Year: Method 1: #include <conio.h> #include <stdio.h> void main(void) { clrscr(); int n; printf("Enter the number of days: "); scanf("%d", &n); float a=n/360.0; int y=a; float b=a-y; float c=b*12.0; int m=c; float x=c-m; int d=x*30; printf("No. of Years = %d\n" ,y); printf("No. of Months = %d\n" ,m); printf("No. of Days = %d\n" ,d); getch(); } Method 2: /* This program will calculate the number of years, months and days based on the input in the form of number of days using modf function.*/

#include <stdio.h> #include <conio.h> #include <math.h> void main(void) { clrscr(); int n; double a, y, b, c, m, x, d; printf("Enter the number of days: "); scanf("%d", &n); a=n/360.0; b=modf(a, &y); c=b*12.0; x=modf(c, &m); d=x*30; printf("\n\nNumber of years = %g", y); printf("\nNuber of months = %g", m); printf("\nNumber of days = %g", d); getch(); }

Detect Largest & Smallest No.: Largest: /* This program will detect the greatest number and its position. */ #include <stdio.h> #include <conio.h> void main(void) { clrscr(); int a[]={22, 16, 17, 43, 36}, x, pos, max=0; for(x=0; x<5; x++) {

if(a[x]>max) { max=a[x]; pos=x; } } printf("Max. No. = %d\nPosition = %d", max, pos); getch(); } Smallest: /* This program will detect the smallest number and its position. */ #include <stdio.h> #include <conio.h> void main(void) { clrscr(); int a[]={22, 16, 17, 43, 36}, x, pos, min=a[0]; for(x=0; x<5; x++) { if(a[x]<min) { min=a[x]; pos=x; } } printf("Min. No. = %d\nPosition = %d", min, pos); getch(); }

Detect Max. No. in a String: #include <stdio.h> #include <conio.h> void main(void) { clrscr();

int t[]={22,16,17,43,36}; int x, max=0, pos; for(x=0; x<5; x++) { if(t[x]>max) { max=t[x]; pos=x+1;}} printf("Max No.: %d\nPosition: %d", max, pos); getch(); }

Fibonacci Series: /* This program will generate Fibonacci series upto n terms. Note that 0 is considered a Fibonacci number. Also note that the maximum number of terms correctly generated are 24. This because of the limitation of "int" to hold the number of digits for Turbo C. "int" can hold maximum values between -32768 to 32767. */ #include <stdio.h> #include <conio.h> #include <stdlib.h> void main(void) { clrscr(); int n, x, a=0, b=1, fi; printf("Enter the number of terms: "); scanf("%d" ,&n); if(n==1) printf("\n0"); else { printf("\n0 1 "); for(x=1; x<=n-2; x++) { fi=a+b; a=b; b=fi; printf("%d ", fi);

} } getch(); }

nth term and its Sum: /* This programs displays the nth term of the series Tn = n^2+3n-1 and calculates the sum to nth term. */ #include <stdio.h> #include <conio.h> #include <math.h> void main(void) { clrscr(); int n, sum=0, term; //USER INPUT printf("\t\t\t\tTn = n^2+3n-1"); printf("\n\nEnter the number of elements of the series: "); scanf("%d", &n); //CALCULATIONS term=pow(n,2)+3*n-1; for(int x=1; x<=n; x++) { sum+=pow(x,2)+3*x-1; } //RESULTS printf("\nThe nth term is %d", term); printf("\nThe sum to nth term is %d", sum); getch(); }

Odd Even Detector: /* This program will check whether the entered number is even or odd. */ #include <stdio.h> #include <conio.h> void main(void) { clrscr(); int n; //USER INPUT printf("Enter an integer: "); scanf("%d", &n); //CHECKING... if(n%2==0) printf("\n%d is an even number.", n); else printf("\n%d is an odd number.", n); getch(); }

Percentage Grade Convertor: /* This program will get input from the user in the form of either percentage or Grade, and print the grade or the percentage respectively. */ #include <stdio.h> #include <conio.h> void main(void) { clrscr(); //DEFINING IDENTIFIERS char c, g, loop; float p;

//USER INPUT Adr1: printf("Enter G to find your grade OR P to find your percentage: "); c=getche(); //DECISIONS switch(c) { //SWITCHING TO FIND GRADE... case 'G': case 'g': printf("\n\nEnter your percentage: "); scanf("%g", &p); if(p<60) printf("\nYour grade is D."); else if(p>=60 && p<=69) printf("\nYour grade is C."); else if(p>=70 && p<=79) printf("\nYour grade is B."); else if(p>=80) printf("\nYour grade is A."); break; //SWITCHING TO FIND PERCENTAGE... case 'P': case 'p': Adr3: printf("\n\nEnter your grade: "); g=getche(); if(g=='D' || g=='d') printf("\nYour percentage is less than 60%%."); else if(g=='C' || g=='c') printf("\nYour percentage is between 60%% to 69%%."); else if(g=='B' || g=='b') printf("\nYour percentage is between 70%% to 79%%."); else if(g=='A' || g=='a') printf("\nYour percentage is 80%% or above."); else goto Adr3; break; default: goto Adr1;

} //CHECKING FOR MORE INPUT Adr2: printf("\nDo you wish to enter more results? (Y/N): "); loop=getche(); switch(loop) { case 'Y': case 'y': printf("\n\n"); goto Adr1; case 'n': case 'N': break; default: goto Adr2; } printf("\n\nPress any key to exit."); getch(); }

Prime Number Checker: Any Number: #include <conio.h> #include <stdio.h> void main(void) { clrscr(); //Get the number int num, i; printf("Enter a number to check whether it is a prime number: "); scanf("%d", &num); //Checking of the entered number

i=2; while(i<=(num-1)) { if(num%i==0) { printf("\nThe number you entered is not a prime number."); break; } i++; } if(i==num || num==1) { printf("\nThe number you entered is a prime number."); } getch(); } Between 1 500: /* This program will print the prime numbers between 1 - 500 */ #include <conio.h> #include <stdio.h> void main(void) { clrscr(); int n, num, i, d; for(num=1; num<=500; num++) { d=1; for(n=2; n<=num-1; n++) { if(num%n==0) d=0; } if(d==1) printf("\t%d", num); } getch();

Quadratic Equation Solver: #include <stdio.h> #include <conio.h> #include <math.h> void main(void) { clrscr(); //USER INPUT float a,b,c,r,rt; printf("Enter second degree coefficient: "); scanf("%g", &a); printf("\nEnter the coefficient of x: "); scanf("%g", &b); printf("\nEnter the constant: "); scanf("%g", &c); //Calculation r=(pow(b,2))-(4*a*c); rt=sqrt(fabs(r))/(2*a); //Decision if(r<0) { printf("\nFirst Root = %g + %gi", -b/(2*a), rt); printf("\nSecond Root = %g - %gi", -b/(2*a), rt); } else { printf("\nFirst Root = %g", (-b/(2*a))+rt); printf("\nSecond Root = %g", (-b/(2*a))-rt); } getch(); }

Smallest, Most Divisible No.: /* This program will find the most divisible number from 1 - 500. */ #include <stdio.h> #include <conio.h> void main(void) { clrscr(); int x, y, d, num=0, div=0; for(x=1; x<=500; x++) { d=0; for(y=1; y<=x; y++) { if(x%y==0) d++; } if(d>div) { div=d; num=x; } } printf("Number = %d\tDivisor = %d", num, div); getch(); }

Multiplication Table Creator: #include <conio.h> #include <stdio.h> void main(void) { clrscr(); /* Mutiplication Table of "n" */ //User INPUT

int n, a, b; printf("Enter the number whose table you wish to create: "), scanf("%d", &n); printf("\nEnter the starting multiplier: "); scanf("%d", &a); printf("\nEnter the end multiplier: "); scanf("%d", &b); printf("\n"); //LOOP while(a<=b) { printf("\n%d x %d = %d", n, a, n*a); a++; } getch(); }

Das könnte Ihnen auch gefallen