0% found this document useful (0 votes)
12 views39 pages

4th Record (1)_merged (1)

The document outlines a series of C programming exercises aimed at developing problem-solving skills through various tasks, including calculating fortunes based on birth dates, checking for prime numbers, counting arrangements of letters, implementing a FizzBuzz program, generating GPA reports, and performing matrix operations. Each exercise includes a pseudocode, a C program, sample outputs, and a result statement confirming successful execution. The document serves as a laboratory guide for students learning programming concepts in C.

Uploaded by

mm.nitish76
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)
12 views39 pages

4th Record (1)_merged (1)

The document outlines a series of C programming exercises aimed at developing problem-solving skills through various tasks, including calculating fortunes based on birth dates, checking for prime numbers, counting arrangements of letters, implementing a FizzBuzz program, generating GPA reports, and performing matrix operations. Each exercise includes a pseudocode, a C program, sample outputs, and a result statement confirming successful execution. The document serves as a laboratory guide for students learning programming concepts in C.

Uploaded by

mm.nitish76
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
You are on page 1/ 39

IT22111 – PROGRAMMING FOR PROBLEM SOLVING LABORATORY

Ex.no:
Date: C Programming using Simple statements and expressions

1.An astrologer calculates the day’s fortune as the sum of the numbers in
the year of birth. Write a C program to find help him. (hint: find the sum
of digits in a number).

Aim:
To execute a basic C program using vi, cc, and ./a.out commands.

Pseudocode:

C Program:
#include<stdio.h>
int main()
{
int date,year,month,r,n,sum=0,s=0;
printf("Enter the date, month, year of DOB:\n");1
scanf("%d%d%d",&date,&month,&year);
if(date>=1&&date<=31&&month>=1&&month<=12&&year>=1700&&year<=9999)
{
n=date+month+year;
while(n>0)
{
r=n%10;
sum=sum+r;
n=n%10;
}
r=sum%10;
s=s+r;

Roll no: Page no:


IT22111 – PROGRAMMING FOR PROBLEM SOLVING LABORATORY

sum=sum/10;
}
printf("Sum of year DOB is %d\n";s);
}
Sample output:
[eea22@mritlin ~]$ vi 4a.c
[eea22@mritlin ~]$ cc 4a.c
[eea22@mritlin ~]$ ./a.out
Enter the date, month, year of DOB:
03 06 2006
Sum of year DOB is 8

Result:
Thus, a basic C program using vi, cc, and ./a.out commands have been executed successfully.

Roll no: Page no:


IT22111 – PROGRAMMING FOR PROBLEM SOLVING LABORATORY

2. Write a C program to check if a number is Prime or not.

Aim: To execute a basic C program to display the basic details of a student using escape
sequence.

Pseudocode:

C Program:
#include<stdio.h>
void main()
{
int n,i;
printf("Enter a positive integer:");
scanf("%d",&n);
if(n<=1)
{
printf("%d is not a prime number:\n",n);
}
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
printf("%d is not a prime number:\n",n);
}
}
printf("%d is a prime number:\n",n);
}

Roll no: Page no:


IT22111 – PROGRAMMING FOR PROBLEM SOLVING LABORATORY

Sample output:
[eea22@mritlin ~]$ vi 4b.c
[eea22@mritlin ~]$ cc 4b.c
[eea22@mritlin ~]$ ./a.out
Enter a positive integer:59
59 is a prime number

Result:
Thus, a basic C program to check whether the number is prime or not have been executed
successfully.

Roll no: Page no:


IT22111 – PROGRAMMING FOR PROBLEM SOLVING LABORATORY

3.Write a C program to count the number of ways, the letters in a name can be
arranged. (Hint: letters can be arranged in n! ways)
Aim:
To execute a c to count number of ways, the letters in a name can be arranged

Pseudocode:

C Program:
#include<stdio.h>
void main()
{
int n,i,fact=1;
printf("Enter a number of letters in your name:");
scanf("%d",&n);
if(n<0)
{
printf("Factorial does exist\n");
}
else{
for(i=1;i<=n;++i)
fact*=i;
printf("The name can be arranged in %d ways”,n,fact);
}
}

Sample Output:
[eea22@mritlin ~]$ vi 4c.c
[eea22@mritlin ~]$ cc 4c.c
[eea22@mritlin ~]$ ./a.out

Roll no: Page no:


IT22111 – PROGRAMMING FOR PROBLEM SOLVING LABORATORY

Enter the number of letters in your name:7


The name can be arranged 5040 ways

Result:
Thus, a basic C program to display the number of ways name can be rearranged have been
executed successfully.

Roll no: Page no:


IT22111 – PROGRAMMING FOR PROBLEM SOLVING LABORATORY

4. Write a program that prints the numbers from 1 to 100. But for multiples of three print
“Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers
which are multiples of both three and five print “FizzBuzz”.

Aim:
To write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz”
instead of the number and for the multiples of five print “Buzz”. For numbers which are
multiples of both three and five print “FizzBuzz”.

Pseudo code:

C Program:
#include<stdio.h>
int main()
{
int i;
for(i=1;i<=100;i++)
{
if(i%15==0)
{
printf("FIZZBUZZ%d\t",i);
}
else if(i%3==0)
{
printf("FIZZ%d\t",i);
}
else if(i%5==0)
{
printf("BUZZ%d\t",i);
}
else

Roll no: Page no:


IT22111 – PROGRAMMING FOR PROBLEM SOLVING LABORATORY

{
printf("%d\t",i);
}
}
return 0;
}

Sample Output:
[eea22@mritlin ~]$ vi 4d.c
[eea22@mritlin ~]$ cc 4d.c
[eea22@mritlin ~]$ ./a.out
1 2 FIZZ 4 BUZZ FIZZ 7 8 FIZZ BUZZ 11 FIZZ 13
14 FIZZBIZZ 16 17 FIZZ 19 BUZZ FIZZ 22 23
FIZZ BUZZ 26 FIZZ 28 29 FIZZBIZZ 31 32 FIZZ 34
BUZZ FIZZ 37 38 FIZZ BUZZ 41 FIZZ 43 44 FIZZBIZZ
46 47 FIZZ 49 BUZZ FIZZ 52 53 FIZZ BUZZ 56
FIZZ 58 59 FIZZBIZZ 61 62 FIZZ 64 BUZZ FIZZ 67
68 FIZZ BUZZ 71 FIZZ 73 74 FIZZBIZZ 76 77 FIZZ 79
BUZZ FIZZ 82 83 FIZZ BUZZ 86 FIZZ 88 89 FIZZBIZZ 91
92 FIZZ 94 BUZZ FIZZ 97 98 FIZZ BUZZ

Result:
The basic c program have been executed successfully.

Roll no: Page no:


IT22111 – PROGRAMMING FOR PROBLEM SOLVING LABORATORY

5. Generate the grade sheet with all subjects along with their credits; calculate the GPA
(with 2 digits precision) as the fraction of total credits earned over the total credits in
Semester 1.
Aim:
To generate the grade sheet with all subjects along with their credits; calculate the GPA (with
2 digits precision) as the fraction of total credits earned over the total credits in Semester 1.

Pseudo Code:

C Program:
#include<stdio.h>
void main()
{
int n,i,j,k,l,credits=0,gpa[10][2];
int marks=0,cred=0;
float gpas;
printf("Enter Number of Subjects: ");
scanf("%d",&n);
char grade;
for(i=0;i<n;i++){
k=i+1;
printf("Enter Subject %d Marks: ",k);
scanf("%d",&gpa[i][1]);
printf("Enter Subject %d Credits: ",k);
scanf("%d",&gpa[i][2]);
}
printf("\n");

Roll no: Page no:


IT22111 – PROGRAMMING FOR PROBLEM SOLVING LABORATORY

for(i=0;i<n;i++){
l=i+1;
marks=gpa[i][1]/10.0;
switch( marks){
case 10: grade = 'O';
break;
case 9: grade = 'S';
break;
case 8: grade = 'A';
break;
case 7: grade = 'B';
break;
case 6: grade = 'C';
break;
case 5: grade = 'D';
break;
default: grade = 'U';
break; }
cred=cred+(marks*gpa[i][2]);
credits=credits+gpa[i][2];
printf("Grade in Subject %d: %c \n",l,grade);
}
gpas=cred/(float)credits;
printf("Credits Earned: %d\n",cred);
printf("GPA : %.2f",gpas);
}

Sample Out:
Enter Number of Subjects: 4
Enter Subject 1 Marks: 100
Enter Subject 1 Credits: 4
Enter Subject 2 Marks: 98
Enter Subject 2 Credits: 3
Enter Subject 3 Marks: 99
Enter Subject 3 Credits: 2
Enter Subject 4 Marks: 100
Enter Subject 4 Credits: 4

Grade in Subject 1: O

Roll no: Page no:


IT22111 – PROGRAMMING FOR PROBLEM SOLVING LABORATORY

Grade in Subject 2: S
Grade in Subject 3: S
Grade in Subject 4: O
Credits Earned: 125
GPA: 9.62

Result:
Thus, the c program to calculate gpoa is executed successfully

Roll no: Page no:


IT22111 – PROGRAMMING FOR PROBLEM SOLVING LABORATORY

Ex No:
Date: Simple programming for one dimensional and two-dimensional
Arrays
___________________________________________________________________________

1. Read the heights of N kids and find the maximum height.

Aim: To Execute A Basic C Program to Read the heights of N kids and find the maximum height.

Pseudocode:

C Program:
#include<stdio.h>
void main()
{
int height[50],i,j,max,n;
printf("Enter the Number of Students: ");
scanf("%d",&n);
for(i=0;i<n;i++){
j=i+1;
printf("Enter the Heights of Student %d: ",j);
scanf("%d",&height[i]);}
max=height[0];
for(i=1;i<n;i++){
if(height[i]>max)
max=height[i];
}
printf("The Maximum Height is %d",max);
}

Roll no: Page no:


IT22111 – PROGRAMMING FOR PROBLEM SOLVING LABORATORY

Sample Output:
[eea22@mrtlin~]$vi 5aa.c
[eea22@mrtlin~]$cc 5aa.c
[eea22@mrtlin~]$./a.out
Enter the Number of Students: 4
Enter the Heights of Student 1: 175
Enter the Heights of Student 2: 168
Enter the Heights of Student 3: 169
Enter the Heights of Student 4: 167
The Maximum Height is 175

Result: Thus, A Basic C Program to Read the heights of N kids and find the maximum height has been
Executed Successfully

Roll no: Page no:


IT22111 – PROGRAMMING FOR PROBLEM SOLVING LABORATORY

2. Read the marks of M students in N subjects and print the same as a matrix.

Aim: To Execute a Basic C Program to Read the marks of M students in N subjects and print the same as a
matrix.

Pseudocode:

C Program:
#include<stdio.h>
void main(){
int submarks[10][100],i,j,k,l,m,n;
printf("Enter the Number of Students: ");
scanf("%d",&m);
printf("Enter the Number of Subjects: ");
scanf("%d",&n);
for(i=0;i<m;i++){
k=i+1;
printf("Subject %d:\n",k);
for(j=0;j<n;j++){
l=j+1;
printf("Enter the Marks of Student %d: ",l);
scanf("%d",&submarks[i][j]);}
}
printf("\n");
for(i=0;i<m;i++){
k=i+1;
printf("Subject %d Marks: ",k);
for(j=0;j<n;j++){
printf("%d ",submarks[i][j]);}
printf("\n");}
}

Roll no: Page no:


IT22111 – PROGRAMMING FOR PROBLEM SOLVING LABORATORY

Sample Output:
[eea22@mrtlin~]$vi 5bb.c
[eea22@mrtlin~]$cc 5bb.c
[eea22@mrtlin~]$./a.out
Subject 1:
Enter the Marks of Student 1: 100
Enter the Marks of Student 2: 99
Enter the Marks of Student 3: 98
Enter the Marks of Student 4: 100
Subject 2:
Enter the Marks of Student 1: 100
Enter the Marks of Student 2: 99
Enter the Marks of Student 3: 97
Enter the Marks of Student 4: 98
Subject 3:
Enter the Marks of Student 1: 100
Enter the Marks of Student 2: 97
Enter the Marks of Student 3: 98
Enter the Marks of Student 4: 99
Subject 4:
Enter the Marks of Student 1: 100
Enter the Marks of Student 2: 98
Enter the Marks of Student 3: 99
Enter the Marks of Student 4: 100

Subject 1 Marks: 100 99 98 100


Subject 2 Marks: 100 99 97 98
Subject 3 Marks: 100 97 98 99
Subject 4 Marks: 100 98 99 100

Result: Thus, A Basic C Program to Read the marks of M students in N subjects and print the same as a matrix
has been Executed Successfully.

Roll no: Page no:


IT22111 – PROGRAMMING FOR PROBLEM SOLVING LABORATORY

3. Write a C program to perform matrix addition.


Aim: To Execute A Basic C Program to perform Matrix Addition.

Pseudocode:

Roll no: Page no:


IT22111 – PROGRAMMING FOR PROBLEM SOLVING LABORATORY

C Program:
#include<stdio.h>
void main(){
int m,n,i,j,k,l,add,mat1[10][10],mat2[10][10];
printf("Enter Number of Rows: ");
scanf("%d",&m);
printf("Enter Number of Columns: ");
scanf("%d",&n);
printf("\nMatrix A\n");
for(i=0;i<n;i++){
k=i+1;
for(j=0;j<n;j++){
l=j+1;
printf("Enter A%d%d: ",k,l);
scanf("%d",&mat1[i][j]);}
}
printf("\n");
printf("Matrix B\n");
for(i=0;i<n;i++){
k=i+1;
for(j=0;j<n;j++){
l=j+1;
printf("Enter B%d%d: ",k,l);
scanf("%d",&mat2[i][j]);}
}
printf("\nAddition of A and B:\n");
for(i=0;i<n;i++){
for(j=0;j<n;j++){
add=mat1[i][j]+mat2[i][j];
printf("%d\t",add);
}
printf("\n");}
}

Sample Output:
[eea22@mrtlin~]$vi 5cc.c
[eea22@mrtlin~]$cc 5cc.c
[eea22@mrtlin~]$./a.out
Enter Number of Rows: 2
Enter Number of Columns: 2

Matrix A
Enter A11: 1
Enter A12: 2
Enter A21: 3
Enter A22: 4

Matrix B
Roll no: Page no:
IT22111 – PROGRAMMING FOR PROBLEM SOLVING LABORATORY

Enter B11: 4
Enter B12: 8
Enter B21: 9
Enter B22: 6

Addition of A and B:
5 10
11 10

Result: Thus, A Basic C Program to Perform Matrix Addition has been Executed Successfully.

Roll no: Page no:


IT22111 – PROGRAMMING FOR PROBLEM SOLVING LABORATORY

4. Prof. Siva awards marks for N questions in the range 0-16. Support him in finding the total. Ensure
the following: a. Only numbers are entered b. If he awards marks out of the range, ask him to re-enter
the mark c. limit the total to 100.

Aim: To Execute A Basic C Program to Read Marks of each Question which accepts only numbers as input
between 0 to 16 and limits the Total to 100.

Pseudocode:

C Program:
#include<stdio.h>
void main(){
int n,i,j,k,q[100],total=0;
printf("Enter Number of Questions: ");
scanf("%d",&n);
for(i=0;i<n;i++){
j=i+1;
printf("Enter Q%d Marks: ",j);
if(scanf("%d",&q[i])!=1){
printf("Enter Valid Marks\n");
printf("Re-Enter Q%d Marks: ",j);
scanf("%d",&q[i]);

Roll no: Page no:


IT22111 – PROGRAMMING FOR PROBLEM SOLVING LABORATORY

total+=q[i];
continue;}
else if(q[i]<0 || q[i]>16){
printf("Mark exceeds 16!\n");
printf("Re-Enter Q%d Marks: ",j);
scanf("%d",&q[i]);
total+=q[i];
continue;}
total+=q[i];
if(total>100){
printf("Total Marks exceeds Zero\n");
break;}
}
if(total<100)
printf("Total Marks: %d",total);}

Sample Output:
[eea22@mrtlin~]$vi 5dd.c
[eea22@mrtlin~]$cc 5dd.c
[eea22@mrtlin~]$./a.out
Enter Number of Questions: 5
Enter Q1 Marks: 17
Enter Q2 Marks: 17
Enter Q3 Marks: 17
Enter Q4 Marks: 10
Enter Q5 Marks: 9
Total Marks: 70

Result:

Thus, A Basic C Program to to Read Marks of each Question which accepts only numbers as input between
0 to 16 and limits the Total to 100 has been Executed Successfully.

Roll no: Page no:


IT22111 – PROGRAMMING FOR PROBLEM SOLVING LABORATORY

5. Dr. Sudha marks daily attendance of N students as a sequence of ‘P’ and ‘A’ for present and absent
respectively. Help her to find the number of present and absentees for a day.

Aim: To Execute A Basic C Program to find number of Present and Students per day.

Pseudocode:

C Program:
#include<stdio.h>
void main(){
int i,j,n,p=0,a=0;
char s[100];
printf("Enter Number of Students: ");
scanf("%d",&n);
printf("Enter P for Present and A for Absent\n");
for(i=0;i<n;i++){
j=i+1;
printf("Student %d: ",j);
scanf(" %c",&s[i]);
if(s[i]=='P' || s[i]=='p')
p++;
if(s[i]=='A' || s[i]=='a')
a++;}
printf("Number of Present Students: %d\n",p);
printf("Number of Absent Students: %d",a);}
}

