Presentation is loading. Please wait.

Presentation is loading. Please wait.

Control Statements_2.

Similar presentations


Presentation on theme: "Control Statements_2."— Presentation transcript:

1 Control Statements_2

2 Programming tasks Write a program to add an 8% sales tax to a given amount and round the result to the nearest cent. Write a program to tell if a number is prime. Write a program that takes a series of numbers and counts the number of positive and negative values.

3 int num,i,count=0; printf("Enter a number: "); scanf("%d",&num); for(i=2;i<=num-1;i++){ if(num%i==0){ count++; break; } if(count==0 && num!= 1) printf("%d is a prime number",num); else printf("%d is not a prime number",num); return 0;

4 Program to find factorial of a number
#include<stdio.h> #include<conio.h> void main() { int n,i,fact=1; printf(“Enter any no: ”); scanf(“%d”,&n); for(i=n;i>=1;i--) fact=fact*i; } printf(“Factorial=%d”,fact); getch();

5 #include <stdio.h>
int main(int numParms, char *parms[]) { int sumEven; int sumOdd; int count; sumEven = 0; sumOdd = 0; for (count=1; count<=5; count++) if ((count%2)==0) sumEven += count; } else sumOdd += count; }} printf("The sum of the even values is %d \n", sumEven); printf("The sum of the odd values is %d \n", sumOdd); return 0;

6 Scope and Class All variables have two attributes: scope and class. The scope of a variable is the area of the program in which the variable is valid. A global variable is valid everywhere (hence the name global), so its scope is the whole program. A local variable has a scope that is limited to the block in which it is declared and cannot be accessed outside that block. A block is a section of code enclosed in curly braces ({}).

7 Local and global variables

8 Cont.. You can declare a local variable with the same name as a global variable. Normally, the scope of the variable count (first declaration) would be the whole program. The declaration of a second local count takes precedence over the global declaration inside the small block in which the local count is declared. In this block, the global count is hidden . You can also nest local declarations and hide local variables

9


Download ppt "Control Statements_2."

Similar presentations


Ads by Google