Workshop 5: Exercise 1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Workshop 5

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>

void numswap(int* a, int* b, int* c){


    int t;
    t=*a;
    *a=*c;
    *c=*b;
    *b=t;
   
   
}

int main(int argc, char *argv[]) {


   
    int d1,d2,d3;
    printf("Enter the value of the first element: ");
    scanf("%d",&d1);
    printf("Enter the value of the second element: ");
    scanf("%d",&d2);
    printf("Enter the value of the third element: ");
    scanf("%d",&d3);
    printf("The value before swapping are :\nele 1: %d\nele 2: %d\nele 3: %d\n ",d1,d2,d3);
    numswap(&d1,&d2,&d3);
    printf("The value after swapping are :\nele 1: %d\nele 2: %d\nele 3: %d\n ",d1,d2,d3);
    return 0;
}

                        
          

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>

int main(int argc, char *argv[]) {


    int choice, a, b, n;
    char c1, c2;
    do{
   
    printMenu();
    printf("\n\nYour choice: ");
    scanf("%d",&choice);
    switch(choice){
        case 1:
            printf("enter 1st num: ");scanf("%d",&a);
            printf("enter 2nd num: ");scanf("%d",&b);
            printf("Sum of integers: %d\n",sumInteg(a, b));
            break;
        case 2:
            fflush(stdin);
            printf("enter 1st character: ");scanf("%c",&c1);
            fflush(stdin);
            printf("enter 2nd character: ");scanf("%c",&c2);
            printASCII(c1,c2);
            break;
        case 3:
            printf("enter a positive integer: ");scanf("%d",&n);
            checkfibo(n);
           
            break;
           
    } 
}while(choice>0 && choice<4);
    printf("Goodbye!");
    getch();
    return 0;   
}
   
   
void printMenu(){

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;}

void printASCII(char c1,char c2){


//ASCII table   
   
    int i;
    printf("\nASCII table according to input characters:");
    printf("\n\nDec | Char\n");
    if(c2>c1){
   
    for(i=0;i<=c2-c1;i++){ 
    printf("\n%d  |  %c", i+c1, i+c1);
}
}
    else{
     for(i=0;i<=c1-c2;i++){ 
    printf("\n%d  |  %c", i+c2, i+c2);
    }
}
  
}

int checkfibo(int n){


//fibo checker
    int a,b,c,next;
 if((n==0)||(n==1))
   printf("\n%d is a Fibonacci term\n",n);
 else
 {
   a=0;
   b=1;
   c=a+b;
   while(c<n)
   {
     a=b;
     b=c;
     c=a+b;
   }
   if(c==n){
   
     printf("\n%d is a Fibonacci term\n",n);}
   else{
   
     printf("\n%d is not a Fibonacci term\n",n);
    }
 return 0;
}
}

You might also like