0% found this document useful (0 votes)
24 views

Programming in C Language Assignment1

Ram has n n trophies that he wishes to place in his main cabinet, which has space only for two trophies. If the number of trophies is increased by 3, then the number of possible ways to arrange the trophies in the main cabinet becomes 5 times the number of ways to arrange n n trophies. How many trophies does Ram have?

Uploaded by

Dharmik Dasara
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Programming in C Language Assignment1

Ram has n n trophies that he wishes to place in his main cabinet, which has space only for two trophies. If the number of trophies is increased by 3, then the number of possible ways to arrange the trophies in the main cabinet becomes 5 times the number of ways to arrange n n trophies. How many trophies does Ram have?

Uploaded by

Dharmik Dasara
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Programming in C Language

Project File

Submitted to: - Submitted by:-


Sandeep Pratap Singh Dharmik Dasara
40001545 590014163
Batch 30
ASSIGNMENT-1 C PROGRAMMING
Btech- 1st semester
1.Question:
1. WAP to count the number of characters in the message “Hello how are
you doing ?”.

Code:
#include <stdio.h>
int main() {
char text[] = "Hello how are you doing ?";
int length;
length = strlen(text);
printf("The number of characters in the message is: %d\n", length);

return 0;
}

Output:
The number of characters in the message is: 25
2.Question:
WAP to count the number of inputs entered by the user.

Code:
#include <stdio.h>
int main() {
int input, count = 0;

printf("Enter numbers (Enter -1 to stop): \n");

while(1) {
scanf("%d", &input);
if(input == -1) {
break;
}
count++;
}

printf("You entered %d inputs.\n", count);

return 0;
}

Output:
Enter numbers (Enter -1 to stop):
2
3
4
5
9
75

3
3
-1
You entered 8 inputs.

3Q. What is the difference between #include<stdio.h> and


#include”stdio.h”.

Ans:
<stdio.h> searches in standard C library locations, whereas "stdio.h" searches in
the current directory as well.

4Q. WAP to add two numbers under 50 using characters


datatype.

Code:
#include <stdio.h>
int main() {
char num1, num2, sum;
printf("Enter first number (under 50): ");
scanf("%hhd", &num1);
printf("Enter second number (under 50): ");
scanf("%hhd", &num2);

if(num1 >= 50 || num2 >= 50) {


printf("Both numbers must be under 50.\n");
} else {
sum = num1 + num2;
printf("The sum of %d and %d is: %d\n", num1, num2, sum);
}
return 0;
}

Output:
Enter first number (under 50): 38
Enter second number (under 50): 18
The sum of 38 and 18 is: 56

5Q. WAP to swap two numbers without using third variable


(Using bitwise XOR)
Code:
#include <stdio.h>

int main() {
int num1, num2;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);
num1 = num1 ^ num2;
num2 = num1 ^ num2;
num1 = num1 ^ num2;
printf("After swapping: num1 = %d, num2 = %d\n", num1, num2);
return 0;
}

6.Question:
WAP to check whether the number entered by user is even or
odd (using bitwise AND).

Code:

#include <stdio.h>

int main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);

