Workshop 5: Exercise 1
Workshop 5: Exercise 1
Workshop 5: Exercise 1
Exercise 1:
Write a program in C to swap elements by calling the pointer parameter.
Input:
Enter the value of the first element: 8
Enter the value of the 2nd element: 9
Enter the value of the 3rd element: 10
Output:
The value before swapping are :
element 1 = 8
element 2 = 9
element 3 = 10
The value after swapping are :
element 1 = 10
element 2 = 8
element 3 = 9
Answer:
#include <stdio.h>
#include <stdlib.h>
Exercise 2:
Write a C program using the following menu:
1- Sum of integers.
2- Print out the ASCII table.
3- Check fibonaci number.
Others- Quit
If user chooses 1, user will input 2 integers, the program will print out sum of integers between them
including them. Show menu again.
If user chooses 2, user will input 2 characters, the program will print out the ASCII table between
two inputted characters in ascending order. Show menu again
If user chooses 3, user will input 1 positive integer, the program will check this number is a
fibonacci number or not and print the result. Show menu again.
If user chooses other options, the program will terminate.
***
(Students should attend class fully, these 2 exercises are guided in class.)
Answer:
#include <stdio.h>
#include <stdlib.h>
printf("\n 1- Sum of integers\n 2-Print out the ASCII table\n 3-Check fibonaci number \n 4-Others- Quit\
n");
}
int sumInteg(int a, int b){
//!-Sum Integers
int i,sum=0;
if(b>a){
printf("Table of digits from 1st num to 2nd num: \n");
for(i = 0; i <= b-a; i++)
{
printf("%d\n", i+a);
sum = sum + i+a;
}
return sum;}
printf("Table of digits from 2nd num to 1st num: \n");
for(i = 0; i <= a-b; i++)
{
printf("%d\n", i+b);
sum = sum + i+b;
}
return sum;}