C language Programming


C language Programming

A. Answer the following questions:
1. What is C language?

- C Language  is a high-level, general-purpose programming language developed in the early 1970s by Dennis Ritchie at Bell Labs. It is widely used for system and application software, embedded systems, and for teaching computer programming.


2. Why is C language called a structured programming language?
- C language is called a structured programming language because it allows the use of functions, loops, conditionals, and blocks of code that can be nested and organized to create clear, efficient, and understandable programs.

3. Why is C language called a middle-level language?
- C language is called a middle-level language because it combines elements of both high-level languages (which are easier for humans to read and write) and low-level languages (which are closer to machine code and offer fine control over hardware).

4. Why is C language more popular than QBASIC?
- C language is more popular than QBASIC because it offers greater flexibility, efficiency, and performance. It is also more versatile, being used for a wide range of applications, from operating systems and compilers to games and business applications, whereas QBASIC is limited mainly to educational purposes and simple scripting.

5. List any two features of C language.
- 1) Portability: C programs can run on different machines with little or no modification.
   2) Rich Library: C has a rich set of built-in functions and operators.

6. Define Source code and Object code.
- Source code: The original code written by the programmer in a high-level programming language.
- Object code: The machine code or intermediate code generated after the source code is compiled by a compiler.

7. List two similarities of QBASIC and C language.
- 1) Both are procedural programming languages.
  2) Both can be used to write structured programs using loops, conditionals, and functions/subroutines.

8. List any two differences between QBASIC and C language.
- 1) C language has a more complex syntax and more powerful features compared to the simpler syntax and limited features of QBASIC.
2) C language supports low-level memory manipulation and pointer arithmetic, whereas QBASIC does not.

9. What is an identifier in C language? List the rules for naming the identifiers.
- An identifier in C language is a name used to identify variables, functions, arrays, etc.

Rules for naming identifiers include:
- Must begin with a letter (uppercase or lowercase) or an underscore (_).
- Can contain letters, digits (0-9), and underscores.
- Case-sensitive (e.g., `Var` and `var` are different identifiers).
- Cannot be a reserved keyword (e.g., `int`, `return`).

10. List the basic data types of C language.
The basic data types in C language are:
- `int` (integer)
- `float` (floating-point number)
- `double` (double-precision floating-point number)
- `char` (character)

11. What is a data type modifier? List them.
- A data type modifier alters the properties of the basic data types, such as their size or range. Common data type modifiers in C include:
- `short`
- `long`
- `signed`
- `unsigned`

12. What is an operator in C language? List any four operators used in C language.
- An operator in C language is a symbol that tells the compiler to perform specific mathematical or logical manipulations. Four operators used in C include:
- `+` (Addition)
- `-` (Subtraction)
- `*` (Multiplication)
- `/` (Division)

13. What is a unary operator in C language?
- A unary operator is an operator that operates on a single operand. Examples include the increment operator (`++`), the decrement operator (`--`), the unary plus (`+`), and the unary minus (`-`).

14. What is a logical operator? List its operators.
- Logical operators are used to perform logical operations, typically used in conditional statements. The logical operators in C are:
- `&&` (Logical AND)
- `||` (Logical OR)
- `!` (Logical NOT)

15. What is an assignment operator? List its operators.
- An assignment operator is used to assign a value to a variable. The basic assignment operator is `=`, but there are also compound assignment operators which combine arithmetic operations with assignment. The assignment operators in C include:
- `=` (Assignment)
- `+=` (Add and assign)
- `-=` (Subtract and assign)
- `*=` (Multiply and assign)
- `/=` (Divide and assign)
- `%=` (Modulus and assign)

16. What is a header file? List any two header files.
- A header file in C contains declarations of functions and macros to be shared between several source files. Examples include:
- `stdio.h` (Standard Input Output header)
- `stdlib.h` (Standard Library header)

17. Write the uses of `printf()` and `scanf()` function.
- `printf()`: Used to output (print) formatted text to the standard output (usually the screen).
- `scanf()`: Used to read formatted input from the standard input (usually the keyboard).

