Write A Program To Display The Multiplication Table of A Given Number
Write A Program To Display The Multiplication Table of A Given Number
Write A Program To Display The Multiplication Table of A Given Number
view source
print?
01 #include <stdio.h>
02 int main() {
03 int num, i = 1;
05 scanf("%d", &num);
10 }
11 return 0;
12 }
Download Code
Output:
Explanation: We need to multiply the given number (i.e. the number for which we
want the multiplication table) with value of ‘i’ which increments from 1 to 10.
Back to top
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Program:
view source
print?
01 #include<stdio.h>
02 int main() {
03 int i, j, k, c = 5;
08 printf(" ");
09 }
12 two spaces for alignment and the numbers are printed in the order. */
13 printf("%2d", i);
14 }
15 printf("\n");
16 /*c is decremented by 1 */
17 c--;
18 }
19 return 0;
20 }
Download Code
Output:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Explanation: Here ‘i’ loop is used for printing the numbers in the respective rows
and ‘k’ loop is used for providing spaces. ‘j’ loop prints the numbers. ‘c’ is decremented
for numbers to be displayed in alternate columns.
Back to top
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
Program:
view source
print?
01 #include<stdio.h>
02 int main() {
04 int i, j, c = 9, m, k;
08 printf(" ");
09 }
14 printf("%2d", m);
15 }
16 printf("\n");
17 /* c is decremented by 2 */
18 c = c - 2;
19 }
20 return 0;
21 }
Download Code
Output:
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
Explanation: Here ‘i’ loop is used for printing numbers in rows and ‘k’ loop is used
for providing spaces. ‘j’ loop is used for printing numbers in increasing order. ‘m’ loop is
used for printing numbers in reverse order.
Back to top
------
a b
------
1 5
2 4
3 3
4 2
5 1
------
Program:
view source
print?
01 #include<stdio.h>
02 int main() {
03 int i = 1, j = 5;
04 printf("----------\n");
05 printf("a \t b \n");
06 printf("----------\n");
07 /* logic: while loop repeats
10 while (i <= 5) {
17 }
18 printf("----------");
19 return 0;
20 }
Download Code
Output:
------
a b
------
1 5
2 4
3 3
4 2
5 1
------
Explanation: Here, ‘i’ is initialized to least value 1 and ‘j’ initialized to highest value
5. We keep incrementing the i’ value and decrementing the ‘j’ value until the condition
fails. The value is displayed at each increment and at each decrement. Back to top
--------
no. sum
--------
1 1
2 3
3 6
4 10
5 15
--------
Program:
view source
print?
01 #include<stdio.h>
02 int main() {
03 int num = 1, sum = 0;
04 printf("-----------\n");
05 printf("num \t sum\n");
06 printf("-----------\n");
07 /* while loop repeats 5 times
08 * i.e. until the condition
10 while (num <= 5) {
16 num++;
17 }
18 printf("-----------");
19 return 0;
20 }
Download Code
Output:
--------
no. sum
--------
1 1
2 3
3 6
4 10
5 15
--------
Explanation: In the above program we have taken two variables ‘num’ and ‘sum’.
‘num’ is used to check the condition and to display the numbers up to 5. ‘sum’ is used to
add the numbers which are displayed using variable ‘num’. The ‘sum’ value is initialized to
zero. sum is added to the numbers which are incremented by ‘i’ and displayed.
//////////////////////////////////////////////////////////////////////////////////////////////////////////
1
2 3
4 5 6
7 8 9 10
A static variable is a special variable that is stored in the data segment unlike the default
automatic variable that is stored in stack. A static variable can be initialised by using
keyword static before variable name.
For Example:
static int a = 5;
Example:
01 #include <stdio.h>
03 void count(void) {
04 static int count1 = 0;
05 int count2 = 0;
06 count1++;
07 count2++;
10
11 /*Main function*/
12 int main(){
13 count();
14 count();
15 count();
16 return 0;
17 }
Download Code
Output:
B ack to to p
2. What is a pointer?
A pointer is a special variable in C language meant just to store address of any other variable or
function. Pointer variables unlike ordinary variables cannot be operated with all the arithmetic
operations such as ‘*’,'%’ operators.
It follows a special arithmetic called as pointer arithmetic.
int *ap;
int a = 5;
In the above two statements an integer a was declared and initialized to 5. A pointer
to an integer with name ap was declared.
ap=&a;
This operation would initialize the declared pointer to int. The pointer ap is now said
to point to a.
Operations on a pointer:
o Dereferencing operator ‘ * ‘:
This operator gives the value at the address pointed by the pointer . For
example after the above C statements if we give
printf("%d",*ap);
o Addition operator ‘ + ‘:
ap=ap+1;
Above expression would not increment the value of ap by one, but would
increment it by the number of bytes of the data type it is pointing to. Here ap is
pointing to an integer variable hence ap is incremented by 2 or 4 bytes depending
upon the compiler.
A pointer is a special variable in C language meant just to store address of any other
variable or function. Pointer variables unlike ordinary variables cannot be operated with all
the arithmetic operations such as ‘*’,'%’ operators. It follows a special arithmetic called as
pointer arithmetic.
int *ap;
int a = 5;
In the above two statements an integer a was declared and initialized to 5. A pointer
to an integer with name ap was declared.
ap=&a;
This operation would initialize the declared pointer to int. The pointer ap is now said
to point to a.
B ack to to p
3. What is a structure?
int roll_number;
char name[30];
int total_marks;
This concept would be particularly useful in grouping data types. You could declare a
structure student as:
1 struct student {
2 int roll_number;
3 char name[30];
4 int total_marks;
5 } student1, student2;
The above snippet of code would declare a structure by name student and it
initializes two objects student1, student2. Now these objects and their fields could be
accessed by saying student1.
style='font-size:10.0pt;font-family:"Courier New";color:#0000C0'>roll_number for accesing
roll number field of student1 object, similarly student2.
style='font-size:10.0pt;font-family:"Courier New";color:#0000C0'>name for accesing name
field of student2 object.
B ack to to p
1
2 3
4 5 6
7 8 9 10
Program:
01 #include <stdio.h>
02
03 int main() {
04 int i, j, ctr = 1;
08 }
09 printf("\n");
10 }
11 return 0;
12 }
Download Code
Explanation:
There are two loops, a loop inside another one. Outer loop iterates 5 times. Inner loop
iterates as many times as current value of i. So for first time outer loop is executed, inner
loop is executed once. Second time the outer loop is entered, inner loop is executed twice
and so on. And every time the program enters inner loop, value of variable ctr is printed
and is incremented by 1. %2d ensures that the number is printed in two spaces for proper
alignment.
B ack to to p
Program:
01 #include <stdio.h>
02 int main() {
03 int i = 65;
04 int k = 120;
05 printf("\n value of i=%d k=%d before swapping", i, k);
06 i = i ^ k;
07 k = i ^ k;
08 i = i ^ k;
09 printf("\n value of i=%d k=%d after swapping", i, k);
10 return 0;
11 }
Download Code
Explanation:
i = i^k;
i...0100 0001
k...0111 1000
---------
val of i = 0011 1001
---------
k = i^k
i...0011 1001
k...0111 1000
---------
val of k = 0100 0001 binary equivalent of this is 65
---------(that is the initial value of i)
i = i^k
i...0011 1001
k...0100 0001
---------
val of i = 0111 1000 binary equivalent of this is 120
---------(that is the initial value of k)
//////////////////////////////////////////////////////////////////////////////////////////////////////////
///
01 #include <stdio.h>
02 int fact(int n);
03
04 int main() {
05 int x, i;
08 i = fact(x);
10 return 0;
11 }
12
13 int fact(int n) {
16 return (1);
17 } else {
22 }
Download Code
Output:
Factorial of 4 is 24
Explanation:
fact(n) = n * fact(n-1)
If n=4
fact(4) = 4 * fact(3) there is a call to fact(3)
fact(3) = 3 * fact(2)
fact(2) = 2 * fact(1)
fact(1) = 1 * fact(0)
fact(0) = 1
fact(1) = 1 * 1 = 1
fact(2) = 2 * 1 = 2
fact(3) = 3 * 2 = 6
Thus fact(4) = 4 * 6 = 24
Terminating condition(n <= 0 here;) is a must for a recursive program. Otherwise the
program enters into an infinite loop.
Back to top
1101,1001,0011,1100
1101 = D
1001 = 9
0011 = 3
1100 = C
1101,1001,0011,1100 = 0xD93C
STRUCTURE UNION
a)Declaration: a)Declaration:
struct union
{ {
data type member1; data type member1;
data type member2; data type member2;
}; };
b)Every structure member is allocated
b)The memory equivalent to the largest item is
memory when a structure variable is
allocated commonly for all members.
defined.
Example:
Example:
union emp1 {
struct emp {
char name[5];
char name[5];
int age;
int age;
float sal;
float sal;
};
};
union emp1 e2;
struct emp e1;
Back to top
If a union has two members of different data types, they are allocated the same
memory. The memory allocated is equal to maximum size of the members. The data is
interpreted in bytes depending on which member is being accessed.
Example:
1 union pen {
2 char name;
3 float point;
4 };
Here name and point are union members. Out of these two variables, ‘point’ is larger
variable which is of float data type and it would need 4 bytes of memory. Therefore 4
bytes space is allocated for both the variables. Both the variables have the same memory
location. They are accessed according to their type.
Union is efficient when members of it are not required to be accessed at the same time.
Back to top
10. What is scope & storage allocation of global and extern variables? Explain with an
example
Extern variables: belong to the External storage class and are stored in the main
memory. extern is used when we have to refer a function or variable that is implemented
in other file in the same project. The scope of the extern variables is Global.
Example:
01 /***************
02 Index: f1.c
03 ****************/
04 #include <stdio.h>
05 extern int x;
06
07 int main() {
10 }
Download Code
1 /***************
2 Index: f2.c
3 ****************/
4 int x = 3;
Here, the program written in file f1.c has the main function and reference to variable
x. The file f2.c has the declaration of variable x. The compiler should know the datatype of
x and this is done by extern definition.
Global variables: are variables which are declared above the main( ) function.
These variables are accessible throughout the program. They can be accessed by all the
functions in the program. Their default value is zero.
Example:
01 #include <stdio.h>
02 int x = 0;
06 x = x + 1;
08 }
09
10 int main(){
11 printf("\n value of x: %d", x);
12 increment();
13 return 0;
14 }
Download Code
Back to top
11. What is scope & storage allocation of static, local and register variables? Explain with an
example.
Register variables: belong to the register storage class and are stored in the CPU
registers. The scope of the register variables is local to the block in which the variables
are defined. The variables which are used for more number of times in a program are
declared as register variables for faster access.
Example: loop counter variables.
register int y=6;
Static variables: Memory is allocated at the beginning of the program execution and
it is reallocated only after the program terminates. The scope of the static variables is
local to the block in which the variables are defined.
Example:
01 #include <stdio.h>
02 void decrement(){
03 static int a=5;
04 a--;
06 }
07
08 int main(){
09 decrement();
10 return 0;
11 }
Download Code
Here ‘a’ is initialized only once. Every time this function is called, ‘a’ does not get
initialized. so output would be 4 3 2 etc.,
Local variables: are variables which are declared within any function or a block.
They can be accessed only by function or block in which they are declared. Their default
value is a garbage value.
//////////////////////////////////////////////////////////////////////////////////////////////////////
Pass by Value: In this method, the value of each of the actual arguments in the
calling function is copied into corresponding formal arguments of the called function. In
pass by value, the changes made to formal arguments in the called function have no effect
on the values of actual arguments in the calling function.
Example:
01 #include <stdio.h>
02
03 void swap(int x, int y) {
04 int t;
05 t = x;
06 x = y;
07 y = t;
08 }
09
10 int main() {
13 swap(m, n);
16 }
Download Code
Output:
Explanation:
In the main function, value of variables m, n are not changed though they are passed to
function ‘swap’. Swap function has a copy of m, n and hence it can not manipulate the
actual value of arguments passed to it.
Back to top
Pass by Reference: In this method, the addresses of actual arguments in the calling
function are copied into formal arguments of the called function. This means that using
these addresses, we would have an access to the actual arguments and hence we would
be able to manipulate them. C does not support Call by reference . But it can be simulated
using pointers.
Example:
01 #include <stdio.h>
02 /* function definition */
03 void swap(int *x, int *y) {
04 int t;
08 }
09
10 int main() {
13 swap(&m, &n);
16 }
Download Code
Output:
Explanation:
In the main function, address of variables m, n are sent as arguments to the function
‘swap’. As swap function has the access to address of the arguments, manipulation of
passed arguments inside swap function would be directly reflected in the values of m, n.
Back to top
Enumeration is a data type. We can create our own data type and define values that
the variable can take. This can help in making program more readable. enumdefinition is
similar to that of a structure.
1 enum light_status
2 {
3 on, off
4 };
5
6 enum light_status bulb1, bulb2;
7 /* bulb1, bulb2 are the variables */
Example:
1 struct employee {
2 char name[20];
3 int age;
4 };
5
6 struct employee e;
The above declaration of the structure would be easy to use when renamed using
typedef as:
1 struct employee {
2 char name[20];
3 int age;
4 };
5
6 typedef struct employee EMP;
7 EMP e1, e2;
Back to top
16. What are register variables? What are advantages of using register variables?
Register variables are stored in the CPU registers. Its default value is a garbage
value. Scope of a register variable is local to the block in which it is defined. Lifetime is till
control remains within the block in which the register variable is defined. Variable stored
in a CPU register can always be accessed faster than the one that is stored in memory.
Therefore, if a variable is used at many places in a program, it is better to declare its
storage class as register
Example:
register int x=5;
Variables for loop counters can be declared as register. Note that register keyword may be
ignored by some compilers.
http://www.chiteswarsolutions.com/viewFreshers.jsp?tutorialId=1602
http://interviewmantra.net/2008/10/c-interview-questions-with-solutions-2.html