if (num & 1) {

printf("%d is an odd number.\n", num);

} else {
printf("%d is an even number.\n", num);

return 0;

Output:

Enter a number: 33

33 is an odd number.

Enter a number: 22

22 is an even number.

7.Question: .WAP to check whether the number entered by


user is even or odd (using bitwise AND and ternary
operator).

Code:
#include <stdio.h>

int main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);
(num & 1) ? printf("%d is an odd number.\n", num) : printf("%d is an even
number.\n", num);

return 0;

Output:
Enter a number: 43

43 is an odd number.

9Q:

9a. 12, 9b. 012, 9f. 12, 9g. 123.46, 9h. 123.256700

10Q:
a: statement inside if executes : because 100 is +ve.

b: statement inside if executes : because total is +ve.

c: If a is less than 400 then the statement inside if executes.

d: statement inside if executes.

e: Does not execute .

11Q.

a. else cannot be used without if statement if is used it shows a logical error.


b. if termination of if statement occurs then logical errors occur and the code
gets compromised.
12Q:

a. Printf(“%d”, 5 && 0);

a. The logical AND operator returns true (1) only if both operands are non-zero
(true). Since one of the operands (0) is false, the entire expression 5 && 0
evaluates to false, which is represented as 0.

b. Printf(“%d”, 5 || 0);
The logical OR operator returns true (1) if at least one of the operands is non-
zero (true).
c. Printf(“%d”, !0);
Since !0 evaluates to 1 (true), the expression evaluates to 1.
d.0 && printf("Hello");
Nothing is printed.
e. Printf(“%d”, 5|6);
Nothing is printed
f. printf("%d", 5 & 6);
output:4
5 in binary is 0101
6 in binary is 0110
Performing 5 & 6 gives 0100 (which is 4 in decimal).

g. printf("%d", 5 ^ 6);
output:3
5 in binary is 0101
6 in binary is 0110
Performing 5 ^ 6 gives 0011 (which is 3 in decimal).
h. printf("%d", 5 | 6);
output:7
5 in binary is 0101
6 in binary is 0110
Performing 5 | 6 gives 0111 (which is 7 in decimal).
i. printf("%d", 5 << 1);
output:10
5 in binary is 0101.
Shifting left by 1 bit gives 1010 (which is 10 in decimal)
j. printf("%d", 5 >> 1);
output:2
5 in binary is 0101.
Shifting right by 1 bit gives 0010 (which is 2 in decimal).

17Q: WAP to check whether the number entered by the user is


absolute power of 2 or not.

CODE:
#include <stdio.h>

int main() {

int number;

printf("Enter a number: ");

scanf("%d", &number);
if (number < 1) {

printf("%d is not an absolute power of 2.\n", number);

return 0;

while (number > 1) {

if (number % 2 != 0) {

printf("%d is not an absolute power of 2.\n", number);

return 0;

number /= 2;

printf("The number is an absolute power of 2.\n");

return 0;

Output:
Enter a number: 32

The number is an absolute power of 2

Enter a number: 31

31 is not an absolute power of 2.

18Q. WAP to check the kth bit in the number entered by the
user is either 0 or 1.
CODE:
#include <stdio.h>

int main() {

int number, k;

printf("Enter a number: ");

scanf("%d", &number);

printf("Enter the bit position to check (0-based index): ");

scanf("%d", &k);

int bit = (number >> k) & 1;

if (bit) {

printf("The %d-th bit is 1.\n", k);

} else {

printf("The %d-th bit is 0.\n", k);

return 0;

OUTPUT:
Enter a number: 33

Enter the bit position to check (0-based index): 1

The 1-th bit is 0.


19Q. Write about a comparative study of Evolution of
Programming languages .
Ans:
Early Languages (1940s-1950s)
• Machine Language: The earliest form of programming, consisting of binary code
specific to a computer's architecture.
• Assembly Language: Introduced mnemonics for machine instructions, allowing
easier programming than pure binary, but still closely tied to hardware.
2. High-Level Languages (1950s-1960s)
• FORTRAN (1957): One of the first high-level languages, designed for scientific and
engineering applications. It introduced concepts like loops and conditionals.
• COBOL (1959): Aimed at business data processing, emphasizing readability and
structured data handling.
• LISP (1958): Developed for artificial intelligence, introducing concepts like recursion
and list processing.
3. Structured Programming (1960s-1970s)
• ALGOL (1958): Influenced many languages with its structured programming
concepts, including block structures and scope.
• Pascal (1970): Designed for teaching programming, emphasizing structured
programming and data structuring.
• C (1972): Brought low-level access while still being a high-level language, widely
used for system programming.
4. Object-Oriented Programming (1980s)
• C++ (1985): An extension of C that introduced object-oriented concepts, promoting
code reuse and modularity.
• Smalltalk (1972): Pioneered the concept of objects and message passing, influencing
many future languages.
5. Scripting and Interpreted Languages (1990s)
• Python (1991): Emphasized readability and simplicity, becoming popular for web
development, automation, and data analysis.
• JavaScript (1995): Created for client-side web development, it became essential for
interactive web applications.
6. Modern Programming Paradigms (2000s-Present)
• Java (1995): Introduced platform independence with the "write once, run anywhere"
capability. It emphasized strong typing and garbage collection.
• Ruby (1995): Focused on simplicity and productivity, known for its elegant syntax
and use in web frameworks like Ruby on Rails.
• Swift (2014): Developed by Apple for iOS and macOS applications, combining
performance with safety features.
7. Trends and Future Directions
• Functional Programming: Languages like Haskell and Scala emphasize
immutability and first-class functions, gaining popularity in data processing and
concurrent programming.
• Domain-Specific Languages (DSLs): Tailored languages for specific applications,
such as SQL for databases and HTML/CSS for web development.
• Concurrency and Parallelism: Increasing focus on languages that support
concurrent programming models, like Go and Rust, to leverage modern multi-core
processors.
OVERALL,
The evolution of programming languages reflects the changing needs of
technology and software development. From low-level machine code to high-
level abstractions and modern paradigms, each generation of programming
languages has introduced new concepts, improving efficiency, readability, and
developer productivity. As technology continues to evolve, programming
languages will likely adapt, integrating new paradigms and addressing emerging
challenges in computing.
20Q. Give a brief description of various datatypes (basic as
well as derived).

Basic Data Types


1. int:
❖ Represents integer values.
❖ Size: Typically 4 bytes (depends on the system).
❖ Example: int a = 10;
2. float:
❖ Represents single-precision floating-point numbers.
❖ Size: Typically 4 bytes.
❖ Example: float b = 5.5;
3. double:
❖ Represents double-precision floating-point numbers.
❖ Size: Typically 8 bytes.
❖ Example: double c = 3.14159;
4. char:
❖ Represents a single character.
❖ Size: Typically 1 byte.
❖ Example: char d = 'A';
5. void:
❖ Represents the absence of type. Used for functions that do not
return a value.
❖ Example: void function();
Derived Data Types
1. Array:
o A collection of elements of the same type stored in contiguous
memory.
o Example: int arr[10];
2. Structure (struct):
o A user-defined data type that groups related variables of different
types.

Conclusion
These data types provide the foundation for data manipulation in C. Basic types
represent simple data, while derived types allow for more complex data
structures, enhancing code organization and functionality. Understanding these
types is essential for effective programming in C.

You might also like