P1 Test Preparation PDF
Document Details
Tags
Summary
This document provides test preparation material for P1, focusing on programming examples involving loops, calculations (sum, product) and other related topics. It includes exercises on various programming constructs such as while loops and for loops. The examples demonstrate common algorithms in programming.
Full Transcript
Test preparation for P1 While: Repeats instructions (body) while condition is True Stops when condition becomes False Else statement (optional) executed after loop ends Ex: 1. Nr of digits of a nr: a = 123 c=1 while a > 9: a = a // 10 c=c+1 else: print("Finishe...
Test preparation for P1 While: Repeats instructions (body) while condition is True Stops when condition becomes False Else statement (optional) executed after loop ends Ex: 1. Nr of digits of a nr: a = 123 c=1 while a > 9: a = a // 10 c=c+1 else: print("Finished: ", c, "digits") 2. Write a program that reads an integer number from standard input and computes the sum of its digits. a=int(input()) sum = 0 while a > 0: sum = sum + a % 10 a = a // 10 else: print(sum) 3. Write a program that reads an integer number from standard input and computes the product of its digits. a=257 p=1 while a>0: p=p*(a%10) a=a//10 else: print(p) 4. Write a program that reads a number, N, from standard input and computes the sum of the first N natural numbers (N included). N=int(input()) sum=0 i=1 while i