//1) Repeat the problem no. 11 in Sheet#0 but with an ‘Exit’ option; that is, the program allows the
//user to enter new values and choose the required operations until the user chooses to exit.
#include<stdio.h>
operation();
int main()
{ int x,y,op;
printf("available operations:\n1 to addition\n2 to subtraction\n3 to multiplication\n4 to division\n5 to exit: \n");
while(op!=5)
{
printf("choose an operation: ");
scanf("%d",&op);
if(op==5) continue;
printf("enter integer number of x: ");
scanf("%d",&x);
printf("enter integer number of y: ");
scanf("%d",&y);
if(op==4)
{
if(y==0)
{ printf("the program not allow the division by zero");
return ;
}
}
else
{
switch(op)
{
case 1:
printf("the addition is %d\n",x+y);
break;
case 2:
printf("the subtraction is %d\n",x-y);
break;
case 3:
printf("the multiplication is %d\n",x*y);
break;
case 4:
printf("the division is %.2f\n",x/y);
break;
case 5:
break;
default:
printf("choose from 1 to 5");
}
}
}
}
//--------------------------------------------------------------------------------------------------
//3) Write a program that will:
//● Prompt the user to input ten integer values.
//● Calculate the smallest and the greatest of those values.
//● Call a function to calculate the difference between those smallest and greatest values.
//● Finally: displays the entered ten integers, the difference between the smallest and greatest
//values, and the value that occurs the most
#include<stdio.h>
difference(int,int);
int main()
{ int num,num1,num2,num3,num4,num5,num6,num7,num8,num9,num10,max,min;
printf("input ten integer values:\n");
for(int counter=1 ; counter<=10 ; counter++)
{ printf("number-%d: ",counter);
scanf("%d",&num);
if(counter==1)
{ min=num;
max=num;
num1=num;
}
else
{ if(counter==2)
num2=num;
if(counter==3)
num3=num;
if(counter==4)
num4=num;
if(counter==5)
num5=num;
if(counter==6)
num6=num;
if(counter==7)
num7=num;
if(counter==8)
num8=num;
if(counter==9)
num9=num;
if(counter==10)
num10=num;
if(num>max)
max=num;
else
{ if(num<min)
min=num;
}
}
} printf("\n");
printf("the ten values are:\n");
printf("the number-1: %d\nthe number-2: %d\nthe number-3: %d\nthe number-4: %d\nthe number-5: %d\nthe number-6: %d\nthe
number-7: %d\nthe number-8: %d\nthe number-9: %d\nthe number-10: %d\n",num1,num2,num3,num4,num5,num6,num7,num8,num9,num10);
printf("the greatest is %d and smallest is %d and the difference between them is %d",max,min,difference(max,min));
}
int difference(int max, int min)
{
return max-min;
}
//--------------------------------------------------------------------------------------------------
//4) Write a C program to find the sum of the first n natural numbers using recursion.
//Note: Positive integers are known as natural numbers
//solution 1
#include<stdio.h>
int sum(int n)
{
if(n==0)
return;
else
{
return n+sum(n-1);
}
}
int main()
{
int n;
printf("enter the number of n:");
scanf("%d",&n);
printf("the sum of the first n natural numbers using recursion is %d",sum(n));
}
//solution 2
int sum(int n,int summ)
{
if(n==0)
return summ;
else
{
summ+=n;
return sum(n-1,summ);
}
}
int main()
{
int n;
printf("enter the number of n:");
scanf("%d",&n);
printf("the sum of the first n natural numbers using recursion is %d",sum(n,0));
}
//--------------------------------------------------------------------------------------------------
//5) Write a C program to check whether a number is a prime number or not, by using recursion
#include<stdio.h>
int CheckPrime(int i,int num)
{
if(num==i)
return 0;
else
if(num%i==0)
return 1;
else{
return CheckPrime(i+1,num);
}
}
int main()
{
int num;
printf("Enter your Number:");
scanf("%d",&num);
if(CheckPrime(2,num)==0)
printf("It is a Prime Number.");
else
printf("It is not a Prime Number.");
}
//--------------------------------------------------------------------------------------------------
//6) Write a C program to reverse a string, by using recursion.
#include<stdio.h>
void reverseString();
int main()
{
printf("Enter a sentence: ");
reversestring();
return 0;
}
void reverseSting()
{
char c;
scanf("%c", &c);
if (c != '\n')
{
reversestring();
printf("%c", c);
}
}