PSTC QB

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

PSTC QB

. What is a C Constant? Demonstrate various types of constant declaration


with examples.
Ans: A constant is aa value or variable that can’t be changed in the program, for
example: 10, 20, ‘a’, 3.4, “ c programming” etc. There are different types of
constants in C programming.
● Decimal Constant: 10,20,450 etc.
● Real or Floating-Point Constant: 10.3, 20.2, 450.6 etc.
● Octal Constant: 021, 033, 046 etc.
● Hexadecimal Constant: 0x2a, 0x7b, 0xaa etc.
● Character Constant: ‘a’, ‘b’, ‘x’ etc.
● String Constant: “c”, “c program”, “c in javatpoint” etc.
There are two ways to define constant in C
2
a. const KEYWORD
b. preprocessor
A: INPUT: <stdio.h> INPUT:
<stdio.h>
int main(){ int main(){
const float PI= 3.14; const float PI= 3.14;
printf(“The value of PI is: %f”, PI); PI=4.5;
Return 0; printf(“The value
of PI is: %f”,PI);
} return 0;
}
OUTPUT: The value of PI is : 3.140000 OUTPUT: Compile
Time Error: Cannot modify a const object.
B: INPUT: <stdio.h>
PI 3.14
main(){
printf(“%f”,PI);
}
OUTPUT: 3.140000
. Define variable. What are the naming rules for a variable? Give example
Ans: A variable is a name of the memory location. It is used to store data. Its
value can be changed, and it can be reused many times. It is a way to represent
memory location through symbol so that it can be easily identified.
Syntax: type variable_list;
int a; float b; char ch; Here a, b, c are variables. The int, float, char are the data
types.
Rules for Defining Variables:
● A variable can have alphabets, digits, and underscore.
● A variable name can start with the alphabet, and underscore only. It can't
start with a digit.
● No whitespace is allowed within the variable name.
1
● A variable name must not be any reserved word or keyword, e.g. int, float,
3

etc.
INVALID Variable Names: int 2; int a b; int long
VALID Variable Names: int a; int _ab; int a30;
Types of Variables in C: local, global, static, automatic, external
. What are identifiers in C. List out the rules for identifiers.
Ans: Identifiers are names given to different entities such as constants,
variables, structures, functions, etc.
Rules for naming:
● An identifier can only have alphanumeric characters (a-z , A-Z , 0-9) (i.e.
letters & digits) and underscore( _ ) symbol.
● Identifier names must be unique
● The first character must be an alphabet or underscore.
● You cannot use a keyword as identifiers.
● Only the first thirty-one (31) characters are significant.
● It must not contain white spaces.
● Identifiers are case-sensitive.
. Explain the structure of a C program
Ans: The structure of a C program means the specific structure to start the
programming in the C language. Without a proper structure, it becomes difficult
to analyze the problem and the solution. It also gives us a reference to write
more complex programs.
a. Documentation section: It can be used to write the program's objective,
developer and logic details.
b. Linking section: This section tells the compiler to link the certain
occurrences of keywords or functions in your program to the header files
specified in this section. Ex: <stdio.h>
c. Definition section: It is used to declare some constants and assign them
some value. Ex: MAX 25
d. Global Declaration Section: Here the variables which are used through out
the program (including main and other functions) are declared so as to make
them global(i.e accessible to all parts of program). Ex: int i; [before main()]
e. Main function section: It tells the compiler where to start the execution from
{
Declaration section: In this the variables and their data types are declared.
Executable section: This has the part of program which actually performs the
task we need.
}
f. Sub Program or Function Section: This has all the sub programs or the
functions which our program needs.
/* simple program in c */
<stdio.h>
main()
{
printf(“Welcome to my World”);
} /* End of main*/
. Write a C program to read Fahrenheit Temperature and convert to Celsius.
Ans: INPUT: #include <stdio.h>
int main(){
float F,C;
printf("Enter Fahrenheit Value: ");
scanf("%f",&F);
C=((F-32)*5)/9;
printf("The Celsius Value is: %f\n",C);
}

OUTPUT: Enter Fahrenheit Value: 210


