Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<ctype.h>
- #include<math.h>
- #include<stdbool.h>
- int menu () {
- int choice;
- printf("----------------Menu-Selector-----------------\n");
- printf("------------------Assignment------------------\n");
- printf("==============================================\n");
- printf("= 1. Sum of Integers =\n");
- printf("= 2. Swap values =\n");
- printf("= 3. vowels, consonants, others counter =\n");
- printf("= 4. ASCII table =\n");
- printf("= 5. ASCII between 2 chars (ascending) =\n");
- printf("= 6. Print prime numbers =\n");
- printf("= 7. Date Checker =\n");
- printf("= 8. Relative Position of the circle =\n");
- printf("= 9. Print Factorial by n =\n");
- printf("= 10. Print Fibonacci sequence by n =\n");
- printf("= 11. Fibonacci element checker =\n");
- printf("= 12. Sum Digits =\n");
- printf("= 13. Fraction to real number convert =\n");
- printf("= 14. GCD and LCM by 2 pos int =\n");
- printf("= 15. Min Max of a nonnegative int =\n");
- printf("= 16. Small menu with process primes, minmax =\n");
- printf("= Others. Quit =\n");
- printf("==============================================\n");
- printf("Please choose one: ");
- scanf("%d", &choice);
- return choice;
- }
- //EX1
- void sumOfInt () {
- int x;
- int S = 0;
- do {
- printf("Input x: ");
- scanf("%d", &x);
- if (x != 0) {
- S = S + x;
- }
- }
- while (x != 0);
- printf("S = %d\n", S);
- }
- //EX2
- void intSwap () {
- int x;
- int y;
- int temp;
- do {
- printf("Input x: ");
- scanf("%d", &x);
- printf("Input y: ");
- scanf("%d", &y);
- temp = x;
- x = y;
- y = temp;
- printf("the Number after swap:\n");
- printf("+x' = %d\n", x);
- printf("+y' = %d\n", y);
- }
- while (x != 0 && y != 0);
- }
- //EX3
- void charCheck () {
- char chars;
- int nVowels = 0;
- int nConsonants = 0;
- int nOthers = 0;
- printf("Enter the string: ");
- do {
- chars = getchar();
- chars = toupper(chars);
- if (chars >='A' && chars <= 'Z') {
- switch (chars) {
- case 'A':
- case 'E':
- case 'I':
- case 'O':
- case 'U':
- nVowels++;
- break;
- default:
- nConsonants++;
- }
- } else {
- if (chars != 10) {
- nOthers++;
- }
- }
- }
- while (chars != '\n');
- printf("Number of vowels: \t%d\n", nVowels);
- printf("Number of consonants: \t%d\n", nConsonants);
- printf("Number of others: \t%d\n", nOthers);
- }
- //EX4
- void asciiTable () {
- int code;
- for (code = 0; code <= 255; code++)
- printf("%c, %d, %o, %X\n", code, code, code, code);
- if (code !=0 && code %20==0) {
- getchar();
- }
- }
- //EX5
- void asciiDiff () {
- char firstChar;
- char secondChar;
- char temporaryChar;
- char charLoop;
- int difference;
- printf("firstChar: ");
- scanf("%c", &firstChar);
- getchar();
- printf("secondChar: ");
- scanf("%c", &secondChar);
- if (firstChar > secondChar) {
- temporaryChar = firstChar;
- firstChar = secondChar;
- secondChar = temporaryChar;
- }
- difference = secondChar - firstChar;
- printf("difference: %d\n", difference);
- for (charLoop = firstChar; charLoop <= secondChar; charLoop++) {
- printf("%c : %d, %o, %X\n", charLoop, charLoop, charLoop, charLoop);
- }
- }
- //EX6
- int primeChecker (int n) {
- //var
- int m;
- int i;
- //square root of n
- m = sqrt(n);
- if ( n < 2) {
- //Condition 1 is not satisfied
- return 0;
- }
- for (i = 2; i <= m; i++) {
- if (n % i == 0) {
- //n is divided by i
- //n is not a prime
- return 0;
- }
- }
- //n is a prime
- return 1;
- }
- //EX7
- int validDate (int day, int month, int year) {
- //max day of months 1, 3, 5, 7, 8, 10, 12
- int maxDays = 31;
- //Basic checking
- if (day < 1 || day > 31 || month < 1 || month > 12) {
- return 0;
- }
- //update maxd of a month
- if ( month == 4 || month == 6 || month == 9 || month == 11) {
- maxDays = 30;
- }
- else if (month == 2) {
- if (year % 400 == 0 || ( year % 4 == 0 && year % 100 != 0)) {
- maxDays = 29;
- }
- else {
- maxDays = 28;
- }
- }
- return day <= maxDays;
- }
- //EX8
- int getRelPos (double x, double y, double r) {
- //d2= x2+ y2
- double d2 = x*x + y*y;
- //r2
- double r2 = r*r;
- if (d2 < r2){
- return 1;
- }
- else if (d2 == r2) {
- return 0;
- }
- else {
- return -1;
- }
- }
- //EX9
- double factorial (int n) {
- double p;
- int i;
- p = 1;
- for (i = 2; i <= n; i++) {
- p *= i;
- }
- return p;
- }
- //EX10
- double fibo (int n) {
- int t1 = 1;
- int t2 = 1;
- int f = 1;
- int i ;
- for (i = 3; i <= n; i++) {
- f = t1 + t2;
- t1= t2;
- t2=f;
- }
- return f;
- }
- // EX11
- int accept () {
- int n;
- do {
- printf("Type n: ");
- scanf("%d", &n);
- }
- while (n < 1);
- return n;
- }
- int isFibonacci (int n) {
- int t1=1;
- int t2=1;
- int f=1;
- if (n==1) {
- return 1; /* n belongs to the Fibonacci sequence*/
- }
- while (f<n) { /* Find out the Fibo number f to n */
- f= t1 + t2;
- t1=t2;
- t2=f;
- }
- return n == f; /* if n==f ' n is Fibo element ' return 1 */
- }
- //EX12
- int sumDigits (int n) {
- int sum;
- sum = 0; /* initialize sum of digits */
- do {
- int remainder = n % 10 ; /* Get a digit at unit position */
- n = n/10;
- sum += remainder;
- }
- while (n>0);
- return sum;
- }
- //EX13
- double makeDouble (int ipart, int fraction) {
- double d_f;
- d_f = fraction;
- while (d_f >=1) {
- d_f = d_f/10; //create the fraction <1
- }
- if (ipart<0) {
- return ipart - d_f; //case-51 - 0.139
- }
- return ipart + d_f ; //case 32 + 0.25
- }
- //EX14
- int gcd(int a, int b) {
- while ( a != b )
- if (a > b) {
- a -=b;
- }
- else {
- b -= a;
- }
- return a;
- }
- int lcm (int a, int b) {
- return a*b/ gcd(a,b);
- }
- //EX15
- void printMinMaxDigits(int n) {
- int digit; /* Variable for extracting 1 digit */
- int min, max; /* Result variables */
- digit = n % 10; /* get the first rightmost digit: 3 */
- n = n / 10; /* 1029, the remainder needs to proceed after*/
- min = max = digit; /* initialize results */
- while (n>0) {
- digit = n % 10; /* Get the next digit */
- n=n/10;
- if (min > digit) {
- min = digit; /* update results */
- }
- if (max < digit) {
- max = digit;
- }
- }
- printf("\nMin = %d", min);
- printf("\nMax = %d", max);
- }
- //EX16
- void menuEX16 () {
- printf("----Select a function----");
- printf("\n1. Process Prime");
- printf("\n2. Print min, max digit in an integer");
- printf("\nother. Exit");
- }
- void operation1 () {
- int x;
- bool boolean = true;
- printf("insert x: ");
- scanf("%d", &x);
- for (int i = 2; i <= round (sqrt(x)); i++) {
- if (x % i ==0) {
- boolean = false;
- break;
- }
- }
- if (boolean) {
- printf ("X is a prime\n");
- } else {
- printf ("X is not a prime\n");
- }
- }
- void operation2 () {
- int x;
- int max = 0;
- int remainder;
- printf("insert x: ");
- scanf("%d", &x);
- do {
- remainder = x % 10;
- if (remainder > max) {
- max = remainder;
- }
- x /= 10;
- } while (x > 0);
- printf("%d is the biggest digit of x\n", max);
- }
- int main () {
- _flushall();
- int i;
- int n;
- int day = 0;
- int month = 0;
- int year = 0;
- double x;
- double y;
- double r;
- int result;
- double sumOfFactor;
- int num;
- double totalOfFibo;
- int sum;
- int a;
- int b;
- int d;
- int m;
- int ipart;
- int fraction;
- double realValue;
- int ex16chose;
- int intMenu;
- do {
- intMenu = menu();
- switch (intMenu) {
- case 1:
- system("cls");
- sumOfInt();
- system("pause");
- break;
- case 2:
- system("cls");
- intSwap();
- system("pause");
- break;
- case 3:
- system("cls");
- charCheck();
- system("pause");
- break;
- case 4:
- system("cls");
- asciiTable();
- system("pause");
- break;
- case 5:
- system("cls");
- asciiDiff();
- system("pause");
- break;
- case 6:
- system("cls");
- n = 0;
- do {
- printf("Enter max range: ");
- scanf("%d",&n);
- }
- while (n < 2);
- for(i = 2; i <= n; i++) {
- if (primeChecker(i) == 1) {
- printf("%d, ", i);
- }
- }
- printf("\n");
- system("pause");
- break;
- case 7:
- system("cls");
- printf("please input the full date (format dd/mm/yyyy): ");
- scanf("%d/%d/%d", &day, &month, &year);
- if(validDate(day,month,year)){
- printf("Valid\n");
- }
- else {
- printf("Invalid\n");
- }
- system("pause");
- break;
- case 8:
- system("cls");
- printf("x: ");
- scanf("%lf", &x);
- printf("y: ");
- scanf("%lf", &y);
- do {
- printf("r: ");
- scanf("%lf", &r);
- }
- while (r < 0);
- result = getRelPos(x,y,r);
- if (result == 1) {
- printf("The point is in the circle\n");
- }
- else if (result==0) {
- printf("The point is on the circle\n");
- }
- else {
- printf("The point is out of the circle\n");
- }
- system("pause");
- break;
- case 9:
- system("cls");
- do {
- printf("pls input your num to compute facotrial: ");
- scanf("%d", &num);
- }
- while (num < 0);
- sumOfFactor = factorial(num);
- printf("Total: %0.2lf\n", sumOfFactor);
- system("pause");
- break;
- case 10:
- system("cls");
- do {
- printf("type your number to compute n^th value by using Fibonacci sequence: ");
- scanf("%d", &n);
- }
- while (n < 1);
- totalOfFibo = fibo(n);
- printf("total = %0.2lf\n", totalOfFibo);
- system("pause");
- break;
- case 11:
- system("cls");
- n = accept();
- if(isFibonacci(n) == 1){
- printf("It is a Fibonacci element.\n");
- } else {
- printf("It is not a Fibonacci element.\n");
- }
- system("pause");
- break;
- case 12:
- system("cls");
- do {
- printf("Type n: ");
- scanf("%d", &n);
- if(n > 0) {
- sum = sumDigits(n);
- printf("SUM = %d\n", sum);
- }
- }
- while (n >= 0);
- system("pause");
- break;
- case 13:
- system("cls");
- printf("accept Integral part: ");
- scanf("%d", &ipart);
- do {
- printf("accept Fraction: ");
- scanf("%d", &fraction);
- }
- while (fraction < 0);
- realValue = makeDouble(ipart, fraction);
- printf("Real numer is: %0.4lf", realValue);
- printf("\n");
- system("pause");
- break;
- case 14:
- system("cls");
- do {
- printf("Enter a: ");
- scanf("%d",&a);
- printf("Enter b: ");
- scanf("%d",&b);
- }
- while (a <= 0 || b <= 0);
- d = gcd(a,b);
- m = lcm (a,b);
- printf("d = %d\n", d);
- printf("m = %d\n", m);
- system("pause");
- break;
- case 15:
- system("cls");
- do {
- printf("Enter n: ");
- scanf("%d",&n);
- printMinMaxDigits(n);
- }
- while (n < 0);
- system("pause");
- break;
- case 16:
- system("cls");
- menuEX16();
- scanf("%d", &ex16chose);
- switch (ex16chose) {
- case 1:
- operation1();
- break;
- case 2:
- operation2();
- break;
- default:
- printf("exiting");
- break;
- }
- system("pause");
- break;
- default:
- printf("Good Bye\n");
- system("pause");
- _flushall();
- }
- system("cls");
- } while (intMenu > 0 && intMenu < 17);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement