0% found this document useful (0 votes)
14 views9 pages

Grade11 C Program Computer Lab Practice File

The document contains a series of C programming exercises designed for Grade 11 Computer Science students. Each exercise includes a brief description and the corresponding C code to perform tasks such as printing messages, performing arithmetic operations, finding factorials, and working with arrays and matrices. The exercises aim to enhance students' understanding of basic programming concepts and syntax in C.

Uploaded by

neupanedns
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views9 pages

Grade11 C Program Computer Lab Practice File

The document contains a series of C programming exercises designed for Grade 11 Computer Science students. Each exercise includes a brief description and the corresponding C code to perform tasks such as printing messages, performing arithmetic operations, finding factorials, and working with arrays and matrices. The exercises aim to enhance students' understanding of basic programming concepts and syntax in C.

Uploaded by

neupanedns
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

GRADE 11COMPUTER SCIENCE LAB PRACTICE C PROGRAMMING

1. Write a program to print Hello World.

#include <stdio.h>

int main() {
printf("Hello, World!\n");
return 0;
}

2. Write a program to print your Name.


#include <stdio.h>
int main() {
printf("My name is Rajesh parajuli\n");
return 0;
}

3. Write a c program to difference the direct value declare and user input value.
#include <stdio.h>
int main() {
// Direct declaration (values assigned directly)
int a = 10;
printf("the value of a is %d\n", a);

// User input (values entered by user)


int b;
printf("Enter two numbers: ");
scanf("%d ", &b);
printf("User entered b value is %d\n", b);

return 0;
}

4. Write a c program to add to numbers.


#include <stdio.h>
int main() {
int num1, num2, add;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
add = num1 + num2;
printf("Sum is %d", add);
return 0;
}
5. Write a C program to perform all arithmetic operations (+,-,*,/,%)
#include <stdio.h>

int main() {
int num1, num2;
int addition, subtraction, multiplication, division, remainder;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
addition = num1 + num2;
subtraction = num1 - num2;
multiplication = num1 * num2;
division = num1 / num2;
remainder = num1 % num2;

printf("Addition is %d\n", addition);


printf("Subtraction is %d\n", subtraction);
printf("Multiplication is %d\n", multiplication);
printf("Division is %d\n", division);
printf("Remainder is %d\n", remainder);
return 0;
}

6. Write a c program to program to find the square of a number.

#include <stdio.h>
int main() {
int number, square;
printf("Enter a number: ");
scanf("%d", &number);
square = number * number;
printf("Square of %d is %d\n", number, square);

return 0;
}

7. Write a program to find the area of a rectangle.

#include <stdio.h>

int main() {
int length, width, area;
printf("Enter length of the rectangle: ");
scanf("%d", &length);
printf("Enter width of the rectangle: ");
scanf("%d", &width);
area = length * width;
printf("Area of the rectangle = %d\n", area);

return 0;
}

8. Write a c program to calculate the simple interest.


#include <stdio.h>
int main() {
float principal, rate, time, simpleInterest;
printf("Enter principal amount: ");
scanf("%f", &principal);

printf("Enter rate of interest (in %%): ");


scanf("%f", &rate);

printf("Enter time (in years): ");


scanf("%f", &time);

simpleInterest = (principal * rate * time) / 100;

printf("Simple Interest = %.2f\n", simpleInterest);

return 0;
}

9. Write a program to find the largest of two numbers.


#include <stdio.h>
int main() {
int num1, num2, largest;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
if(num1 > num2) {
largest = num1;
} else {
largest = num2;
}
printf("The largest number is %d\n", largest);
return 0;
}
10. Write a program to check whether a number is even or odd.

#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0) {
printf("%d is Even\n", num);
} else {
printf("%d is Odd\n", num);
}
return 0;
}

11. Write a C program to find factorial of a number.


#include <stdio.h>

int main() {
int num, i;
int factorial = 1;
printf("Enter a number: ");
scanf("%d", &num);

for(i = 1; i <= num; i++) {


factorial = factorial * i;
}
printf("Factorial is %d\n", factorial);
return 0;
}

