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

LOVELY PROFESSIONAL UNIVERSITY

MITTAL SCHOOL OF BUSINESS

CONTINUOUS ASSESSMENT-1

Name: Ajay Krishna. Gogineni

Registration Number: 12001044

Course Code: CAP172 (Programming Methodologies)

Roll Number: RQE401A31


4.Design an algorithm to find the sum of first 50 numbers?

Ans:

This has an analytic solution so you have a constant time algorithm: S(n) = (n(n+1))/2 =>

S(50) = 25*51 if you start at 1 or 25*49 if you start at 0.

so your function would be:

int sumStartatOne ( int n) { return (n*(n+1)/2)); }

or

int sumStartatZero ( int n) { return (n*(n-1)/2)); }

or more generally

int sum (int numberofInts, int firstInt) { return ( numberofInts*firstInt +


(numberofInts*(numberofInt-1)/2)); }
12. Design and develop a C program to read a year as an input and find whether it is leap year?
Ans:

1. #include<stdio.h>
2. #include<conio.h>
3. void main() {
4. int year;
5. printf("Enter a year: ");
6. scanf("%d", &year);
7. if(((year%4==0) && ((year%400==0) || (year%100!==0))
8. {
9. printf("%d is a leap year", &year);
10. } else {
11. printf("%d is not a leap year", &year);
12. }
13. getch();
14. }

Output

Test 1:
Enter a year: 2004

2004 is a leap year

Test 2:

Enter a year: 1700

1700 is not a leap year


16. Difference between identifiers and keywords with the help of examples?

Ans:
In the world of programming languages, keywords and identifiers are important
terminologies. Some programming languages contain keywords and identifiers that
are recognized by its compiler. In general, keywords are the predefined and
specific reserved words, which hold special meaning. On the other hand, an
identifier is a different term or name given to a variable, label of class in the program
or function. Let’s find out the difference between keywords and identifiers.

What is a Keyword?

Keywords are the predefined and specific reserved words, which hold special meaning.
Keywords help in defining any statement in the program. When it comes to C language it
consists of 32 keywords. Some of the examples of keywords are: double, int, auto, char,
break, and more.

What is an Identifier?

A particular name generated by the programmer to define a variable, structure, class, or


function is called an identifier. It is important to suggest some understandable names to
the identifiers to get the code quickly readable.
Difference between Keyword and Identifier :

S. No. Keyword Identifier

1 Keywords are predefined and specific A particular name generated by the


reserved words, which hold special programmer to define a variable, structure,
meaning. Keywords help in defining any class, or function is called an identifier.
statement in the program.

2 A keyword begins with lowercase. In the identifier, the first character may begin
with uppercase, lowercase or underscores.

3 It defines the type of entity. It classifies the name of the entity.

4 It can only have alphabetical characters. It can have numbers, alphabetical characters,
and underscores.

5 It should be lowercase. It can be both upper and lowercase.

6 It helps in defining a particular property It helps in locating the name of the entity.
that subsists in a computer language.

7 double, int, auto, char, break, and more Test, count1, highspeed, etc are examples of
are examples of keywords. identifiers.

18. What is an operator? Explain the arithmetic, relational, logical, and assignment operators in
C language?

Ans:

An operator is a symbol that tells the compiler to perform specific mathematical or logical
functions. C language is rich in built-in operators and provides the following types of
operators −

Arithmetic Operators
Relational Operators
Logical Operators
Assignment Operators

• Arithmetic Operators

The following table shows all the arithmetic operators supported by the C language.
Assume variable A holds 10 and variable B holds 20 then −

Operator Description Example

+ Adds two operands. A + B = 30

− Subtracts second operand from the first. A − B = -10

* Multiplies both operands. A * B = 200

/ Divides numerator by de-numerator. B/A=2

% Modulus Operator and remainder of after an integer division. B%A=0

++ Increment operator increases the integer value by one. A++ = 11

-- Decrement operator decreases the integer value by one. A-- = 9

• Relational Operators

The following table shows all the relational operators supported by C. Assume
variable A holds 10 and variable B holds 20 then –
Operator Description Example

== Checks if the values of two operands are equal or not. If yes, (A == B) is not
true.
then the condition becomes true.

!= Checks if the values of two operands are equal or not. If the (A != B) is true.

values are not equal, then the condition becomes true.

> Checks if the value of left operand is greater than the value of (A > B) is not
true.
right operand. If yes, then the condition becomes true.

< Checks if the value of left operand is less than the value of (A < B) is true.

right operand. If yes, then the condition becomes true.

>= Checks if the value of left operand is greater than or equal to (A >= B) is not
true.
the value of right operand. If yes, then the condition becomes

true.

<= Checks if the value of left operand is less than or equal to the (A <= B) is true.

value of right operand. If yes, then the condition becomes true.


• Logical Operators

Following table shows all the logical operators supported by C language. Assume
variable A holds 1 and variable B holds 0, then –

Operator Description Example

&& Called Logical AND operator. If both the operands are non-zero, then the (A && B)
is false.
condition becomes true.

|| Called Logical OR Operator. If any of the two operands is non-zero, then (A || B) is


true.
the condition becomes true.

! Called Logical NOT Operator. It is used to reverse the logical state of its !(A &&
B) is
operand. If a condition is true, then Logical NOT operator will make it true.

false.

• Assignment Operators

The following table lists the assignment operators supported by the C language −

Operator Description Example


= Simple assignment operator. Assigns values from right side C=A+B
operands to left side operand will assign
the value
of A + B
to C

+= Add AND assignment operator. It adds the right operand to the left C += A is
operand and assign the result to the left operand. equivalent
to C = C +
A

-= Subtract AND assignment operator. It subtracts the right operand C -= A is


from the left operand and assigns the result to the left operand. equivalent
to C = C -
A

*= Multiply AND assignment operator. It multiplies the right operand with C *= A is


the left operand and assigns the result to the left operand. equivalent
to C = C *
A

/= Divide AND assignment operator. It divides the left operand with the C /= A is
right operand and assigns the result to the left operand. equivalent
to C = C /
A

%= Modulus AND assignment operator. It takes modulus using two C %= A is


operands and assigns the result to the left operand. equivalent
to C = C
%A

<<= Left shift AND assignment operator. C <<= 2


is same
as C = C
<< 2

>>= Right shift AND assignment operator. C >>= 2


is same
as C = C
>> 2
&= Bitwise AND assignment operator. C &= 2 is
same as
C=C&2

^= Bitwise exclusive OR and assignment operator. C ^= 2 is


same as
C=C^2

|= Bitwise inclusive OR and assignment operator. C |= 2 is


same as
C=C|2

6. Design a flowchart whether temperature is less than 0 or greater than 100?

Ans:
#include <stdio.h>
#include <stdlib.h>
int main(void){
printf("%d",test(120, -1));
printf("\n%d",test(-1, 120));
printf("\n%d",test(2, 120));
}
int test(int temp1, int temp2)
{
return temp1 < 0 && temp2 > 100 || temp2 < 0 && temp1 >
100;
}

Sample Output:
1
1
0

Flowchart:

You might also like