Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

6 While - do while حل-2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Fundamentals of Computer Programming UNIVERSITY OF SCIENCE & TECHNOLOGY

Faculty of Engineering
2nd Level Dep. of Electronics
Engineering
Programming Sheet # 6 solution
(While – do-while)
1) Write a program which prints all numbers that multiple of three from 1 to 60 by using while statement.
#include<stdio.h>
main()
{
int i=1;
while(i<=60)
{
if(i%3==0)
printf("%d\t",i);
i++;
}
}
2) Write a program which takes two integer numbers from the user then prints the summation of all odd
numbers between them by using while statement.
#include<stdio.h>
main()
{
int x,y,sum=0,i;
printf("please enter first integer number :");
scanf("%d",&x);
printf("please enter second integer number :");
scanf("%d",&y);
i=x;
while(i<=y)
{
if(i%2!=0)
sum+=i;
i++;
}
printf("Summation of odd numbers between %d and %d = %d",x,y,sum);
}
3) write a program that reads an integer number then displays digits separated by tabs by using while
statements. Example (if you enter 2000, the output must be 2 0 0 0).
#include<stdio.h>
main()
{
int num,i=1,t,m=10000;
printf("please enter an integer number: ");
scanf("%d",&num);
while(i<=5)
{ t=num/m;
printf("%d\t",t);
num=num%m;
m=m/10;
i++; }
}
Sheet #6 - While – do-while Eng. Ali Al-Ashwal 1
4) Write a program takes an integer number from the user then determains the result of the following
sequence:
Result = 1 - 5 + 9 - 13 + 17 ……. N ,You must enter the value of N and use while statement.

#include<stdio.h>
main()
{
int N,i=1,s=1,sum=0;
printf("please enter the value of N :");
scanf("%d",&N);
while(i<=N)
{
sum=sum+s*i;
i=i+4;
s=s*-1;
}
printf("The result= %d",sum);
}
5) Write a program to produce a table of the numbers from 1 to 20 in increments of 2, with their squares and
cubes. The output should be:
NUMBER SQUARE CUBE
------ ------ ------
2.00 4.00 8.00
4.00 16.00 64.00
6.00 36.00 216.00
8.00 64.00 512.00
10.00 100.00 1000.00
12.00 144.00 1728.00
14.00 196.00 2744.00
16.00 256.00 4096.00
18.00 324.00 5832.00
20.00 400.00 8000.00

#include<stdio.h>
#include<math.h>
main()
{
float x=2;
printf("NUMBER\tSQUARE\tCUBE\n");
printf("------\t------\t------\n");
while (x <= 20)
{
printf("%.2f\t%.2f\t%.2f\n",x,pow(x,2),pow(x,3));
x=x+2;
}
}

Sheet #6 - While – do-while Eng. Ali Al-Ashwal 2


6) Write a program which reads two float numbers from the user then shows a menu contain the following
options:
1) press (1) to display the two numbers.
2) press (2) to sum the two numbers.
3) press (3) to subtract the two numbers.
4) press (4) to multiply the two numbers.
5) press (5) to interchange between the two numbers.
6) press (6) to Exit.
you must use do-while statement.

#include<stdio.h>
main()
{
float x,y,t;
int c;
printf("Please enter two float numbers\n");
scanf("%f%f",&x,&y);
do
{
printf("\n\t***************************************\n");
printf("\t* press 1 to display the numbers *\n");
printf("\t* press 2 to sum the two numbers *\n");
printf("\t* press 3 to subtract the two numbers *\n");
printf("\t* press 4 to multiply the two numbers *\n");
printf("\t* press 5 to interchange the numbers *\n");
printf("\t* press 6 to Exit *");
printf("\n\t***************************************\n");
scanf("%d",&c);
switch(c)
{
case 1: printf(" x=%.2f\ty=%.2f",x,y); break;
case 2: printf(" the summation of %.2f and %.2f = %.2f",x,y,x+y); break;
case 3: printf(" the subtraction of %.2f and %.2f = %.2f",x,y,x-y); break;
case 4: printf(" the multiplication of %.2f and %.2f = %.2f",x,y,x*y); break;
case 5: t=x;
x=y;
y=t; break;
case 6: printf("\t\t\t\tExit"); break;
default:printf("please enter number between 1 and 6");
}
}while(c!=6);
}

Sheet #6 - While – do-while Eng. Ali Al-Ashwal 3

You might also like