[go: up one dir, main page]

0% found this document useful (0 votes)
22 views26 pages

PF-Week 08

programming fundamentals

Uploaded by

lachman das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views26 pages

PF-Week 08

programming fundamentals

Uploaded by

lachman das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Programming

Fundamental

Week 08

Sobia Iftikhar
Sobia.Iftikhar@nu.edu.pk
Class 19
Practice

– 1. Vary the control variable from 1 to 100 in increments of 1.


– for (unsigned int i = 1; i <= 100; ++i)
– 2. Vary the control variable from 100 to 1 in increments of -1 (i.e., decrements of 1).
– for (unsigned int i = 100; i >= 1; --i)
– 3. Vary the control variable from 7 to 77 in increments of 7.
– for (unsigned int i = 7; i <= 77; i += 7)
– 4. Vary the control variable from 20 to 2 in decrements of -2.
– for (unsigned int i = 20; i >= 2; i -= 2)
– 5. Vary the control variable over the following sequence of values: 2, 5, 8, 11, 14, 17.
– for (unsigned int j = 2; j <= 17; j += 3)
– 6. Vary the control variable over the following sequence of values: 44, 33, 22, 11, 0.
– for (unsigned int j = 44; j >= 0; j -= 11)
Task

– uses the for statement to sum all the even integers from 2 to 10
Task

– A person invests $1000.00 in a savings account yielding 5% interest. Assuming


that all interest is left on deposit in the account, calculate and print the amount of
money in the account at the end of each year for 10 years.
– Use the following formula for determining these amounts:
– a = p(1 + r)n where p is the original amount invested (i.e., the principal)
– r is the annual interest rate (for example, .05 for 5%)
– n is the number of years a is the amount on deposit at the end of the nth year.
Solution
Exercise

#include <stdio.h>
int main()
{
int n;// variable declaration
printf("Enter the value of n :");
// Displaying the n tables.
for(int i=1;i<=n;i++) // outer loop
{
for(int j=1;j<=10;j++) // inner loop
{
printf("%d\t",(i*j)); // printing the
value.
}
printf("\n");
}
Nested Loops

– for (initialization; condition; update)


– {
– for(initialization; condition; update)
– {
– // inner loop statements.
– }
– // outer loop statements.
– }
Nested While Loop

– while(condition)
– {
– while(condition)
– {
– // inner loop statements.
– }
– // outer loop statements.
– }
Example
Nested do While()

– do
– {
– do
– {
– // inner loop statements.
– }while(condition);
– // outer loop statements.
– }while(condition);
Example

1.int i=1;
2.do // outer loop
3.{
4. int j=1;
5. do // inner loop
6. {
7. printf("*");
8. j++;
9. }while(j<=8);
10. printf("\n");
11. i++;
12. }while(i<=4);
Task

– Write a program to print all the ASCII values and their equivalent characters
using a while loop. The ASCII values vary from 0 to 255.

main() {
int i=0;
clrscr();

while(i<=255) {

printf(" %d %c \n",i,i);
i++;
}
return 0;

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

int i=1,j;
printf("\nPrime numbers between 1 to 300 are:\n\n");

while(i!=10) { if(j==i) {
Task
j=2; printf(" %d",i); Write a program to print all prime
numbers from 1 to 10. (Hint: Use
while(j<i) { } nested loops, break and continue)

if(i%j==0) { i++;
break; printf(" -- i %d\n",i);
} }

j++; getch();
printf(" -- j %d\n",j); return 0;

}
}
– #include<stdio.h>
– #include<conio.h>
– main() {
– int i,j,k;
– printf("All combinations of 1,2,3 are:\n\n");
– for(i=1;i<=3;i++) {
– for(j=1;j<=3;j++) {
– for(k=1;k<=3;k++) {
Task
Write a program to generate all
– printf(" %d%d%d ",i,j,k);
combinations of 1, 2 and 3 using for
– } loop
– }
– }

– getch();
– return 0;
– }
Class 20
Example

Step by step descriptive logic to print empty square star pattern.

• Input number of rows to print from user. Store it in a variable say N.


• To iterate through rows, run an outer loop from 1 to N. For that define loop
with structure for(i=1; i<=N; i++).
• To iterate through columns, run an inner loop from 1 to N. Define loop with
structure for(j=1; j<=N; j++).
• Inside inner loop print star for first and last row or for first and last column.
Which is print star if i==1 or i==N or j==1 or j==N, otherwise print space.
• After printing all columns of a row, move to next line i.e. print a blank line
after inner loop.
Solution
Example

Step by step descriptive logic to print right triangle star pattern.

• Input number of rows to print from user. Store it in a variable say N.


• To iterate through rows run an outer loop from 1 to N with loop structure
for(i=1; i<=N; i++).
• To iterate through columns run an inner loop from 1 to i with loop structure
for(j=1; j<=i; j++). Inside the inner loop print star.
• After printing all columns of a row move to next line i.e. print new line.
Solution
Loops (Contd)

– Write a program to find the factorial value of any number entered through the
keyboard. main() {
long i,j,fact=1;

printf("Please enter any number: ");


scanf("%ld",&i);

for(j=i;j>=1;j--) {
fact=fact*j;

}
printf("Factorial value = %ld ",fact);
Return 0;
/* loop for making a space between patterns */

#include<stdio.h> for(k=i+1;k<=71;k++) {
#include<conio.h>
main() { if(k==71)
printf(" ");
int i,j,k,l;
clrscr(); if(k<71)
printf(" ");
for(i=71;i>=65;i--) { }
/* loop to print descending letters */ Example
/* loop for printing ascending Write a program to produce the
letters */ for(l=i;l>=65;l--) { following output:

for(j=65;j<=i;j++) { if(l==71) { /* to skip printing 'G' twice */


continue;
printf("%c ",j); }
printf("%c ",l);
} }
printf("\n");
}
return 0;
}
Arrays
What are Arrays ?

No doubt, this program will print the value of x as 10. Why so?
Because when a value 10 is assigned to x, the earlier value of x, i.e.
5, is lost. Thus, ordinary variables (the ones which we have used so
far) are capable of holding only one value at a time (as in the above
example). However, there are situations in which we would want to
store more than one value at a time in a single variable.
Arrays

An array is a variable that can store multiple values. For example, if you want to store 100
integers, you can create an array for it.
Class 21
Thankyou

You might also like