Lab-03 Instructions for Understanding scanf() function and for loop in C++, Exercises of Computer Fundamentals

Instructions for a lab exercise in computer fundamentals using turbo c++. The objectives of the lab are to understand the scanf() function and practice reading values from the keyboard, as well as to understand the working of for loops. Several code examples and tasks to try, such as printing numbers from 1 to 10, printing the table of 2, printing even series, controlling for loops with user input, and printing the sum of first 10 integers. Students are encouraged to understand the working of scanf() function and predict the output for each code.

Typology: Exercises

2011/2012

Uploaded on 07/03/2012

prakash
prakash ๐Ÿ‡ฎ๐Ÿ‡ณ

4.6

(10)

63 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Com
p
uter Fundamental
Lab-03
Instructions
1. Use๎˜ƒTurbo๎˜ƒC\C++๎˜ƒfor๎˜ƒthe๎˜ƒfollowing๎˜ƒprograms๎˜ƒ
2. Create๎˜ƒa๎˜ƒfolder๎˜ƒCF_Labs๎˜ƒ
3. Create๎˜ƒnew๎˜ƒfolder๎˜ƒfor๎˜ƒeach๎˜ƒlab๎˜ƒe.g.๎˜ƒcreate๎˜ƒfolder๎˜ƒโ€œlabโ€02โ€๎˜ƒand๎˜ƒsave๎˜ƒall๎˜ƒthe๎˜ƒfiles๎˜ƒin๎˜ƒthat๎˜ƒfolder๎˜ƒfor๎˜ƒthis๎˜ƒlab๎˜ƒ
4. Create๎˜ƒnew๎˜ƒfile๎˜ƒfor๎˜ƒeach๎˜ƒof๎˜ƒthe๎˜ƒfollowing๎˜ƒprogram๎˜ƒ
5. For๎˜ƒany๎˜ƒhelp๎˜ƒcontact๎˜ƒlab๎˜ƒstaff๎˜ƒor๎˜ƒfaculty๎˜ƒmember๎˜ƒavailable๎˜ƒin๎˜ƒthe๎˜ƒlab๎˜ƒ
6. While๎˜ƒleaving๎˜ƒthe๎˜ƒlab๎˜ƒclose๎˜ƒall๎˜ƒprograms๎˜ƒand๎˜ƒshutdown๎˜ƒthe๎˜ƒcomputers,๎˜ƒtake๎˜ƒa๎˜ƒback๎˜ƒup๎˜ƒof๎˜ƒyour๎˜ƒcode๎˜ƒ
7. Do๎˜ƒnot๎˜ƒdiscard๎˜ƒthis๎˜ƒhandout๎˜ƒ
๎˜ƒ
Objective๎˜ƒ
The๎˜ƒobjectives๎˜ƒof๎˜ƒthis๎˜ƒlab๎˜ƒare๎˜ƒto๎˜ƒ
โ€ข Understand๎˜ƒthe๎˜ƒscanf()๎˜ƒfunction๎˜ƒand๎˜ƒpractice๎˜ƒreading๎˜ƒone๎˜ƒor๎˜ƒmultiple๎˜ƒvalues๎˜ƒfrom๎˜ƒkeyboard๎˜ƒfor๎˜ƒyour๎˜ƒprogram๎˜ƒ
โ€ข Understand๎˜ƒthe๎˜ƒformat๎˜ƒand๎˜ƒworking๎˜ƒof๎˜ƒfor๎˜ƒloop,๎˜ƒby๎˜ƒdoing๎˜ƒsimple๎˜ƒtasks๎˜ƒthat๎˜ƒwe๎˜ƒdid๎˜ƒin๎˜ƒthe๎˜ƒclass.๎˜ƒ
๎˜ƒ๎˜ƒ๎˜ƒ
๎˜ƒ
For each of the following code try to understand the
working of scanf function, try to predict the output first.
#include <conio.h>
#include <stdio.h>
void main(void)
{
clrscr();
int A;
int B;
printf(โ€œEnter a number : โ€);
scanf(โ€%dโ€,&A);
printf(โ€œValue of A = %d \nโ€, A);
printf(โ€œEnter another number : โ€);
scanf(โ€%dโ€,&B);
printf(โ€œValue of B = %d \nโ€, A);
printf(โ€The sum of %d and %d=โ€,A,B,A+B);
getch();
}
๎˜ƒ๎˜ƒTASKโ€01๎˜ƒ๎˜ƒ๎˜ƒ scanf
(
)
๎˜ƒ
Predicted Output
c:\>
c:\>
c:\>
c:\>
c:\>
c:\>
c:\>
c:\>
c:\>
c:\>
c:\>
c:\>
c:\>
c:\>
c:\>
c:\>
Docsity.com
pf3

