Use while loop or do-while loop to solve the following problems using C Programm
ID: 3589340 • Letter: U
Question
Use while loop or do-while loop to solve the following problems using C Programming.
Q.1 Write a program which will ask the user to enter a number, say M which is larger than 4 and smaller than 20. Then, the computer must list all the numbers from 1 to M in an increasing order followed by all the numbers from M to 1 in a decreasing order. That is if the user enters 5 then the list on the screen must look like: 1 2 3 4 5 4 3 2 1. Can you print the list using only ONE loop (bonus 10 points for the last part)?
Q.2 Write a program that will display on the console a list of N numbers where each number in the list starting from the third one is sum of the previous two. First two numbers in this list are 1 and 1. The value of N should be taken from the user at the beginning of the program. That is, for N=7, your computer should display the following list: 1 1 2 3 5 8 13.
Q.3 Write a program that will print every 4th letter in the English alphabet, starting from ‘A’ and when it reaches the end it keeps printing in a circular manner until every letter is listed at least once, i.e, the list will be like:
A D G J……V Y B E H……….W Z C F…..
Explanation / Answer
Solution 1:
#include<stdio.h>
int main(){
int M,i=1,flag = 0;
printf("Enter any between 5 to 19 : ");
scanf("%d",&M);
while(i!=0){
printf("%d ",i);
/*
till i reaches M print i by incrementing.
when i reaches M print i by decrementing.
we use flag variable because when decrement started it should be decremented
if we don't use flag variable this loop will go to infinite
*/
if(i < M && flag == 0){
i++;
}
else{
flag = 1;
i--;
}
}
return 0;
}
Sample Output for solution 1:
Enter any between 5 to 19 : 5
1 2 3 4 5 4 3 2 1
Solution 2:
#include<stdio.h>
int main(){
int N,i = 1,j = 0,count = 1;
printf("Enter any positive number : ");
scanf("%d",&N);
while(count <= N){
/*
here i is the first element and j is second element
for every iteration we swap variables i and j.
And i is incremented by j;
Count variable is to check how many number are printed till now
*/
printf("%d ",i);
int temp = i;
i = i + j;
j = temp;
count++;
}
return 0;
}
Sample output for solution 2:
Enter any positive number : 7
1 1 2 3 5 8 13
Solution 3:
#include<stdio.h>
int main(){
int charAscii = 1;
while(charAscii != 24){
/*
A ascii value is 65. So we are adding 64 to 'charAscii' and printing the character.
after that we need every fourth element so add 3 to 'charAscii'
when ever 'charAscii' is greater than 26, then calculate remainder and store it to 'charAscii'.
this process will continued till 'charAscii' reaches '24'
If we write that sequence : 1 4 7 10 13 16 19 22 25 2 5 8 11 14 17 20 23 26 3 6 9 12 15 18 21 24
This is toatl sequence that every character is present atleast one time.
*/
printf("%c ",charAscii + 64);
charAscii = (charAscii + 3);
if(charAscii > 26){
charAscii %= 26;
}
}
return 0;
}
Sample output for solution 3:
A D G J M P S V Y B E H K N Q T W Z C F I L O R U
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.