Anda di halaman 1dari 10

Q-3

Differentiate between Global & Local variables with examples

Local Variable Global Variable


Local variables are declared inside a Global Variables are declared before the
function. main function.
Local Variables cannot be accessed outside Global Variables can be accessed in any
the function. function.
Local Variables are alive only for a function. Global Variables are alive till the end of the
program.

Example of local variable.


//program to add any two integers
void main()
{
int a,b,sum;
clrscr();
printf("Enter any two integer value");
scanf("%d%d",&a,&b);
sum=a+b;
printf("\nSum of two integers %d and %d is %d",a,b,sum);
}
Here, a,b, and sum are local variables which are declared in main function.

Example of global variable.


//program to find the sum and difference between two numbers
int a,b,result;
void main()
{
clrscr();
sum();
sub();
getch();
}
sum()
{
printf("Enter two numbers to find their sum");
scanf("%d%d",&a,&b);
result=a+b;
printf("\n the sum of two numbers is %d",result);
return 0;
}
sub()
{
printf("Enter two numbers to find their difference");
scanf("%d%d",&a,&b);
result=a-b;
printf("\n the difference between two numbers is %d",result);
return0;
}
Here, a,b and result are global variables which are declared before the main function.

Q-2
A goto statement in C programming provides an unconditional jump from the 'goto' to a
labeled statement in the same function.

Flow Diagram

Example:-

#include <stdio.h>

int main () {

/* local variable definition */


int a = 10;

/* do loop execution */
LOOP:do {

if( a == 15) {
/* skip the iteration */
a = a + 1;
goto LOOP;
}

printf("value of a: %d\n", a);


a++;

}while( a < 20 );

return 0;
}

Q-4

++ and -- operator as prefix and postfix


Suppose you use ++ operator as prefix like: ++var. The value of var is incremented by
1 then, it returns the value.

Suppose you use ++ operator as postfix like: var++. The original value of var is
returned first then, var is incremented by 1.

This is demonstrated examples in 4 different programming languages.

Example #1: C Programming


#include <stdio.h>
int main()
{
int var=5;

// 5 is displayed then, var is increased to 6.


printf("%d\n",var++);

// Initially, var = 6. It is increased to 7 then, it is displayed.


printf("%d",++var);

return 0;
}
Q-5
Operator precedence determines the grouping of terms in an expression and
decides how an expression is evaluated. Certain operators have higher
precedence than others; for example, the multiplication operator has a
higher precedence than the addition operator.

#include <stdio.h>

main() {

int a = 20;
int b = 10;
int c = 15;
int d = 5;
int e;

e = (a + b) * c / d; // ( 30 * 15 ) / 5
printf("Value of (a + b) * c / d is : %d\n", e );

e = ((a + b) * c) / d; // (30 * 15 ) / 5
printf("Value of ((a + b) * c) / d is : %d\n" , e );

e = (a + b) * (c / d); // (30) * (15/5)


printf("Value of (a + b) * (c / d) is : %d\n", e );

e = a + (b * c) / d; // 20 + (150/5)
printf("Value of a + (b * c) / d is : %d\n" , e );

return 0;
}

Associativity is used when two operators of same precedence appear in an expression.


Associativity can be either Left to Right or Right to Left.
Q-6

char
%c Character
unsigned char
short
unsigned short
%d Signed Integer
int
long
float
%e or %E Scientific notation of float values
double
%f Floating point float
float
%g or %G Similar as %e or %E
double
%hi Signed Integer(Short) short
%hu Unsigned Integer(Short) unsigned short
short
unsigned short
%i Signed Integer
int
long
%l or %ld or %li Signed Integer long
%lf Floating point double
%Lf Floating point long double
unsigned int
%lu Unsigned integer
unsigned long
%lli, %lld Signed Integer long long
%llu Unsigned Integer unsigned long long
short
unsigned short
%o Octal representation of Integer. int
unsigned int
long
%p Address of pointer to void void * void *
%s String char *
unsigned int
%u Unsigned Integer
unsigned long
short
unsigned short
%x or %X Hexadecimal representation of Unsigned Integer int
unsigned int
long
%n Prints nothing
%% Prints % character

Q-7

#define is a pre processor directive. Things defined by #define are replaced by the pre
processor before compilation begins.

#include <stdio.h>

#define NAME "TechOnTheNet.com"


#define AGE 10

int main()
{
printf("%s is over %d years old.\n", NAME, AGE);
return 0;
}

Q-8
The if else ladder statement in C programming language is used to test set of conditions in
sequence. An if condition is tested only when all previous if conditions in if-else ladder is
false. If any of the conditional expression evaluates to true, then it will execute the
corresponding code block and exits whole if-else ladder.

#include<stdio.h>
#include<conio.h>

int main(){
int marks;

printf("Enter your marks between 0-100\n");


scanf("%d", &marks);
/* Using if else ladder statement to print
Grade of a Student */
if(marks >= 90){
/* Marks between 90-100 */
printf("YOUR GRADE : A\n");
} else if (marks >= 70 && marks < 90){
/* Marks between 70-89 */
printf("YOUR GRADE : B\n");
} else if (marks >= 50 && marks < 70){
/* Marks between 50-69 */
printf("YOUR GRADE : C\n");
} else {
/* Marks less than 50 */
printf("YOUR GRADE : Failed\n");
}

getch();
return(0);
}

Q-8 C program to find Simple Interest

#include <stdio.h>

int main()
{
float principle, time, rate, SI;

printf("Enter principle (amount): ");


scanf("%f", &principle);

printf("Enter time: ");


scanf("%f", &time);

printf("Enter rate: ");


scanf("%f", &rate);

SI = (principle * time * rate) / 100;

printf("Simple Interest = %f", SI);

return 0;
}
Q-9 (1*1)+(2*2)+...+(n*n)

int main()
{
int i,n,sum=0;
n=10;
for(i=1;i<=n;i++)
{
sum+=i*i;
}
printf("Sum: %d",sum);
return 0;
}

Q-10 Armstrong number

#include <stdio.h>
int main()
{
int number, originalNumber, remainder, result = 0;

printf("Enter a three digit integer: ");


scanf("%d", &number);

originalNumber = number;

while (originalNumber != 0)
{
remainder = originalNumber%10;
result += remainder*remainder*remainder;
originalNumber /= 10;
}

if(result == number)
printf("%d is an Armstrong number.",number);
else
printf("%d is not an Armstrong number.",number);

return 0;
}
Q-11

break Statement
The break statement terminates the loop (for, while and do...while loop) immediately
when it is encountered. The break statement is used with decision making statement
such as if...else.

EX:-

#include<stdio.h>

int main()
{

/* local variable definition */


int a = 10;

/* while loop execution */


while( a < 20 ) {

printf("value of a: %d\n", a);


a++;

if( a > 15) {


/* terminate the loop using break statement */
break;
}
}
return 0;

}
continue Statement
The continue statement skips some statements inside the loop. The continue
statement is used with decision making statement such as if...else.

EX:-

#include <stdio.h>

int main () {

int a = 10;

do {

if( a == 15) {

a = a + 1;
continue;
}

printf("value of a: %d\n", a);


a++;

} while( a < 20 );

return 0;
}

Anda mungkin juga menyukai