The Celsius Value is: 98.888885
. Write a C Program to read the marks in 3 subjects and display the average.
Ans: #include <stdio.h>
int main(){
int IDS,AIT,PSTC;
float Avg;
printf("Enter a the marks scored in Three subjects: ");
scanf("%d %d %d",&IDS,&AIT,&PSTC);
Avg=(IDS+AIT+PSTC)/3;
printf("The Average Marks are: %f\n",Avg);
return 0;
}
OUTPUT: Enter a the marks scored in Three subjects: 99 95 91
The Average Marks are: 95.000000
. Explain about Relational and Logical Operators in C
Ans: Relational Operators: We often compare two quantities and depending on
their relation take certain decisions for that comparison we use relational
operators.
operator meaning
< is less than
> is greater than
<= is less than or equal to
>= is greater than or equal to == is equal to
!= is not equal to
Logical Operators: An expression of this kind which combines two or more
relational expressions is termed as a logical expressions or a compound
relational expression.
Logical AND, OR, NOT
. Explain about Type Casting in C
Ans: Type conversion is the process that converts the predefined data type of
one variable into an appropriate data type. The main idea behind type
conversion is to convert two different data type variables into a single data type
to solve mathematical and logical expressions easily without any data loss.
Type conversion can be done in two ways in C, one is implicit type
conversion, and the second is explicit type conversion.
Implicit Type Conversion: The implicit type conversion is the type of conversion
done automatically by the compiler without any human effort. It means an
implicit conversion automatically converts one data type into another type
based on some predefined rules of the C compiler. Hence, it is also known as
the automatic type conversion.

Explicit Type Conversion: Conversions that require user intervention to change


the data type of one variable to another, is called the explicit type conversion.
The explicit type conversion is divided into two ways:
a. Explicit conversion using the cast operator: cast operator is a unary operator
who forcefully converts one type into another type.
b. Explicit conversion using the assignment operator
. Explain any four Bitwise Operators with Example.
Ans: Bitwise AND: The result of Bitwise AND operation is 1 if both the bits are 1.
Example: x= 13 -> 1101
y= 15 -> 1111
x&y= 1101 -> 13
Bitwise OR: The result of Bitwise OR operation is 1 if any of the bits is 1.
Example: x= 13 -> 1101
y= 15 -> 1111
x|y= 1111 -> 15
Bitwise XOR: The result of Bitwise Exclusive OR (XOR) is 1 if and only if
one of the bits is 1.
Example: x= 13 -> 1101
y= 15 -> 1111
x^y= 0010 -> 2
Bitwise NOT: The Bitwise NOT operator inverts the bits of its operand. It
is nothing but 1’s complement
Example: x = 5 -> 0000 0000 0000 0000 0000 0000 0000 0101
~x= 1111 1111 1111 1111 1111 1111 1111 1010 -> -6
Left Shift: Left Shifting an integer x with y i.e., x<<y = x * (2^y)
Example: x= 21, x<<2 = 21 * (2^2) = 84
Right Shift: Right Shifting an integer x with y i.e., x>>y = x / (2^y)
Example: x=21, x>>2 = 21 / (2^2) = 5
. Write a C program for swapping of two numbers using a third variable.
Ans: #include <stdio.h>
int main(){
int a,b,c;
printf("Enter Two Numbers:");
scanf("%d %d",&a,&b);
c=a;
a=b;
b=c;
printf("The Numbers After Swapping are a=%d and b=%d\n",a,b);
return 0;
}
OUTPUT: Enter Two Numbers:10 12
The Numbers After Swapping are a=12 and b=10
. Write a C program to implement increment and decrement operators.
Ans: #include <stdio.h>
int main(){
int a=10,b=12;
printf("The initial value of a = %d\n",a);
printf("The initial value of b = %d\n",b);
a++;
printf("After Increment by 1: a = %d\n",a);
b--;
printf("After Decrement by 1: b = %d\n",b);
return 0;
}
OUTPUT: The initial value of a = 10
The initial value of b = 12
After Increment by 1: a = 11
After Decrement by 1: b = 11
. Write a C program to implement bitwise operators.
Ans: #include <stdio.h>
int main(){
int a=13, b=15;
printf("a&b = %d\n",a&b);
printf("a|b = %d\n",a|b);
printf("a^b = %d\n",a^b);
printf("a<<1 = %d\n",a<<1);
printf("b>>1 = %d\n",b>>1);
printf("~a = %d\n",a=~a);
}
OUTPUT: a&b = 13
a|b = 15
a^b = 2
a<<1 = 26
b>>1 = 7
~a = -14
. Write a C program to find the greatest of three numbers using conditional
operator.
Ans: #include <stdio.h>
int main(){
int a,b,c,max;
printf("Enter Three Numbers:");
scanf("%d %d %d",&a,&b,&c);
max = (a>b)?((a>c)?a:c):((b>c)?b:c);
printf("The Greatest Number is: %d\n",max);
}
OUTPUT: Enter Three Numbers:13 15 12
The Greatest Number is: 15
. Explain about printf() and scanf() in C language
Ans: Scanf: The scanf() function is used for input. It reads the input data
from the console.
scanf("format string",argument_list);
Printf: The printf() function is used for output. It prints the given
statement to the console.
printf("format string",argument_list);  
The format string can be %d (integer), %c (character), %s (string), %f (float)
etc.
#include<stdio.h>    
int main(){    
int number;    
printf("enter a number:");    
scanf("%d",&number);    
printf("cube of number is:%d ",number*number*number);    
return 0;  
}   
The scanf("%d",&number) statement reads integer number from the console
and stores the given value in number variable.
The printf("cube of number is:%d ",number*number*number) statement
prints the cube of number on the console.
. Write a C program to check whether the given number is even or odd.
Ans: #include <stdio.h>
int main(){
int a;
printf("Enter a Number: ");
scanf("%d",&a);
if (a%2==0) {
printf("The Given Number is EVEN\n");
} else {
printf("The Given Number is ODD\n");
}
return 0;
}
OUTPUT: Enter a Number: 13
The Given Number is ODD
. Write a program to calculate the largest of two numbers.
Ans: #include <stdio.h>
int main(){
int a,b;
printf("Enter Two Numbers: ");
scanf("%d %d",&a,&b);
if (a>=b) {
printf("%d is the Greatest Number\n",a);
} else {
printf("%d is the Greatest Number\n",b);
}
return 0;
}
OUTPUT: Enter Two Numbers: 14 21
21 is the Greatest Number
. Where is the program to check the given year is leap year or not
Ans: #include <stdio.h>
int main(){
int year;
printf("Enter a Year:");
scanf("%d",&year);
if (((year%4==0) && ((year%400==0) || (year%100!=0)))){
printf("Bingo! It's A Leap Year\n");
} else {
printf("Sorry It's Not A Leap Year\n");
}
return 0;
}
OUTPUT: Enter a Year:2010
Sorry It's Not A Leap Year
. Explain about Nested-If statement in C.
Ans: When an if else statement is present inside the body of another “if” or
“else” then this is called nested if else.
if(condition) {
//Nested if else inside the body of "if"
if(condition2) {
//Statements inside the body of nested "if"
}
else {
//Statements inside the body of nested "else"
}
}
else {
//Statements inside the body of "else"
}

. Write a C program to find out the roots of a quadratic equation.


