Comment "C Is Middle Level Language"? 2. What Is Problem Solving?
Comment "C Is Middle Level Language"? 2. What Is Problem Solving?
Algorithm
An algorithm is a step-by-step description of the solution to a problem. It is
defined as an ordered sequence of well-defined and effective operations which,
when carried out for a given set of initial conditions, produce output, and terminate
in a finite time. The term “ordered sequence” specifies, after the completion of each
step in the algorithm, the next step must be unambiguously defined.
An algorithm must be:
• Definite
• Finite
• Precise and Effective
• Implementation independent ( only for problem not for programming
languages)
5. Discuss about how the problem can be solved with computers?
6. What is a program?
Properties of an algorithm:
Constants Variables
A value that can not be altered throughout A storage location paired with an
the program associated symbolic name which has a
value
It is similar to a variable but it cannot be A storage area holds data
modified by the program once defined
Can not be changed Can be changed according to the need of
the programmer
Value is fixed Value is varying
➢ The first letter of a variable name must be alphabet (underscore is also allowed).
➢ Variable can only contain letters, digits, and underscores.
➢ White space is not allowed.
➢ Keywords cannot be used as an identifier to name variables.
➢ C is case sensitive language variable name written in uppercase will differ from
lowercase
16. State the use of %d and %f. write a print statement in C using above mentioned
symbol?
Code Output
#include<stdio.h> Enter the value of a and b:10 10.5
void main() a=10
{ b=10.5
int a;
float b;
printf(“Enter the value of a and b:”);
scanf(“%d%f”,&a,&b);
printf(“a= %d\nb= %f”,a,b);
}
17. What is main difference between variable and constant?
Constants Variables
A value that can not be altered throughout A storage location paired with an
the program associated symbolic name which has a
value
It is similar to a variable but it cannot be A storage area holds data
modified by the program once defined
Can not be changed Can be changed according to the need of
the programmer
Value is fixed Value is varying
The bitwise shift operators move the bit values of a binary object. The left operand
specifies the value to be shifted. The right operand specifies the number of positions that the bits
in the value are to be shifted.
<< (left shift) Takes two numbers, left shifts the bits of the first operand, the second
operand decides the number of places to shift. Or in other words left shifting an integer “x” with
an integer “y” (x<<y) is equivalent to multiplying x with 2^y (2 raise to power y).
code output
#include<stdio.h> the value
int main()
{
int a=5; int b,c;
b=a<<1;
c=a<<2;
printf("after 1 bit left shift of a is: %d\n", b);
printf("after 2 bit left shift of a is: %d\n", c);
return 0;
}
19. Explain the different data types supported in C.
Data Types
Data types are used to indicate the type of value represented or stored in a variable, the
number of bytes to be reserved in memory, the range of values that can be represented in
memory, and the type of operation that can be performed on a particular data item.
A scope is a region of the program, and the scope of variables refers to the area of the
program where the variables can be accessed after its declaration.
In C every variable defined in scope. You can define scope as the section or region of a
program where a variable has its existence; moreover, that variable cannot be used or accessed
beyond that region.The variable can be declared in three places. These are:
Position Type
Inside a function or a block local variables
Out of all functions. Global variables
In the function parameters. Formal parameters
Example:
Code output
#include<stdio.h> 11-1-1
int main()
{
print(“%d”,4 % 3);
print(“%d”,4 % -3);
print(“%d”,-4 % 3);
print(“%d”,-4 % -3);
return 0;
}
A storage class defines the scope (visibility) and life-time of variables and/or functions
within a C Program. They precede the type that they modify. We have four different storage
classes in a C program −
1. auto
2. register
3. static
4. extern
auto
➢ The auto storage class is the default storage class for all local variables
register
➢ The register storage class is used to define local variables that should be
stored in a register instead of RAM. This means that the variable has a
maximum size equal to the register size (usually one word) and can't have
the unary '&' operator applied to it (as it does not have a memory
location).
➢ The register should only be used for variables that require quick access
such as counters.
static
➢ The static storage class instructs the compiler to keep a local variable in
existence during the life-time of the program instead of creating and
destroying it each time it comes into and goes out of scope. Therefore,
making local variables static allows them to maintain their values between
function calls.
extern
➢ The extern storage class is used to give a reference of a global variable
that is visible to ALL the program files. When you use 'extern', the
variable cannot be initialized however, it points the variable name at a
storage location that has been previously defined.
➢ When you have multiple files and you define a global variable or
function, which will also be used in other files, then extern will be used in
another file to provide the reference of defined variable or function. Just
for understanding, extern is used to declare a global variable or function
in another file.
➢ The extern modifier is most commonly used when there are two or more
files sharing the same global variables or functions as explained below.
28. Solve the following program and find its output:
Code Output
#include<stdio.h> x=3 y=9,z=27
int main()
{
int x=3,y,z;
z=y=x;
z*=y=x*x;
printf(“x=%d y=%d,z=%d”,x,y,z);
return 0;
}
29. Write a C program to get Principal, Rate of Interest and No of Years as input from
the user and calculate the simple interest.
Code Output
#include<stdio.h> enter the principal amount: 10000
#include<math.h> enter the rate of interest: 2.5
int main() enter the no of years: 5
{ the simple interest is: 1250.00
int p,y;
float i,si;
printf("enter the principal amount:
");
scanf("%d",&p);
printf("enter the rate of interest: ");
scanf("%f",&i);
printf("enter the no of years: ");
scanf("%d",&y);
si=((p*y*i)/100);
printf("\nthe simple interest is:
%.2f",si);
return 0;
}
30. If a four digit number is input through the keyboard , write a C program to obtain
the sum of first and last digit of the number.
Code Output
#include<stdio.h> Enter the 4 digit number: 1234
#include<math.h> The sum of 1st and 4th digit is: 5
int main()
{
int a,sum,digit1,digit4;
printf("Enter the 4 digit number: ");
scanf("%d",&a);
digit1=a%10;
digit4=a/1000;
sum=digit1+digit4;
printf("\nThe sum of 1st and 4th
digit is: %d",sum);
return 0;
}
31. If the total selling price of 15 items and the total profit=25% is input through the
keyboard , write the c program to find the cost of one item.
code #include<stdio.h>
int main()
{
float p;
printf("enter the profit amount: ");
scanf("%f",&p);
float total=(p*100)/25;
printf("price of one item is:
%.2f",total/15);
return 0;
}
output enter the profit amount: 200
price of one item is: 53.33
32. Explain with example ++i and i++
There is a big distinction between the suffix and prefix versions of ++.
In the pre-increment version (i.e., ++i), the value of i is incremented, and the value of the
expression is the new value of i. So basically it first increments then assigns a value to the
expression.
In the postincrement version (i.e., i++), the value of i is incremented, but the value of the
expression is the original value of i. So basically it first assigns a value to expression and
then increments the variable.
Code Output
#include<stdio.h> 10
int main() 12
{
int a=10;
a=a++;
printf(“%d\n”,a);
a=++a;
return 0;
}
Code Output
#include<stdio.h> the value of a is: 2
#include<math.h> the value of b is: 3
int main() the result of (a+b)^2 is: 25
{
int a,b,c;
printf("enter the values: ");
scanf("%d%d",&a,&b);
printf("\nthe value of a is: %d",a);
printf("\nthe value of b is: %d",b);
c = (pow(a,2)+pow(b,2)+(2*a*b));
printf("\nthe result of (a+b)^2 is: %d",c);
return 0;
}
34. Solve the following program and find its output:
Code Output
#include<stdio.h> 10
int main() 11
{ 11
int a=10; 10
a=a++;
printf(“%d\n”,a);
a=++a;
printf(“%d\n”,a);
a=a--;
printf(“%d\n”,a);
a=--a;
printf(“%d\n”,a);
return 0;
}