Partial preview of the text

Download Lab-03 Instructions for Understanding scanf() function and for loop in C++ and more Exercises Computer Fundamentals in PDF only on Docsity!

Computer Fundamental

Lab-

Instructions

  1. Use Turbo C\C++ for the following programs
  2. Create a folder CF_Labs
  3. Create new folder for each lab e.g. create folder โ€œlabโ€02โ€ and save all the files in that folder for this lab
  4. Create new file for each of the following program
  5. For any help contact lab staff or faculty member available in the lab
  6. While leaving the lab close all programs and shutdown the computers, take a back up of your code
  7. Do not discard this handout

Objective The objectives of this lab are to

  • Understand the scanf() function and practice reading one or multiple values from keyboard for your program
  • Understand the format and working of for loop , by doing simple tasks that we did in the class.

For each of the following code try to understand the working of scanf function, try to predict the output first.

#include <conio.h> #include <stdio.h> void main(void) { clrscr(); int A; int B; printf(โ€œEnter a number : โ€); scanf(โ€%dโ€,&A); printf(โ€œValue of A = %d \nโ€, A); printf(โ€œEnter another number : โ€); scanf(โ€%dโ€,&B); printf(โ€œValue of B = %d \nโ€, A); printf(โ€The sum of %d and %d=โ€,A,B,A+B); getch(); }

TASKโ€ (^01) scanf()

Predicted Output c:> c:> c:> c:> c:> c:> c:> c:> c:> c:> c:> c:> c:> c:> c:> c:>

Program-01 : Print numbers from 1 to 10

#include <conio.h> #include <stdio.h> void main(void) { clrscr(); for(int a=0;a<11;a++){ printf(โ€œA = %d โ€œ,a); } getche(); }

Program-02: Print table of 2

#include <conio.h> #include <stdio.h> void main(void) { clrscr(); for(int a=0;a<=10;a++){ printf(โ€2 x %d = %d \nโ€œ,a,2*a); } getche(); }

Program-03: Printing even series , by using a for loop

#include <conio.h> #include <stdio.h> void main(void) { clrscr(); int sum=0; printf(โ€Even series\nโ€); for(int a=0;a<=10;a++){ printf( โ€œ%d โ€,sum); sum=sum+2; } getch(); }

Program-03A : We can print the same series by another way as given below

#include <conio.h> #include <stdio.h> void main(void) {

clrscr(); printf(โ€œEven series\nโ€œ); for(int a=0;a<=10;a=a+2){ printf(โ€œ%d โ€œ,a); } getch(); }

Program-04: Controlling the for loops from user input

The following program when executed gets two numbers from the users and prints the number between these two numbers. The first number entered should be smaller than the second number.

#include <stdio.h> #include <conio.h> void main(void){

int A,B; printf(โ€Enter value of A : โ€); scanf(โ€%dโ€,A);

printf(โ€\nEnter value of B : โ€ ); scanf(โ€%dโ€,B); printf(โ€\nNumbers between %d and %d are \nโ€,A,B); for(int a=A;a<B; a++) printf(โ€%d โ€,a); getch(); } Program-05: printing the sum of first 10 numbers #include <conio.h> #include <stdio.h> void main(void) { clrscr(); int sum=0; printf(โ€Even series\nโ€); for(int a=0;a<=10;a++){ sum=sum+a; } printf(โ€œThe sum of first 10 integers = %dโ€,sum); getch(); }

TASKโ€ (^02) Learn the syntax of for loop