***************************************************************************
Course Course Course L-T-P Credits
Code Name Category
20CS1181 Problem Solving and Programming
ESC 0-0-3 1.5
ThroughC Lab
Course Learning Objective:
1. Identify situations where computational methods and computers would be useful.
2. Given a computational problem, identify and abstract the programming task involved.
3. Approach the programming tasks using techniques learned and write pseudo-code.
4. To understand the concepts of Programming language
5. To learn the basics of C declarations, operators and expressions
6. To learn on the manipulation of strings, functions and pointers
7. To apply concepts and techniques for implementation
List of Programming Assignments for
Laboratory:Statements, Expressions & Conditionals
1. Write a program to print the memory allocation required for all the datatype in C Language.
2. Write a program to check whether the given number is even number or odd number.
3. Write a menu based program to take of input of two values followed input of
choice andaccordingly perform arithmetic operations like Addition, Subtraction,
Multiplication, Modulus, Division, Power( Using Switch Statement)
4. Write a program to swap two given numbers with and without using extra variable.
5. Write a program to find out the whether the given number is a perfect square or not.
6. Write a program to find out whether the given number is positive, negative or zero value.
Iterative Constructs - I: For Loop, While Loop &Do. While
1. Write a program print all the factors of a given number
2. Write a program to find the factorial of a given number
3. Write a program to find whether a given number is Palindrome or not.
4. Write a program to find whether a given number is Prime or not.
5. Write a program to print the Fibonacci series upto given ‘n’ number of terms.
Iterative Constructs – II: Nested Loops
1. Write a program to print the first ‘n’ prime numbers and prime numbers upto ‘n’ value.
2. Write a program to print the Pascal Triangle for given ‘n’ value
3. Write a program to print the first ‘n’ perfect number for a given ‘n’ value.
4. Write a program to print the following pattern for given ‘n’ value.
5. For Eg. If n = 3, the output would be
*
* * *
* * * * *
* * *
*
6. Write a program to print the following pattern for given ‘n’
valueFor Eg. If n = 4, the output would be
2
3 5
7 11 13
17 19 23 29
Single Dimensional Arrays: Basic Operations and Problems
1. Write a program to take an input array of ‘n’ numbers and find out the sum
of all theelements, product of all the elements and mean of the array.
2. Write a program to take an input array of ‘n’ numbers and print the second
smallest andsecond largest element of all elements in the array.
Two Dimensional Arrays –Matrices& its operations
1. Write a program to find the addition and subtraction for the given two matrices of
sizes ‘M xN’ and ‘P x Q’ respectively
2. Write a program to find the multiplication of the given two matrices of sizes ‘M x N’
and ‘Px Q’ respectively.
3. Write a program to find transpose of a matrix.
Strings – Dealing with non-numerical data
1. Write a program to convert the Lower Case letters to Upper Case Letters and
Upper CaseLetters to Lower Case Letters in a given input string.
2. Write a program to the print out the number of vowels, consonants, and digits (0-9)
presentin the given input string.
3. Write a program to check whether the given input string is palindrome string or not
4. Write a program to sort the given string of characters.
Array of Strings
1. Write a program to find the strings starting with “c” and “a” for the given n input strings..
2. Write a program to print the words of given input string in reverse
order ForEg. If input string is “I am an Indian”, the output would be
“Indian an am I”
3. Write a program to arrange the given ‘n’ strings in Dictionary Order.
Functions
1. Write a program to implement the string operations like Length of String, String Copying,
String Concatenation, Conversion to Uppercase and String Comparison.( Define own
Function for each of the operation. Header file “string.h” is not allowed)
2. Write a C program to implement Multiplication and Division Operations without using
operators “*” and “\” respectively. Define function “mul” for multiplication and “div” for
integer division.
Recursion
Write a program to print the integers from 1 to N and then N to 1 for the given input
number‘N’ without using any loops.
2. Write a program to find the X power N(X using the user defined recursive
N)
function “pow(X,N)” without using any predefined function from the library.
3. Write a program to find the GCD of two numbers ‘a’ and ‘b’ by defining a
recursivefunction GCD(a,b).
Structure
s 1. Write a program to take the information of ‘n’ Students (REGID, Name, CGPA,
1. Address – Village, District, Phone NO) and print the topper among the n students.
2. Write a program to take the information of ‘n’ Students (REGID, Name, CGPA,
Address – Village, District, Phone NO) and print the students in the ascending order
of Regn ID.
3. Write a program to take the information of ‘n’ Students (REGID, Name, CGPA,
Address – Village, District, Phone NO)and print the list of Phone Number for the
students who are the above average of CGPA.
File Handling – Create, Read and Write operations on File
1. Write a program to print the number of lines and words in a given input file name.
2. Write a program to copy from the given file to another file.
3. Write a program to append one file at the end of another file.
4. Write a program to search for a word in a given text file.
Course outcomes
At the end of the course, the student will be able
CO 1 To formulate the algorithms for simple problems
CO 2 To translate the given algorithms to a working and correct program
CO 3 To identify and correct logical errors encountered at run time
CO 4 To write iterative as well as recursive programs
CO 5 To represent Data in arrays, strings, Structures and manipulate them through a
program
CO 6 To decompose a problem into functions and synthesize a complete program
CO 7 To be able to create, read and write to and from text files
Assessment Method
Assessment Experiment Report/Viva-Voce/ Quiz/MCQ/Lab Total
Tool s project
Weightage (%) 25% 15% 40%
End Semester Examination weightage (%) 60%
PROGRAMS
1. Write a program to print the memory allocation required for all the
datatype in C Language.
PROGRAM:
Whenever we declare a variable in C, compiler will allocate storage space in
the computer’s memory. For example, if we declare a variable as “int”, 32-bit
C compiler will allocate 4 bytes of memory space.
#include<stdio.h>
int main() {
int intType;
float floatType;
double doubleType;
char charType;
// sizeof evaluates the size of a variable
printf("Size of int: %zu bytes\n", sizeof(intType));
printf("Size of float: %zu bytes\n", sizeof(floatType));
printf("Size of double: %zu bytes\n", sizeof(doubleType));
printf("Size of char: %zu byte\n", sizeof(charType));
return 0;
}
Output
Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte
[Link] a program to check whether the given number is even number or odd number.
PROGRAM:
An even number is an integer that is exactly divisible by 2. For example: 0, 8, -24
An odd number is an integer that is not exactly divisible by 2. For example: 1, 7, -11, 15
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
// true if num is perfectly divisible by 2
if(num % 2 == 0)
printf("%d is even.", num);
else
printf("%d is odd.", num);
return 0;
}
Output
Enter an integer: -7
-7 is odd.
[Link] a menu based program to take of input of two values followed
input of choice andaccordingly perform arithmetic operations like
Addition, Subtraction, Multiplication, Modulus, Division, Power( Using
Switch Statement)
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int a,b,base,exp;
int result;
int op;
printf(" [Link]\n [Link]\n [Link]\n [Link]\n");
printf("Enter the values of a & b: ");
scanf("%d %d",&a,&b);
printf("Enter your Choice : ");
scanf("%d",&op);
printf (" Enter the base value from the user: ");
scanf (" %d", &base); // get base from user
printf (" Enter the power value for raising the power of base: ");
scanf (" %d", &exp); // get exponent from user
result = pow ( base, exp);
switch(op)
{
case 1 :
printf("Sum of %d and %d is : %d",a,b,a+b);
break;
case 2 :
printf("Difference of %d and %d is : %d",a,b,a-b);
break;
case 3 :
printf("Multiplication of %d and %d is : %d",a,b,a*b);
break;
case 4 :
printf("Division of Two Numbers is %d : ",a/b);
case 5 :
printf("Modulo of two numbers is %d:",a%b);
break;
case 6:
printf (" %d to the power of %d is = %d ", base, exp, result);
default :
printf(" Enter Your Correct Choice.");
break;
}
return 0;
}
OUTPUT:
[Link] a program to swap two given numbers with and without using extra variable.
PROGRAM:
Swap Numbers Using Temporary Variable
#include<stdio.h>
int main() {
double first, second, temp;
printf("Enter first number: ");
scanf("%lf", &first);
printf("Enter second number: ");
scanf("%lf", &second);
// value of first is assigned to temp
temp = first;
// value of second is assigned to first
first = second;
// value of temp (initial value of first) is assigned to second
second = temp;
// %.2lf displays number up to 2 decimal points
printf("\nAfter swapping, first number = %.2lf\n", first);
printf("After swapping, second number = %.2lf", second);
return 0;
}
Output
Enter first number: 1.20
Enter second number: 2.45
After swapping, first number = 2.45
After swapping, second number = 1.20
Swap Numbers Without Using Temporary Variables
#include <stdio.h>
int main() {
double a, b;
printf("Enter a: ");
scanf("%lf", &a);
printf("Enter b: ");
scanf("%lf", &b);
// swapping
// a = (initial_a - initial_b)
a = a - b;
// b = (initial_a - initial_b) + initial_b = initial_a
b = a + b;
// a = initial_a - (initial_a - initial_b) = initial_b
a = b - a;
// %.2lf displays numbers up to 2 decimal places
printf("After swapping, a = %.2lf\n", a);
printf("After swapping, b = %.2lf", b);
return 0;
}
Run Code
Output
Enter a: 10.25
Enter b: -12.5
After swapping, a = -12.50
After swapping, b = 10.25
5. Write a program to find out the whether the given number is a perfect square or not.
PROGRAM:
#include <stdio.h>
#include <math.h>
int main()
{
int num;
int iVar;
float fVar;
printf("Enter an integer number: ");
scanf("%d",&num);
fVar=sqrt((double)num);
iVar=fVar;
if(iVar==fVar)
printf("%d is a perfect square.",num);
else
printf("%d is not a perfect square.",num);
return 0;
}
OUTPUT:
6. Write a program to find out whether the given number is positive, negative or zero
value.
PROGRAM:
#include <stdio.h>
int main()
{
int A;
printf("Enter the number A: ");
scanf("%d", &A);
if (A > 0)
printf("%d is positive.", A);
else if (A < 0)
printf("%d is negative.", A);
else if (A == 0)
printf("%d is zero.", A);
return 0;
}
OUTPUT: