12.6 Lab8
introduction-to-computer-science-laboratory-8.pdf
Introduction to Computer Science - Laboratory 8
-
Write a C program that:
- Takes a word as input from standard input.
- Determines if it is a palindrome. (A palindrome is a word that reads the same backward and forward, e.g., "racecar").
#include<stdio.h> int main() { int word[32]; int ch; int letternumber = 0; printf("Please type a word in lowercase: "); for(int i = 0; ch != EOF; i++) { ch = getchar(); if(ch >= 'a' && ch <= 'z') { letternumber++; word[i] = ch; } } int is_palindrome = 1; for(int j = 0; j < letternumber; j++) { if(word[j] != word[letternumber - j - 1]) is_palindrome = 0; } if(is_palindrome == 1) { printf("Yes, "); for(int k = 0; k < letternumber; k++) { printf("%c", word[k]); } printf(" is a palindrome!!!\n"); } else { printf("No, "); for(int l = 0; l < letternumber; l++) { printf("%c", word[l]); } printf(" is not a palindrome!!!\n"); } return 0; }
-
Write a C program that:
- Receives as input a number
N
and a sequence ofM
elements (whereM >= N
). - Calculates the Nth smallest number in the sequence.
#include<stdio.h> int main() { int n, m; int lowlimit = 0; int lowposition = 0; printf("Please enter a number n and a number m:"); scanf("%d %d", &n, &m); int sequence[m]; // store the sequence printf("Please enter a sequence:"); for (int i = 0; i < m; i++) { scanf("%d", &sequence[i]); } //find Nth smallest number while(lowlimit < n) { lowposition = lowlimit; for(int j = lowlimit; j < m; j++) { if(sequence[j] < sequence[lowposition]) { lowposition = j; } } //swap the smaller element to the front int temp; temp = sequence[lowlimit]; sequence[lowlimit] = sequence[lowposition]; sequence[lowposition] = temp; lowlimit++; } printf("%d", sequence[lowlimit - 1]); return 0; }
- Receives as input a number
-
Write a program that:
- Takes a sequence of integer numbers and an integer number
N
as input. - If
N
is in the sequence, printsN
’s position; otherwise, prints-1
.
#include<stdio.h> int main() { int N, number_sequence; printf("Please enter a number N and the number of sequence: "); scanf("%d %d", &N, &number_sequence); int sequence[number_sequence]; // store the sequence printf("Please enter a sequence:"); for (int i = 0; i < number_sequence; i++) { scanf("%d", &sequence[i]); } //record the flag and position if the number is in the sequence int is_same = 0; int is_same_position; //compare the value for(int j = 0; j < number_sequence; j++) { if(sequence[j] == N) { is_same = 1; is_same_position = j + 1; } } //output if(is_same == 1) printf("Yes, %d is in the sequence and it's position is %d", N, is_same_position); else printf("-1"); }
- Takes a sequence of integer numbers and an integer number
-
Write a C program that:
- Takes a sequence of integer numbers as input.
- Classifies the sequence into even and odd numbers.
- Prints the even numbers first, followed by the odd numbers.
#include<stdio.h> int main() { int number_sequence; printf("Please enter the number of sequence: "); scanf("%d", &number_sequence); //define two arraies to store the number int evenseq[number_sequence]; int oddseq[number_sequence]; int inputnumber; //store the sequence printf("Please enter a sequence:"); int i = 0; int j = 0; // seperate the even numbers and odd numbers into two array while((i + j) < number_sequence) { scanf("%d", &inputnumber); if(inputnumber % 2 == 0) { evenseq[i] = inputnumber; i++; } else { oddseq[j] = inputnumber; j++; } } for(int k = 0; k < i; k++) printf("%d", evenseq[k]); //warning!!! usual mistake!!! donot write evenseq[i] for(int k = 0; k < j; k++) printf("%d", oddseq[k]); return 0; }
-
Design and implement a C program that:
- Takes an integer number
K
and a sequence of integer numbers as input. - Counts all the numbers in the sequence greater than
K
.
#include<stdio.h> int main() { int K, number_sequence; printf("Please enter a number K and the number of sequence: "); scanf("%d %d", &K, &number_sequence); int sequence[number_sequence]; // store the sequence printf("Please enter a sequence:"); for (int i = 0; i < number_sequence; i++) { scanf("%d", &sequence[i]); } int times = 0; for(int i = 0; i < number_sequence; i++) { if(sequence[i] > K) times++; } printf("The number of number which is greater than K is %d", times); return 0; }
- Takes an integer number