

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 3
This page cannot be seen from the preview
Don't miss anything!


Instructions
Objective The objectives of this lab are to
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