18. What is escape character in C-language?
- An escape character is a backslash (`\`) followed by a character that has a special meaning. It allows you to include special characters in strings, such as newline (`\n`), tab (`\t`), backslash (`\\`), and double quote (`\"`).

B. Write a program in C language to add 15 and 27.
#include <stdio.h>
int main()
{
int num1 = 15;
int num2 = 27;
int sum = num1 + num2;
printf("The sum of %d and %d is %d\n", num1, num2, sum);
return 0;
}

C. Program to Add Any Two Numbers Supplied by a User
#include <stdio.h>
int main()

{
int num1, num2, sum;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
sum = num1 + num2;
printf("The sum of %d and %d is %d\n", num1, num2, sum);
return 0;
}

D. Program to Display Area of a Rectangle
#include <stdio.h>
int main()

{
float length, width, area;
printf("Enter length of the rectangle: ");
scanf("%f", &length);
printf("Enter width of the rectangle: ");
scanf("%f", &width);
area = length * width;
printf("The area of the rectangle is %.2f\n", area);
return 0;
}

 

E. Program to Display Perimeter of a Rectangle
#include <stdio.h>
int main()

{
float length, width, perimeter;
printf("Enter length of the rectangle: ");
scanf("%f", &length);
printf("Enter width of the rectangle: ");
scanf("%f", &width);
perimeter = 2 * (length + width);
printf("The perimeter of the rectangle is %.2f\n", perimeter);
return 0;
}

F. Program to Display Area of Four Walls of a Room
#include <stdio.h>
int main()

{
float length, width, height, area;
printf("Enter length of the room: ");
scanf("%f", &length);
printf("Enter width of the room: ");
scanf("%f", &width);
printf("Enter height of the room: ");
scanf("%f", &height);
area = 2 * height * (length + width);
printf("The area of the four walls of the room is %.2f\n", area);
return 0;
}

G. Program to Display Circumference of a Circle
#include <stdio.h>
#define PI 3.14159
int main()

{
float radius, circumference;
printf("Enter radius of the circle: ");
scanf("%f", &radius);
circumference = 2 * PI * radius;
printf("The circumference of the circle is %.2f\n", circumference);
return 0;
}

H. Program to Display Volume of a Cylinder
#include <stdio.h>
#define PI 3.14159
int main()

{
float radius, height, volume;
printf("Enter radius of the cylinder: ");
scanf("%f", &radius);
printf("Enter height of the cylinder: ");
scanf("%f", &height);

volume = PI * radius * radius * height;
printf("The volume of the cylinder is %.2f\n", volume);
return 0;
}

I. Program to Calculate and Display Profit or Loss
#include <stdio.h>
int main()

{
float costPrice, sellingPrice, profitOrLoss;
printf("Enter cost price: ");
scanf("%f", &costPrice);
printf("Enter selling price: ");
scanf("%f", &sellingPrice);

profitOrLoss = sellingPrice - costPrice;
if (profitOrLoss > 0) {
printf("Profit: %.2f\n", profitOrLoss);
} else if (profitOrLoss < 0) {
printf("Loss: %.2f\n", -profitOrLoss);
} else {
printf("No profit, no loss.\n");
}
return 0;
}

 

J. Program to Display Whether a Number is Positive, Negative or Zero
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);

if (num > 0) {
printf("The number is positive.\n");
} else if (num < 0) {
printf("The number is negative.\n");
} else {
printf("The number is zero.\n");
}
return 0;
}

K. Program to Display the Greater Number Among Any Three Numbers Supplied by the User
#include <stdio.h>
int main()

{
int num1, num2, num3;
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);

if (num1 >= num2 && num1 >= num3) {
printf("The greatest number is %d\n", num1);
} else if (num2 >= num1 && num2 >= num3) {
printf("The greatest number is %d\n", num2);
} else {
printf("The greatest number is %d\n", num3);
}
return 0;
}

L. Program to Display the Smaller Number Among Any Three Numbers Supplied by the User
#include <stdio.h>
int main()

{
int num1, num2, num3;
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);

if (num1 <= num2 && num1 <= num3) {
printf("The smallest number is %d\n", num1);
} else if (num2 <= num1 && num2 <= num3) {
printf("The smallest number is %d\n", num2);
} else {
printf("The smallest number is %d\n", num3);
}
return 0;
}

M. Program to Display the Middle Number Among Any Three Numbers Supplied by the User
#include <stdio.h>
int main() {
int num1, num2, num3;
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);

if ((num1 >= num2 && num1 <= num3) || (num1 <= num2 && num1 >= num3)) {
printf("The middle number is %d\n", num1);
} else if ((num2 >= num1 && num2 <= num3) || (num2 <= num1 && num2 >= num3)) {
printf("The middle number is %d\n", num2);
} else {
printf("The middle number is %d\n", num3);
}
return 0;
}

N. Write the program using C language to generate the following numeric seriese:
1. Series: 1, 2, 3, 4, 5, ..., up to 20 terms
#include <stdio.h>
int main()

{
for (int i = 1; i <= 20; i++) {
printf("%d ", i);
}
printf("\n");
return 0;
}

2. Series: 100, 90, 80, 70, ………..,10
#include <stdio.h>
int main()

{
for (int i = 100; i >= 10; i -= 10) {
printf("%d ", i);
}
printf("\n");
return 0;
}

 

3. Series: 2, 4, 6, 8, ..., up to 15 terms
#include <stdio.h>

