While loop Write a program that asks the user to enter a number, then displays whether this number is even or odd. The program repeats until the user quits.

Slides:



Advertisements
Similar presentations
For loops For loops are controlled by a counter variable. for( c =init_value;c
Advertisements

Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection.
1 ICS103 Programming in C Lecture 7: Repetition Structures.
Arrays Write a program that first reads in 20 integers and then asks the user whether they want to display all the even integers or all the odd integers.
ICS103 Programming in C Lecture 11: Recursive Functions
Guidelines for working with Microsoft Visual Studio.Net.
LOOP (Part 2) for while do-while 1. TK1913-C Programming2 TK1913-C Programming 2 Loop : for Loop : for Condition is tested first Loop is controlled by.
Guidelines for working with Microsoft Visual Studio 6.
לולאות 02 יולי יולי יולי 1502 יולי יולי יולי 1502 יולי יולי יולי 15 1 Department of Computer Science-BGU.
1 Agenda - Loops while for for & while Nested Loops do-while Misc. & Questions.
Programming Arrays. Question Write a program that reads 3 numbers from the user and print them in ascending order. How many variables do we need to store.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
Programming Arrays. Example 1 Write a program that reads 3 numbers from the user and print them in reverse order. How many variables do we need to store.
Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=
Do-while loop Syntax do statement while (loop repetition condition)
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
WEEK 4 Class Activities Lecturer’s slides.
Chapter 6: Control Structures Computer Programming Skills Second Term Department of Computer Science Foundation Year Program Umm Alqura.
Introduction to C Programming CE Lecture 4 Further Control Structures in C.
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
CSCI 171 Presentation 5. The while loop Executes a block as long as the condition is true general form: while (condition) { statement 1; statement 2;
1 Decision Making if (score
Problems Reads a letter grade from user and displays the corresponding range for that letter grade (A  , B  80-89, C  70-79, D  60-69, otherwise.
C Programming - Structures. Structures containing arrays A structure member that is an array does not ‘behave’ like an ordinary array When copying a structure.
/* example program to demonstrate the passing of an array */ #include int maximum( int [] ); /* ANSI function prototype */ int maximum(
Pointers Value, Address, and Pointer. Values and Addresses int x, y, z; y x z values of x,
Principle Prog Revision. Question 1 (a) Identify errors in the following program segment and how the errors can be corrected. void main(){ constant int.
DATA TYPE, MEMORY, AND FUNCTION Dong-Chul Kim BioMeCIS UTA 2/18/
Divisibility Find out if a number, Numb, is divisible by another number, Div. Is 432 divisible by 3? Is 432 divisible by 4? 432 / 3 = ? 432 / 4 = ? 432.
Functions Dr. Sajib Datta Functions A function is a self-contained unit of program code designed to accomplish a particular task. Some functions.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Problem Solving and Program Design in C Chap. 5 Repetition and Loop Statement Chow-Sing Lin.
1 Agenda Arrays: Definition Memory Examples Passing arrays to functions Multi dimensional arrays.
Arrays Name, Index, Address. Arrays – Declaration and Initialization int x; y[0] y[1] y[2]
 Real numbers representation - Floating Point Notation  First C Program  Variables Declaration  Data Types in C ◦ char, short, int, long, float, double,
CHAPTER 4 REPETITION STRUCTURES 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 A.AlOsaimi.
Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2.
The Repetition control structure using while loop.
Dr. Sajib Datta  char type technically is an integer type  Computer uses numeric codes to represent characters, and store characters as integers.
Dr. Sajib Datta Sep 8,  char type technically is an integer type  Computer uses numeric codes to represent characters, and store characters.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
CSE1320 Loop Dr. Sajib Datta
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Week 4 – Chapter 3 Repetition.
CS1010 Programming Methodology
Computer Programming Methodology Input and While Loop
Functions Dr. Sajib Datta
מבוא כללי למדעי המחשב תרגול 2
Control Structures Lecture 7.
Looping.
Vòng lặp Chương 6.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Lec 7.
Programming application CC213
Chapter 8 The Loops By: Mr. Baha Hanene.
Dr. Joe Anderson September 6, 2017
A function with one argument
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Incremental operators
Week 6 CPS125.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Philip Bernhard, PhD Spring 2018
Arrays.
Types of loops definite loop: A loop that executes a known number of times. Examples: Repeat these statements 10 times. Repeat these statements k times.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
CSCE 206 Lab Structured Programming in C
Presentation transcript:

while loop Write a program that asks the user to enter a number, then displays whether this number is even or odd. The program repeats until the user quits. Write a program that asks the user to enter a number, then counts by 2 starting from 0 to the given number. Write a program which finds the factorial of a given number SECENG 1111

Even/Odd #include int main(void) { charansw = 'y'; intnum; while(answ == 'y') { printf("Please enter an integer: "); scanf("%d", &num); if(num%2 == 0)printf("%d is even.\n", num); elseprintf("%d is odd.\n", num); printf("Do you want to give another number? (y/n): "); scanf("%c%c", &answ, &answ); } return(0); }

Count by 2 #include int main(void) { int num, stp = 0; printf("Please enter a number: "); scanf("%d", &num); while(stp <= num) { printf("%d ", stp); stp = stp + 2; } printf("\n"); return(0); }

Factorial #include int main(void) { int num, Fact = 1; printf("Please enter a number: "); scanf("%d", &num); while(num > 1) { Fact = Fact * num; num = num -1; } printf("The factorial is %d\n", Fact); return(0); }

Min/Max #include int main(void) { int Min, Max, Nmbr; int Cnt=1; printf("Please enter number %d: ", Cnt); scanf("%d", &Min); Max = Min; Cnt = Cnt + 1; while(Cnt <= 10) { printf("Please enter number %d: ", Cnt); scanf("%d", &Nmbr); if(Nmbr < Min) Min = Nmbr; if(Nmbr > Max) Max = Nmbr; Cnt = Cnt + 1; } printf("The minimum is %d\n", Min); printf("The maximum is %d\n", Max); return(0); }