Ans: #include <stdio.h>
#include <math.h>
int main(){
int a,b,c,d;
float x1,x2;
printf("Give the values of a,b,c");
scanf("%d %d %d",&a,&b,&c);
d=(b*b)-(4*a*c);
if(d==0){
printf("Both the Roots are EQUAL");
x1 = -b/(2*a);
x2 = x1;
printf("Root I = %f\n",x1);
printf("Root II = %f\n",x2);
} else if (d>0){
printf("Roots are REAL and DISTINCT");
x1 = (-b + sqrt(d))/(2*a);
x2 = (-b + sqrt(d))/(2*a);
printf("Root I = %f\n",x1);
printf("Root II = %f\n",x2);
}else
printf("Roots are Imaginary\nNo Solution\n");
}
OUTPUT: Give the values of a,b,c 5 6 2
Roots are Imaginary
No Solution
. Explain about else… if… ladder in C
The else..if statement is useful when you need to check multiple conditions
within the program, nesting of if-else blocks can be avoided using else..if
statement.
if (condition1)
{
//These statements would execute if the condition1 is true
}
else if(condition2)
{
//These statements would execute if the condition2 is true
}
else if (condition3)
{
//These statements would execute if the condition3 is true
}
.
.
else
{
//These statements would execute if all the conditions return false.
}
. Write a C program to read 3 subject Marks. Calculate and display the
grade of a student based on the following percentages.
< 40 - Fail
Between 41 to 50 – C grade
Between 51 to 60 – B grade
Between 61 to 75 – A grade
Greater than 75 – distinction
Ans: #include <stdio.h>
int main(){
int LAO,AIT,IDS,total;
float per;
printf("Enter marks scored: ");
scanf("%d %d %d",&LAO,&AIT,&IDS);
total = LAO+AIT+IDS;
per = (total*100)/300;
printf("Your Percentage Score for Three Subjects is: %f\n",per);
if(per<0 || per>100){
printf("ERROR!\n");
} else if (per<40){
printf("FAIL!\n");
} else if (per>=41&&per<=50){
printf("GRADE C\n");
}else if (per>=51&&per<=60){
printf("GRADE B\n");
} else if (per>=61&&per<=70){
printf("GRADE A\n");
} else
printf("DISTINCTION\n");
return 0;
}
OUTPUT: Enter marks scored: 92 96 97
Your Percentage Score for Three Subjects is: 95.000000
DISTINCTION
. Write a C program to check whether a person is eligible or not to cast a
vote.
Ans: #include <stdio.h>
int main()
{
int age;
printf("Enter your age: ");
scanf("%d",&age);
(age>=18)? (printf("Eligible for Voting\n")) : (printf("Not Eligible for
Voting\n"));
return 0;
}
OUTPUT: Enter your age: 20
Eligible for Voting
. EXPLAIN ABOUT TWO-WAY SELECTION STATEMENT
Ans: A two way selection (if/else) is written when there are two sets of
statements: one to be executed when the Boolean condition is true, and
another set for when the Boolean condition is false. The body of the “if”
statement is executed when the Boolean condition is true, and the body of the
“else” is executed when the Boolean condition is false.
// A Block if/else statement //A single if/else statement
if (boolean expression) if (boolean expression){
{ Do statement;
statement1; } else{
statement2; Do other statement;
} }
else
{
do other statement;
and another one;
}
. Explain about switch statement.
Ans: The switch statement in C is an alternate to if-else-if ladder statement
which allows us to execute multiple operations for the different possibles values
of a single variable called switch variable. Here, We can define various
statements in the multiple cases for the different values of a single variable.
switch(expression){    
case value1:    
 //code to be executed;    
 break;  //optional  
case value2:    
 //code to be executed;    
 break;  //optional  
......    
default:     
 code to be executed if all cases are not matched;    
}    
. Write a C program to perform Arithmetic Operations using switch
Ans: #include <stdio.h>
int main(){
int a,b,x;
printf("Enter the values of a and b:");
scanf("%d %d",&a,&b);

printf("1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n5.Modulus\n");
printf("Enter your Choice:");
scanf("%d",&x);
switch (x) {
case 1:
printf("The Sum of Two Numbers = %d\n",a+b);
break;
case 2:
printf("The Difference of Two Numbers = %d\n",a-b);
break;
case 3:
printf("The Product of Two Numbers = %d\n",a*b);
break;
case 4:
printf("The Division of Two Numbers = %d\n",a/b);
break;
case 5:
printf("The Remainder of Two Numbers = %d\n",a%b);
break;

default:
printf("Incorrect Input of Choice!\n");
break;
}
}
OUTPUT: Enter the values of a and b:10 6
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Modulus
Enter your Choice:5
The Remainder of Two Numbers = 4
. Write a C program to check whether the given Letter is Vowel or
Consonant.
Ans:
#include <stdio.h>
int main()
{
char ch;
printf("Enter any Alphabet\n");
scanf("%c",&ch);
switch(ch){
case 'a':
printf("%c is a vowel",ch);
break;
case 'e':
printf("%c is a vowel",ch);
break;
case 'i':
printf("%c is a vowel",ch);
break;
case 'o':
printf("%c is a vowel",ch);
break;
case 'u':
printf("%c is a vowel",ch);
break;
//check upper case vowel letters
case 'A':
printf("%c is a vowel",ch);
break;
case 'E':
printf("%c is a vowel",ch);
break;
case 'I':
printf("%c is a vowel",ch);
break;
case 'O':
printf("%c is a vowel",ch);
break;
case 'U':
printf("%c is a vowel",ch);
break;
default:
printf("%c is a consonant",ch);
}
return 0;
}
OUTPUT: Enter any Alphabet
F
F is a consonant
. Write a C program to find the Sum of n natural numbers
Ans: #include <stdio.h>
int main(){
int n,sum=0,i;
printf("Enter the no of Terms:");
scanf("%d",&n);
for (i=1; i<=n; i++) {
sum+=i;
}
printf("The sum of N Natural Numbers is: %d\n",sum);
return 0;
}
OUTPUT: Enter the no of Terms:6
The sum of N Natural Numbers is: 21
. Write a C program to find a factorial of a given Number.
Ans: #include <stdio.h>
int main(){
int i,f=1,number;
printf("Enter a Number:");
scanf("%d",&number);
for (i=1; i<=number; i++) {
f*=i;
}
printf("The Factorial of %d is: %d\n",number,f);
}
OUTPUT: Enter a Number: 5
The Factorial of 5 is: 120
. Write a C program to print Fibonacci numbers.
Ans: #include<stdio.h>
int main()
{
int count, n1 = 0, n2 = 1, n3, i;
printf("Enter the number of terms:");
scanf("%d",&count);

printf("First %d terms of Fibonacci series:\n",count);


for ( i = 0 ; i < count ; i++ )
{
if ( i <= 1 )
n3 = i;
else
{
n3 = n1 + n2;
n1 = n2;
n2 = n3;
}
printf("%d ",n3);
}
printf("\n");
return 0;
}
OUTPUT: Enter the number of terms:5
First 5 terms of Fibonacci series:
01123
. Write a program to find the reverse of a number.
Ans: #include<stdio.h>
int main()
{
int n, reverse=0, rem;
printf("Enter a number: ");
scanf("%d", &n);
while(n!=0)
{
rem=n%10;
reverse=reverse*10+rem;
n/=10;
}
printf("Reversed Number: %d",reverse);
return 0;
}
Output:

Enter a number: 123


Reversed Number: 321
. Write a C program to check the given number is Armstrong or not
Ans: #include<stdio.h>
int main()
{
int n,r,sum=0,temp;
printf("enter the number=");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(temp==sum)
printf("armstrong number ");
else
printf("not armstrong number");
return 0;
}
Output:

enter the number=153


armstrong number
. GCD of given two numbers.
Ans: #include <stdio.h>

// Recursive function to return gcd of a and b


int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}

// Driver program to test above function


int main()
{
int a = 98, b = 56;
printf("GCD of %d and %d is %d ", a, b, gcd(a, b));
return 0;
}
Output:

GCD of 98 and 56 is 14
. Write a C program to print the alphabets from A to Z using for loop
Ans: #include <stdio.h>
int main() {
char c;
for (c = 'A'; c <= 'Z'; ++c)
printf("%c ", c);
return 0;
}
Output

ABCDEFGHIJKLMNOPQRSTUVWXYZ
. What is the purpose of ‘for’ loop in C. Demonstrate with an example
program
Ans: The for loop in C language is used to iterate the statements or a part of
the program several times. It is frequently used to traverse the data structures
like the array and linked list.
The syntax of for loop in c language is given below:

for(initialization; condition; increment){


//code to be executed
}
#include<stdio.h>
int main(){
int i=1,number=0;
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i<=10;i++){
printf("%d ",(number*i));
}
return 0;
}
Output
Enter a number: 2
2 4 6 8 10 12 14 16 18 20
. Write a c program to print the following pattern using nested for loop
Ans: #include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
for (j = 1; j <= i; ++j) {
printf("%d ", j);
}
printf("\n");
}
return 0;
}

You might also like