int main() {
for (int i = 1; i <= 15; i++) {
printf("%d ", 2 * i);
}
printf("\n");
return 0;
}

 4. Series: 2, 4, 6, 8, ………..22
#include <stdio.h>
int main() {
int series[] = {2, 4, 6, 8, 22};
int length = sizeof(series) / sizeof(series[0]);

for (int i = 0; i < length; i++) {
printf("%d ", series[i]);
}
printf("\n");
return 0;
}


 5. Series: 23, 21, 19,………… 5
#include <stdio.h>
int main() {
int series[] = {23, 21, 19, 5};
int length = sizeof(series) / sizeof(series[0]);

for (int i = 0; i < length; i++) {
printf("%d ", series[i]);
}
printf("\n");
return 0;
}


 6. Series: 2, 2, 4, 6, 10, ..., up to 12 terms
#include <stdio.h>
int main() {
int a = 2, b = 2;
printf("%d %d ", a, b);

for (int i = 3; i <= 12; i++) {
int next = a + b;
printf("%d ", next);
a = b;
b = next;
}
printf("\n");
return 0;
}

 7. Series: 2, 3, 5, 8, 13, ..., up to 12 terms
#include <stdio.h>
int main() {
int a = 2, b = 3;
printf("%d %d ", a, b);

for (int i = 3; i <= 12; i++) {
int next = a + b;
printf("%d ", next);
a = b;
b = next;
}
printf("\n");
return 0;
}

 8. Series: 2, 3, 5, 8, 12, ..., up to 12 terms
#include <stdio.h>
int main() {
int a = 2, b = 3;
printf("%d %d ", a, b);

for (int i = 3; i <= 12; i++) {
int next = a + b + 1;
printf("%d ", next);
a = b;
b = next;
}
printf("\n");
return 0;
}

O. WAP in C language to display 'computer' Ten Times
#include <stdio.h>
int main() {
for (int i = 0; i < 10; i++) {
printf("computer\n");
}
return 0;
}
P. WAP in C language to display Sum of First 10 Counting Numbers
#include <stdio.h>
int main() {
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i;
}
printf("The sum of the first 10 counting numbers is %d\n", sum);
return 0;
}

Q. WAP in C language to display Sum of Any Ten Numbers Input by a User
#include <stdio.h>
int main() {
int numbers[10], sum = 0;
printf("Enter 10 numbers:\n");
for (int i = 0; i < 10; i++) {
scanf("%d", &numbers[i]);
sum += numbers[i];
}
printf("The sum of the entered numbers is %d\n", sum);
return 0;
}

R. WAP in C language to display Sum of Odd Numbers from 3 to 21
#include <stdio.h>
int main() {
int sum = 0;
for (int i = 3; i <= 21; i += 2) {
sum += i;
}
printf("The sum of odd numbers from 3 to 21 is %d\n", sum);
return 0;
}


 
S. WAP in C language to to Count and Display Total Number of Factors of a Number
#include <stdio.h>
int main() {
int num, count = 0;
printf("Enter a number: ");
scanf("%d", &num);

for (int i = 1; i <= num; i++) {
if (num % i == 0) {
count++;
}
}
printf("The total number of factors of %d is %d\n", num, count);
return 0;
}

T. WAP in C language to display Factors of a Number
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);

printf("The factors of %d are: ", num);
for (int i = 1; i <= num; i++) {
if (num % i == 0) {
printf("%d ", i);
}
}
printf("\n");
return 0;
}


U. WAP in C language to Check and Display Whether a Number is Prime or Composite
#include <stdio.h>
int main() {
int num, i, isPrime = 1;
printf("Enter a number: ");
scanf("%d", &num);

if (num <= 1) {
isPrime = 0;
} else {
for (i = 2; i <= num / 2; i++) {
if (num % i == 0) {
isPrime = 0;
break;
}
}
}

if (isPrime) {
printf("%d is a prime number.\n", num);
} else {
printf("%d is a composite number.\n", num);
}
return 0;
}

V. WAP in C language to display Sum of Digits of a Number
#include <stdio.h>
int main() {
int num, sum = 0, digit;
printf("Enter a number: ");
scanf("%d", &num);

while (num > 0) {
digit = num % 10;
sum += digit;
num /= 10;
}
printf("The sum of the digits is %d\n", sum);
return 0;
}

W.  WAP in C language to display Reverse of a Number
#include <stdio.h>
int main() {
int num, reverse = 0, digit;
printf("Enter a number: ");
scanf("%d", &num);

while (num > 0) {
digit = num % 10;
reverse = reverse * 10 + digit;
num /= 10;
}
printf("The reverse of the number is %d\n", reverse);
return 0;
}

Post a Comment (0)
Previous Post Next Post

ADS 1

ADS 2