Janitha Dilsham SC/2022/12911
1. Control structures allows programmers to control the flow
of their code, making it more efficient and effective.
i. Make choices:- We can tell the program to do
something if a condition is true, or something else if it
is not true.
ii. Repeat Action:- We can ask to do something over and
over again until a condition false.
2. 1)Sequential Control Structure
Ex:- Basic calculator
Start
Get Number 01
Get Number 02
Total=Number1 + Number2
Display Total
Stop
Janitha Dilsham SC/2022/12911
2) Selection Structure (if-else)
Flowchart:-
Start
If Condition True
Body of if
False
Stop
3) Repetition Structure (loop)
Flowchart:-
Start
Initialisation Statement
Test Expression Body of for loop
True
False
Exit for loop
Statement following the loop
Stop
Janitha Dilsham SC/2022/12911
3. a) int main()
{ int i,j;
printf("Enter Two Integer: ");
scanf("%d %d",&i,&j);
if(i>=j){
printf("The Maximum Number is: %d ",i);
}else{
printf("The Maximum Number is: %d ",j);
}
return 0;
}
b)
int main()
{ int i,j;
printf("Enter Two Integer: ");
scanf("%d %d",&i,&j);
if(i>=j){
printf("The larger Number is: %d\n",i);
printf("The Smaller Number is: %d",j);
}else{
printf("The larger Number is: %d\n",j);
printf("The Smaller Number is: %d",i);
}
return 0;
}
Janitha Dilsham SC/2022/12911
C) int main()
{ const PI=3.14159;
int choice;
float side,base,height,radius,area;
printf("Select the shape to calculate area:\n");
printf("1. Square\n");
printf("2. Triangle\n");
printf("3. Circle\n");
printf("Enter your choice (1,2,3): ");
scanf("%d",&choice);
switch(choice){
case 1:
printf("Enter the side length of the square: ");
scanf("%f",&side);
area=side*side;
printf("Area of the square: %.2f\n",area);
break;
case 2:
printf("Enter the base length of the triangle: ");
scanf("%f",&base);
printf("Enter the height length of the triangle: ");
scanf("%f",&height);
area=0.5*base*height;
printf("Area of the triangle: %.2f",area);
break;
case 3:
printf("Enter the radius of the circle: ");
scanf("%f",&radius);
area=PI*radius*radius;
printf("Area of the circle: %.2f",area);
break;
default:
printf("Invalid choice!");
return 0;
}
Janitha Dilsham SC/2022/12911
d) int main()
{ int marks;
printf("Enter your marks: ");
scanf("%d",&marks);
if(marks>=75){
printf("Grade: A\n");
}else if(marks<75 && marks>=65){
printf("Grade: B\n");
}else if(marks<65 && marks>=55){
printf("Grade: C\n");
}else if(marks<55 && marks>=45){
printf("Grade: D\n");
}else if(marks<44){
printf("Grade: E\n");
}else{
printf("Invalid Marks!\n");
}
return 0;
}
e) int main()
{ char Grade;
printf("Enter Your Grade: ");
scanf("%s",&Grade);
switch(Grade){
case 'A':
printf("Excellent");
break;
case 'B':
printf("Very Good");
break;
case 'C':
printf("Good");
break;
case 'D':
printf("Work Hard");
break;
default:
printf("Invalid Grade!");
}
return 0;
}
Janitha Dilsham SC/2022/12911
f) int main()
{ char usename[20];
char password[20];
char correctUsername[]="admin";
char correctPassword[]="password123";
printf("Enter username: ");
scanf("%s",&usename);
printf("Enter password: ");
scanf("%s",&password);
switch(strcmp(usename,correctUsername)){
case 0:
switch(strcmp(password,correctPassword)){
case 0:
printf("Login successful");
break;
default:
printf("Invalid Password");
}
break;
default:
printf("Invalid Username");
break;
}
return 0;
}
Janitha Dilsham SC/2022/12911
g) int main()
{ int number[100];
int n,i=0;
int sum=0;
float average;
printf("Enter the number of element in the array: ");
scanf("%d",&n);
printf("Enter %d integers:\n",n);
while(i<n){
printf("Enter element %d: ",i+1);
scanf("%d",&number[i]);
sum+=number[i];
i++;
average=(float)sum/n;
printf("Sum of the entered integers: %d\n",sum);
printf("Average of the entered integers: %.2f\n",average);
return 0;
}
Janitha Dilsham SC/2022/12911
h) #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main()
{
int num = 2;
bool isPrime;
printf("Prime numbers between 0 and 1000:\n");
do {
isPrime = true;
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
printf("%d ", num);
}
num++;
} while (num <= 1000);
printf("\n");
return 0;
}
Janitha Dilsham SC/2022/12911
int main()
i) a) { int i,j,n=5,k;
for(i=0;i<=4;i++){
for(j=0;j<=n;j++){
printf(" ");
}
for(k=0;k<=5;k++){
printf("*");
}
n--;
printf("\n");
}
return 0;
}
b)
int main()
{ int i,j,n=1;
for(i=0;i<=4;i++){
for(j=1;j<=n;j++){
if(i==2 && j==2){
printf(" ");
continue;
}else if(i==3 && j==2){
printf(" ");
continue;
}else if(i==3 && j==3){
printf(" ");
continue;
}
printf("*");
}
n++;
printf("\n");
}
return 0;
}
Janitha Dilsham SC/2022/12911
int main()
c) { int i,j,n=5;
for(i=0;i<=4;i++){
for(j=1;j<=n;j++){
if(i==2 && j==2){
printf(" ");
continue;
}else if(i==1 && j==2){
printf(" ");
continue;
}else if(i==1 && j==3){
printf(" ");
continue;
}
printf("*");
}
n--;
printf("\n");
}
return 0;
}
Janitha Dilsham SC/2022/12911
J) void fibonacci_sequence(int num_loops) {
int i, first = 0, second = 1, next;
printf("Fibonacci sequence with %d loops:\n", num_loops);
for (i = 0; i < num_loops; i++) {
if (i <= 1)
next = i;
else {
next = first + second;
first = second;
second = next;
}
printf("%d ", next);
}
printf("\n");
}
int main()
{ int num_loops;
printf("Enter the number of loops for Fibonacci sequence: ");
scanf("%d", &num_loops);
if (num_loops <= 0) {
printf("Please enter a positive integer.\n");
return 1;
}
fibonacci_sequence(num_loops);
return 0;
}
Janitha Dilsham SC/2022/12911
K)
//function to calculate factorial
int factorial(int n){
if(n<=1)
return 1;
else
return n* factorial(n-1);
}
//function to calculate binomial coefficient
int binomial_coefficient(int n, int k){
return factorial(n) /
(factorial(k)*factorial(n-k));
}
int main()
{ int rows=11;
for(int i=0;i<rows;i++){
for(int j=0;j<rows-i-1;j++)
printf(" ");
for(int j=0;j<=i;j++)
printf("%6d",binomial_coefficient(i,j));
printf("\n");
}
return 0;
}
Janitha Dilsham SC/2022/12911
4. 1) Answer :- D. Complier error
2) Answer :- C. 6 6
3) Answer :- Negative number
4) Answer :- Continue cannot be used but break can be
used
5) Answer :- If-else
6) Answer :- Know Program
7) Answer :- Hello
5.
int main()
{ char ch;
printf("Enter a character: ");
scanf("%c",&ch);
ch=tolower(ch);
switch(ch){
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
printf("%c is a vowel.\n",ch);
break;
default:
if(ch>='a' && ch<='z'){
printf("%c is a consonant.\n",ch);
}else{
printf("%c is not a letter.\n",ch);
}
break;
}
return 0;
}
Janitha Dilsham SC/2022/12911
6.
int main()
{ int num1,num2,num3;
printf("Enter first number: ");
scanf("%d",&num1);
printf("Enter second number: ");
scanf("%d",&num2);
printf("Enter third number: ");
scanf("%d",&num3);
int largest=num1;
int smallest=num1;
if(num2>largest){
largest=num2;
}
if(num2<smallest){
smallest=num2;
}
if(num3>largest){
largest=num3;
}
if(num3<smallest){
smallest=num3;
}
printf("Largest number: %d\n",largest);
printf("Smallest number: %d\n",smallest);
return 0;
}
Janitha Dilsham SC/2022/12911
7.
int main()
{ char ch;
float F,K,C;
printf("Enter F for Fahrenheit or K for Kelvin: ");
scanf("%c",&ch);
printf("Enter Celsius value: ");
scanf("%f",&C);
switch(ch){
case 'F':
case 'f':
F = (C * 9/5) + 32;
printf("%.2f",F);
break;
case 'K':
case 'k':
K = C + 273.15;
printf("%.2f",K);
break;
default:
printf("Invalid");
}
return 0;
}
8. 1) Answer :- 1
2) Answer :- B. for (i = 0, j = 0; i < n, j < n; i++, j += 5)
3) Answer :- C. Compile error
4) Answer :- D. 3
5) Answer :- C. 4
6) Answer :- A. Know Program
7) Answer :- B. Know Program is printed infinite times.
8) Answer :- B. 5
Janitha Dilsham SC/2022/12911
int main()
9) { int i=0,n,sum;
int array[100];
printf("Enter how many digits in array: ");
scanf("%d",&n);
do{
printf("Enter %d Element: ",i+1);
scanf("%d",&array[i]);
i++;
}while(i<n);
sum=array[0]+array[n-1];
printf("Sum of the first and last digits: %d",sum);
return 0;
}
10) Output :- 2 3 4
11)
int main()
{ int i,n,sum=0;
float avg;
int array[100];
printf("Enter how many elements in array: ");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("Enter %d element: ",i+1);
scanf("%d",&array[i]);
sum+=array[i];
}
avg=sum/n;
printf("%f",avg);
return 0;
}
Janitha Dilsham SC/2022/12911
12) Display values:
City 1, Day 1=10
City 1, Day 2=20
City 1, Day 3=30
City 1, Day 4=40 Red Color :- User enterd
City 1, Day 5=50 values
City 1, Day 6=60
City 1, Day 7=70
City 2, Day 1=10
City 2, Day 2=20
City 2, Day 3=30
City 2, Day 4=40
City 2, Day 5=50
City 2, Day 6=60
City 2, Day 7=70
Janitha Dilsham SC/2022/12911
13) void inputMatrix(int mat[MAX_ROWS][MAX_COLS],int rows, int cols){
printf("Enter the element of the matrix:\n");
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
scanf("%d",&mat[i][j]);
}
}
}
void displayMatrix(int mat[MAX_ROWS][MAX_COLS],int rows, int cols){
printf("The matrix is:\n");
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
printf("%d\t",mat[i][j]);
}
printf("\n");
}
}
void addMatrix(int mat1[MAX_ROWS][MAX_COLS], int mat2[MAX_ROWS][MAX_COLS], int
result[MAX_ROWS][MAX_COLS], int rows, int cols){
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
result[i][j] = mat1[i][j] + mat2[i][j];
}
}
}
void subtractMatrix(int mat1[MAX_ROWS][MAX_COLS], int
mat2[MAX_ROWS][MAX_COLS], int result[MAX_ROWS][MAX_COLS], int rows, int cols){
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
result[i][j] = mat1[i][j] - mat2[i][j];
}
}
}
void multiplyMatrix(int mat1[MAX_ROWS][MAX_COLS], int
mat2[MAX_ROWS][MAX_COLS], int result[MAX_ROWS][MAX_COLS], int rows1, int cols1,
int cols2){
for(int i=0;i<rows1;i++){
for(int j=0;j<cols2;j++){
result[i][j] = 0;
for(int k=0;k<cols1;k++){
result[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
}
Janitha Dilsham SC/2022/12911
Int main(){
int mat1[MAX_ROWS][MAX_COLS], mat2[MAX_ROWS][MAX_COLS],
result[MAX_ROWS][MAX_COLS];
int rows1, cols1, rows2, cols2, choice;
printf("Enter the number of rows and columns of the first matrix: ");
scanf("%d %d", &rows1, &cols1);
printf("Enter the number of rows and columns of the second matrix: ");
scanf("%d %d", &rows2, &cols2);
if (rows1 != rows2 || cols1 != cols2) {
printf("Matrix addition and subtraction are not possible. Ensure the matrices
have the same dimensions.\n");
return 1;
}
printf("Enter elements for the first matrix:\n");
inputMatrix(mat1, rows1, cols1);
printf("Enter elements for the second matrix:\n");
inputMatrix(mat2, rows2, cols2);
printf("Choose the operation:\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
scanf("%d", &choice);
switch (choice) {
case 1:
addMatrix(mat1, mat2, result, rows1, cols1);
displayMatrix(result, rows1, cols1);
break;
case 2:
subtractMatrix(mat1, mat2, result, rows1, cols1);
displayMatrix(result, rows1, cols1);
break;
case 3:
if (cols1 != rows2) {
printf("Matrix multiplication is not possible. Number of columns in
the first matrix must be equal to the number of rows in the second matrix.\n");
return 1;
}
multiplyMatrix(mat1, mat2, result, rows1, cols1, cols2);
displayMatrix(result, rows1, cols2);
break;
default:
printf("Invalid choice\n");
return 1;
}
return 0;
}
Janitha Dilsham SC/2022/12911
14)
Int main(){
int mat[3][4];
printf("Enter 12 integers for 3*4 matrix:\n");
for(int i=0;i<3;i++){
for(int j=0;j<4;j++){
scanf("%d",&mat[i][j]);
}
}
printf("Display Matrix\n");
for(int i=0;i<3;i++){
for(int j=0;j<4;j++){
printf("%d\t",mat[i][j]);
}
printf("\n");
}
Return 0;
}
15)
Int main(){
char name[100];
printf("Enter your name: ");
fgets(name,sizeof(name),stdin);
name[strcspn(name, "\n")] = 0;//Remove new line
//String Copy
char copiedName[100];
strcpy(copiedName,name);
printf("Copied Name: %s\n",copiedName);
//String concatenation
char additionalName[100];
printf("Enter additional name to concatenate: ");
fgets(additionalName,sizeof(additionalName),stdin);
strcat(name,additionalName);
additionalName[strcspn(additionalName, "\n")] = 0; //Remove new line
printf("Concatenated Name: %s\n", name);
// Length of the name
int length = strlen(name);
printf("Length of the name: %d\n", length);
Return 0;
}
Janitha Dilsham SC/2022/12911
16) 1. Answer :- C. Depend on the variable which it is
pointing
2. Answer :-
Both declared and initialized
3. Answer :-
D. 23
4. Answer :-
B. False
5. Answer :-
B. k is a pointer to a pointer to a pointer
to a pointer to a char
6. Answer :- A. 30
7. Answer :- sizeof arri[] = 12sizeof ptri = 8sizeof
arrc[] = 3sizeof ptrc = 8