Using C Programming: Write a program that will take 5 integers from the user, st
ID: 3594525 • Letter: U
Question
Using C Programming:Write a program that will take 5 integers from the user, store them in an array and display them on the screen in reverse order. For instance if the input is: 12 23 1 45 -2 then the output will be: -2 45 1 23 12. Q.2 Write a program that will create an integer array of size 25. Compute and store 25 Fibonacci numbers in it and then display on the screen. Q.3 Write a program that will ask the user to enter an integer number. The computer must separate the digits and display them in same order with a space between each digit. For example, if the user enters 237, the computers output should be: 2 3 7. Hint: You may use an array now to solve Q.3. Using C Programming:
Write a program that will take 5 integers from the user, store them in an array and display them on the screen in reverse order. For instance if the input is: 12 23 1 45 -2 then the output will be: -2 45 1 23 12. Q.2 Write a program that will create an integer array of size 25. Compute and store 25 Fibonacci numbers in it and then display on the screen. Q.3 Write a program that will ask the user to enter an integer number. The computer must separate the digits and display them in same order with a space between each digit. For example, if the user enters 237, the computers output should be: 2 3 7. Hint: You may use an array now to solve Q.3. Using C Programming:
Explanation / Answer
A1.
#include <stdio.h>
#include <string.h>
int main()
{
int i;
int array[5];
//Take 5 integers from user
for(i=0;i<5;i++){
char temp[256];
printf("Please enter element %d : ",i+1);
scanf("%s", temp);
array[i]=atoi(temp);
}
printf("++++++++++++++++++ Output: ++++++++++++++++++ ");
//Print in reverse order
for(i=4;i>=0;i--){
printf("%d ",array[i]);
}
}
++++++++++++++++++++
Sample Output :
Please enter element 1 : 12
Please enter element 2 : 23
Please enter element 3 : 1
Please enter element 4 : 45
Please enter element 5 : -2
++++++++++++++++++
Output:
++++++++++++++++++
-2 45 1 23 12
=======================================================================
A.2 :
#include <stdio.h>
#include <string.h>
int main()
{
int i, t1=0, t2=1, nextTerm;
int array[25];
//Calculate Fibonacci Series and store into array
for (i = 1; i <= 25; ++i)
{
array[i-1] = t1;
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
//displaying output on console
for(i=0;i<25;i++){
printf("%d ",array[i]);
}
}
+++++++++++++++++++++++++
Sample Output :
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
===============================================
A.3
#include <stdio.h>
#include <string.h>
#include <math.h>
int main()
{
int input,input_temp; char temp[100];
printf("Please enter integer : ");
scanf("%s", temp);
input=input_temp=atoi(temp);
//Calculate length of input
int length=0;
while(input_temp != 0)
{
length++;
input_temp /= 10;
}
//Print digit one by one
while(input>0){
int factor = ceil(pow(10,length-1));
int digit = input / factor;
input = input%factor;
printf("%d ",digit);
length--;
}
}
+++++++++++++++++++++++
Sample Output :
Please enter integer : 4859562
4 8 5 9 5 6 2
Hope it helps.
Happy Learning.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.