Sample Output:
[eea22@mrtlin~]$vi 5ee.c
[eea22@mrtlin~]$cc 5ee.c
[eea22@mrtlin~]$./a.out
Enter Number of Students: 4
Enter P for Present and A for Absent
Student 1:P
Student 2: P
Student 3: A
Student 4:P
Number of Present Students: 3
Number of Absent Students: 1

Roll no: Page no:


IT22111 – PROGRAMMING FOR PROBLEM SOLVING LABORATORY

Result:
Thus, a Basic C Program to find number of Present and Students per day has been Executed Successfully.

Roll no: Page no:


IT22111-PROGRAMING FOR PROBLEM SOLVING LABORATORY

EXP.NO:
DATE:

Solving problems using Strings

1. Write a C program to print each character in a string in a line

AIM:
To execute a c program to print each character in a string in a line .

PSEUDOCODE:

C PROGRAM:

#include <stdio.h>
#include <string.h>
void main()
{
char a[100];
printf("Enter a string: ");
gets(a);
int n = strlen(a);
for(int i = 0;i<n;i++){
printf("%c\n",a[i]);
}
}

Roll Number : 2127240601027 Page No :


IT22111-PROGRAMING FOR PROBLEM SOLVING LABORATORY

SAMPLE OUTPUT:

[eea27@mritlin ~]$ cc 6.1.c


[eea27@mritlin ~]$ ./a.out
Enter a string: Cristiano
C
r
i
s
t
i
a
n
o

RESULT:

Thus, the c program to execute a c program to print each character in a string in a line has
been executed successfully.

Roll Number : 2127240601027 Page No :


IT22111-PROGRAMING FOR PROBLEM SOLVING LABORATORY

2. Write a C program to count the frequency of vowels in a string.

AIM:
To execute a C program to count the frequency of vowels in a string.

PSEUDOCODE:

C PROGRAM:

#include <stdio.h>
#include <string.h>
void main()
{
char a[100];
printf("Enter a string: ");
gets(a);
int n = strlen(a);
int c = 0;
for(int i = 0;i<n;i++){
if(a[i] == 'a' || a[i] == 'e' || a[i] == 'i' || a[i] == 'o' || a[i] == 'u' || a[i] == 'A' || a[i] == 'E' || a[i]
== 'I' || a[i] == 'O' || a[i] == 'U'){
c++;
}
}
printf("%d is the number of vowels in the string: %s",c,a);
}

Roll Number : 2127240601050 Page No :


IT22111-PROGRAMING FOR PROBLEM SOLVING LABORATORY
SAMPLE OUTPUT:

[eea27@mritlin ~]$ cc 6.2.c


[eea27@mritlin ~]$ ./a.out
Enter a string: Cristiano
4 is the number of vowels in the string:

RESULT:

Thus a C program to count the frequency of vowels in a string is executed successfully.

Roll Number : 2127240601027 Page No :


IT22111-PROGRAMING FOR PROBLEM SOLVING LABORATORY
3.A numerologist suggests renaming a person by repeating the vowel in the name
subsequent to its existing position. Ex: Sivakumar : Siivaakuumaar. Write a C program
to automate his suggestion

AIM:
To execute a c program to rename a person by repeating the vowel in the name subsequent
to its existing position.

PSEUDOCODE:

Roll Number : 2127240601027 Page No :


IT22111-PROGRAMING FOR PROBLEM SOLVING LABORATORY
C PROGRAM:

#include <stdio.h>
#include <string.h>
void main()
{
char a[100],b[100]="";
printf("Enter a string: ");
gets(a);
int n = strlen(a);
for(int i = 0;i<n;i++){
char e[] = {a[i],'\0'};
if(a[i] == 'a' || a[i] == 'e' || a[i] == 'i' || a[i] == 'o' || a[i] == 'u' || a[i] == 'A' || a[i] == 'E' || a[i]
== 'I' || a[i] == 'O' || a[i] == 'U'){
strcat(b,e);
strcat(b,e);
}
else {

strcat(b,e);
}
}
printf("The astrologer's suggestion for the name is %s",b);

SAMPLE OUTPUT:

[eea27@mritlin ~]$ cc 6.3.c


[eea27@mritlin ~]$ ./a.out
Enter a string: Gokul
The astrologer's suggestion for the name is Gookuul

RESULT:

Thus, the C program to rename a person by repeating the vowel in the name subsequent
to its existing position has been executed successfully

Roll Number : 2127240601027 Page No :


IT22111-PROGRAMING FOR PROBLEM SOLVING LABORATORY
4. Read the first name, middle name, and last name of a person and form a full name as
the concatenation of them. (Using with and without using library functions).

AIM:
To execute a C program read the first name, middle name, and last name of a person and
form a full name as the concatenation of them.

PSEUDOCODE:

Roll Number : 2127240601027 Page No :


IT22111-PROGRAMING FOR PROBLEM SOLVING LABORATORY

C PROGRAM:

#include <stdio.h>
#include <string.h>
void usefunc(char a[300], char b[100], char c[100]){
strcat(a,(strcat(b,c)));
printf("\n With using Inbuilt function : %s",a);

void nousefunc(char a[300], char b[100], char c[100]){


int pos = 0;
char result[300];
for (int i = 0; a[i] != '\0'; i++) {
result[pos++] = a[i];
}

for (int i = 0; b[i] != '\0'; i++) {


result[pos++] = b[i];
}

for (int i = 0; c[i] != '\0'; i++) {


result[pos++] = c[i];
}
printf("\nWithout using Inbuilt function : %s",result);

void main()
{
char a[300],b[100],c[100];
printf("Enter your first,middle and last name: ");
scanf("%s %s %s",a,b,c);
printf("Your full name after concatentation is");
nousefunc(a,b,c);
usefunc(a,b,c);
}

Roll Number : 2127240601027 Page No :


IT22111-PROGRAMING FOR PROBLEM SOLVING LABORATORY

SAMPLE OUTPUT:

[eea27@mritlin ~]$ cc 6.4.c


[eea27@mritlin ~]$ ./a.out
Enter your first, middle and last name: Gokul P
Your full name after concatenation is
Without using Inbuilt function: GokulP
With using Inbuilt function: GokulP

RESULT:

Thus, a C program read the first name, middle name, and last name of a person and form
a full name as the concatenation of them has been executed successfully.

Roll Number : 2127240601027 Page No :


IT22111 – PROGRAMMING FOR PROBLEM SOLVING LABORATORY

Ex.no: 7
Date:
Programs to illustrate user defined functions

1. Write a C program to implement a pocket calculator using user defined functions.

Aim: To execute a basic C program to implement a pocket calculator using user defined
functions.

Pseudocode:

C Program:
#include <stdio.h>
int sum(int a, int b)
{int c=a+b;
return c;}
int diff(int a, int b)
{int d=a-b;
return d;}
int mult(int a,int b)
{int e=a*b;
return e;}
int div(int a,int b)

Roll no: Page no:


eea27
IT22111 – PROGRAMMING FOR PROBLEM SOLVING LABORATORY

{int f=a/b;
return f;}
int mod(int a,int b)
{int g=a%b;
return g;}
void main()
{int a,b,c,d,e,f,g;
printf("enter the values of a and b:");
scanf("%d%d",&a,&b);
c=sum(a,b);
d=diff(a,b);
e=mult(a,b);
f=div(a,b);
g=mod(a,b);
printf("sum=%d\ndiff=%d\nmult=%d\ndiv=%d\nmod=%d",c,d,e,f,g);
}
Sample output:

[eea27@mritlin ~]$ vi7a.c


[eea27@mritlin ~]$ cc 7a.c
[eea27@mritlin ~]$ ./a.out
enter the values of a and b:5 2
sum=7
diff=3
mult=20
div=5
mod=0

Result: Thus a basic C program to implement pocket calculator using user defined functions
have been executed successfully.

Roll no: Page no:


IT22111 – PROGRAMMING FOR PROBLEM SOLVING LABORATORY
2.Write a C program to calculate factorial of a number using recursive function.

Aim: To execute a basic C program to calculate factorial of a number using recursive


function
Pseudocode:

C Program:
#include<stdio.h>
int fact(int n);
int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d is %d", n, fact(n));
return 0;}
int fact(int n)
{if (n>=1)
return n*fact(n-1);
else
return 1;
}

Roll no: Page no:


IT22111 – PROGRAMMING FOR PROBLEM SOLVING LABORATORY

Sample output:

[eea27@mritlin~]$vi7b.c
[eea27@mritlin~]$cc7b.c
[eea27@mritlin ~]$ ./a.out

Enter a positive integer: 5

Factorial of 5 is 120

Result: Thus, a basic C program to calculate factorial of the number using recursion have
been executed successfully.

Roll no: Page no:


IT22111 – PROGRAMMING FOR PROBLEM SOLVING LABORATORY

3.Write a program to pass two numbers to the function print_even(n1, n2) and print all
the even numbers between n1 and n2.

Aim: To execute a basic C program to pass two numbers to the function print_even(n1, n2)
and print all the even numbers between n1 and n2.

Pseudocode:

C Program:
#include <stdio.h>
void print_even(int a, int b)
{int i;
printf("even numbers between %d and %d\n",a,b);
for(i=a;i<=b;i=i+2)
{printf("%d\t",i);}}
void main()
{int a,b;
printf("enter two even numbers a and b:");

Roll no: Page no:


IT22111 – PROGRAMMING FOR PROBLEM SOLVING LABORATORY

scanf("%d%d",&a,&b);
if((a%2==0)&&(b%2==0))
{print_even(a,b);}
else{printf("invalid");}}
Sample output:

[eea27@mritlin~]$vi7c.c
[eea27@mritlin~]$cc7c.c
[eea27@mritlin ~]$ ./a.out
enter two even numbers a and b:2 24
even numbers are
2 4 6 8 10 12 14 16 18 20 22 24

Result: Thus, a basic C program to even numbers have been executed successfully.

Roll no: Page no:


IT22111 – PROGRAMMING FOR PROBLEM SOLVING LABORATORY

4.Write a program to pass two numbers to the function print_prime(n1,n2) and print
all the prime numbers between n1 and n2.

Aim: To execute a basic C program to pass two numbers to the function print_prime(n1, n2)
and print all the prime numbers between n1 and n2.

Pseudocode:

C Program:

#include <stdio.h>
void printprime();
int primecheck(int n)
{int i,d;
if (n <= 1)
{d=0;
return d; }

Roll no: Page no:


IT22111 – PROGRAMMING FOR PROBLEM SOLVING LABORATORY

for (i = 2; i <= n / 2; i++) {


if (n % i == 0)
{d=0;
return d; }}
d=1;
return d;}
void printprime(int a, int b)
{int i,f;
printf("prime numbers are\n");
for(i=a;i<=b;i++)
{f=primecheck(i);
if (f==1)
{printf("%d\t",i);}}}
void main()
{int a,b,d,f;
printf("enter two prime numbers a and b:");
scanf("%d%d",&a,&b);
d=primecheck(a);
f=primecheck(b);
if((d==1)&&(f==1))
{printprime(a,b);}}
Sample output:

[eea27@mritlin~]$vi7d.c
[eea27@mritlin~]$cc7d.c
[eea27@mritlin ~]$ ./a.out
enter two prime numbers a and b:3 23
prime numbers are
3 5 7 11 13 17 19 23

Result: Thus, a basic C program to print have been executed successfully.

Roll no: Page no:


eea27

You might also like