12. Write a program to check whether a number is prime.


#include <stdio.h>
int main() {
int num, i;
printf("Enter a number: ");
scanf("%d", &num);

for(i = 2; i < num; i++) {

if(num % i == 0) {
printf("%d is not a Prime number\n", num);
return 0;
}

if(num > 1)
printf("%d is a Prime number\n", num);
else
printf("%d is not a Prime number\n", num);
return 0;
}

13. Write a program to find sum of first n natural numbers.

#include <stdio.h>
int main() {
int n, sum = 0, i;
printf("Enter a number: ");
scanf("%d", &n);
for(i = 1; i <= n; i++) {
sum = sum + i; }
printf("Sum of first natural numbers is %d\n", sum);
return 0;
}

14. Write a program to swap two numbers.

#include <stdio.h>
int main() {
int a, b, temp;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("Before swapping: a = %d, b = %d\n", a, b);
temp = a;
a = b;
b = temp;
printf("After swapping: a = %d, b = %d\n", a, b);
return 0;
}

15. Write a program to print multiplication table.


#include <stdio.h>
int main() {
int num, i;
printf("Enter a number: ");
scanf("%d", &num);
printf("Multiplication table of %d:\n", num);
for(i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", num, i, num * i);
}
return 0;
}

16. Write a c program to count numbers from 1 to 10 using for, while, and do-while.

For LOOP

#include <stdio.h>
int main() {
int i;
printf("Counting using for loop:\n");
for(i = 1; i <= 10; i++) {
printf("%d ", i);
}
printf("\n");
return 0;
}

While LOOP

#include <stdio.h>
int main() {
int i = 1;
printf("Counting using while loop:\n");
while(i <= 10) {
printf("%d ", i);
i++;
}
printf("\n");
return 0;
}

Do-While LOOP

#include <stdio.h>
int main() {
int i = 1;
printf("Counting using do-while loop:\n");
do {
printf("%d ", i);
i++;
} while(i <= 10);
printf("\n");
return 0;
}

17. Write a c program to use switch case.

#include <stdio.h>
int main() {
int day;
scanf("%d", &day);

switch(day) {
case 1: printf("Sunday\n");
break;
case 2: printf("Monday\n");
break;
default: printf("Other day\n"); break;
}
return 0;
}

18. Write a program to store and print elements of an array.

#include <stdio.h>
int main() {
int arr[5];
int i;
printf("Enter 5 numbers:\n");
for(i = 0; i < 5; i++) {
scanf("%d", &arr[i]);
}
printf("The elements of the array are:\n");
for(i = 0; i < 5; i++) {
printf("%d\n ", arr[i]);
}
return 0;
}

19. Write a program to calculate the sum of array elements

#include <stdio.h>
int main() {
int arr[] = {10, 25, 7, 89, 54};
int total = 0;

for(int i = 0; i < 5; i++) {


total += arr[i];
}
printf("total elements sum is %d\n", total);
return 0;
}

20. Write a c program to add two 2×2 matrices

#include <stdio.h>
int main() {
int matrix1[2][2], matrix2[2][2], sum[2][2];
int i, j;

// Input elements of first matrix


printf("Enter elements of first 2x2 matrix:\n");
for(i = 0; i < 2; i++) {
for(j = 0; j < 2; j++) {
scanf("%d", &matrix1[i][j]);
}
}

// Input elements of second matrix


printf("Enter elements of second 2x2 matrix:\n");
for(i = 0; i < 2; i++) {
for(j = 0; j < 2; j++) {
scanf("%d", &matrix2[i][j]);
}
}

// Add the two matrices


for(i = 0; i < 2; i++) {
for(j = 0; j < 2; j++) {
sum[i][j] = matrix1[i][j] + matrix2[i][j];
}
}

// Display the sum


printf("Sum of the two matrices:\n");
for(i = 0; i < 2; i++) {
for(j = 0; j < 2; j++) {
printf("%d\t", sum[i][j]);
}
printf("\n");
}
return 0;
}

You might also like