Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Question1:
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- //readln func that deals with the precistion
- int readln(char s[], int maxlen) {
- char ch;
- int i;
- int chars_remain;
- i = 0;
- chars_remain = 1;
- int x=0;
- int z=0;
- while (chars_remain) {
- ch = getchar();
- if (isdigit(ch) || ch == '.' || ch == '\n') {
- if ((ch == '\n') || (ch == EOF)) {
- chars_remain = 0;
- }
- if (ch == '.') {
- if (x <= 3) {
- s[i] = ch;
- i++;
- continue;
- }
- else {
- printf("Output: error; numbers before [.] exceeds 3 digits.");
- z = 1;
- break;
- }
- }
- else if (i < maxlen - 1) {
- s[i] = ch;
- i++;
- if (ch != '.')
- x++;
- }
- }
- else {
- printf("Only digits and positive numbers are allowed");
- break;
- }
- }
- if (!z) {
- s[i] = '\0';
- return i;
- }
- else {
- i = -1;
- return i;
- }
- }
- int main(int argc, char** argv) {
- char [12] number;
- double In;
- int result;
- int integer;
- printf("Input/\n");
- readln(number,12);
- // since the value is in string i've to convert it into decimal using strod fun
- if (result >0) {
- In = strtod(number,NULL);
- In= In * (22 / 7);
- printf("Output/ %.8f \n", In);
- //checking whether it is even or odd
- integer = In;
- if (integer % 2 == 0) {
- printf("%d is even \n", integer);
- --integer;
- if (integer - 3 != 0) {
- int mod = 10 % (integer - 3);
- printf("result:%d \n", mod);
- }
- else
- printf("Error Can't Devide by Zero!");
- }
- else
- printf("%d odd \n", integer);
- }
- return 0;
- }
- Question2:
- #include <stdio.h>
- #include <stdlib.h>
- int main(int argc, char** argv) {
- char name[6];
- char letter;
- char age[10];
- int num;
- char sec[10];
- int Snum;
- printf("Input/\n");
- printf("Name:\n");
- gets(name);
- printf("Favorite letter:\n");
- letter = getchar();
- printf("Age:\n");
- fgets(age, 10, stdin);
- num = atoi(age);
- printf("Secret number:\n");
- fgets(sec,10, stdin);
- Snum = atoi(sec);
- printf("Output/\n");
- if ((letter == 'A') && (num == 24 || num == 25 || num == 26 || num == 27) && (Snum == 123456))
- printf("Access granted:");
- printf("Name %s, Favorite letter: %c, Age: %d, Secret Number %d", name, letter, num, Snum);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement