Lab Report On C Programming For Grade 12 1
Lab Report On C Programming For Grade 12 1
Submitted To
Department of Management
For the Partial fulfillment of
Computer Science (com. 232)
BAC international College, Kathmandu
2077BS
Submitted By
Name: Ram Thapa Magar
Grade: Twelve
Roll No.:
Date: 2077/00/00
1
Lab No. 1
Conclusion: The message hello world has been successfully run and gets the accurate result.
2
Question: Write a program using escape sequence.
Objective: To display program using escape sequence.
Theory: First of all we declare the header files, main function, print the message using
printf();, scanf() for read character and interprets, function and getch() used for holding the
console result.
Algorithm:
Step 1: Start
Step 2: Enter a value of x and y.
Step 3: Calculate sum, sub and mul
Step 4: Print the value of x and y
Step 5: End
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int x,y,sum,sub,mul;
printf("Enter a value of x:");
scanf("%d",&x);
printf("Enter a value of y:");
scanf("%d",&y);
printf("The result of sum is =%d\n",sum=x+y);
printf("The result of sub is =%d\n", sub=x-y);
printf("The result of mul is =%d\n", mul=x*y);
return 0;
getch();
}
3
Conclusion: A program using escape sequence successfully run.
4
Lab No. 2
Question: Write a program to get input from users and swap the value.
Objective: To display the input from users and swap the value.
Theory: First of all we declare the header files, main function, print the message using
printf();, scanf() for read character and interprets, function and getch() used for holding the
console result.
Algorithm:
Step 1: Start
Step 2: Enter a value of x, y and z.
Step 3: swap the x to y and y to z.
Step 4: print the value of x and y.
Step 5: End
Source Code:
#include<stdio.h>
#include<conio.h>
main()
{
int x,y,extra;
printf("Enter a value of x:");
scanf("%d",&x);
printf("Enter a value of y:");
scanf("%d",&y);
extra=x;
x=y;
y=extra;
printf("x=%d \n y=%d",x,y);
return 0;
getch();
}
7
Conclusion: A program using input from users and swap successfully run.
Question: Write a program to collect values from users and calculate square.
Objective: To display the calculation of square using c programming.
Theory: First of all we declare the header files, main function, print the message using
printf();, scanf() for read character and interprets, function and getch() used for holding the
console result.
Algorithm:
Step 1: start
Step 2: Enter a value of x.
Step 3: Calculation of square = x*x
Step 4: print the result of square.
Step 5: End.
Source Cord:
#include<stdio.h>
#include<conio.h>
main()
{
int x, square;
printf("Enter a value of x:");
scanf("%d",&x);
printf("The area of square = %d", square=x*x);
return 0;
8
getch();
}
9
Lab No. 4
Question: Write a program to collect three variables from users and calculate average.
Objective: To display the calculation of average by collecting three variables from users.
Theory: First of all we declare the header files, main function, print the message using
printf();, scanf() for read character and interprets, function and getch() used for holding the
console result.
Algorithm:
Step 1: Start
Step 2: Enter a value of x,y and z.
Step 3: Calculation of average = (x+y+z)/3
Step 4: Print the result of square.
Step 5: End.
Source Cord:
#include<stdio.h>
#include<conio.h>
main()
{
int x,y,z,avg;
printf("Enter a value of x:");
scanf("%d",&x);
printf("Enter a value of y:");
scanf("%d",&y);
printf("Enter a value of z:");
scanf("%d",&z);
printf("The average is = %d", avg= (x+y+z)/3);
return 0;
getch();
}
10
Conclusion: A program to collect three variables from users and calculate average
successfully run.
11
Lab No. 5
12
Conclusion: A program to calculate simple interest.
13
printf("Enter a rate:");
scanf("%f",&R);
CI= P*(pow((1+R/100),T));
CI= CI-P;
printf("Compound Intrest = %f",CI);
return 0;
getch();
}
15
Lab No. 6
Question: Write a program to calculate student details and division using c programming
Objective: To display the calculation student details and division.
Theory: First of all we declare the header files, main function, print the message using
printf();, scanf() for read character and interprets, function and getch() used for holding the
console result.
Source Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
char name,address;
int rollno,sub1,sub2,sub3,sub4,sub5,total;
float percentage;
printf("Enter your name:");
scanf("%s",&name);
printf("Enter your address:");
scanf("%s",&address);
printf("Enter your rollno:");
scanf("%d",&rollno);
printf("Enter your all 5 subjects marks:");
scanf("%d%d%d%d%d",&sub1,&sub2,&sub3,&sub4,&sub5);
total=sub1+sub2+sub3+sub4+sub5;
percentage=total/5;
printf("Your percentage is = %f\n",percentage);
if(100&&percentage>=80)
{
printf("You are passed in Distanction");
}
16
else if(percentage<80&&percentage>=60)
{
printf("You are passed in first division");
}
else if(percentage<60&&percentage>=45)
{
printf("You are passed in secound division");
}
else if(percentage<45&&percentage>=35)
{
printf("You are passed in third division");
}
else
{
printf("Sorry!! Better try for next time");
}
return 0;
getch();
}
Question: Write a program about age who can drive and cannot.
17
Objective: To display a program age who can drive and cannot.
Theory: First of all we declare the header files, main function, print the message using
printf();, scanf() for read character and interprets, function and getch() used for holding the
console result.
Algorithm:
Step 1: start
Step 2: Enter your age.
Step 3: Read the age.
Step 4: Condition check according to age.
Step 5: Print the message.
Step 6: End.
Source Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int age;
printf("Enter your age:");
scanf("%d",&age);
if (age<=70&&age>=18)
{
printf("You are above 18 and below 70, you can drive\n");
}
else
{
printf("You cannot drive");
}
return 0;
getch();
}
18
Conclusion: a program about age who can drive and cannot successfully run.
If passed Message
A Excellent
B Good
C Fair
D Poor
Objective: To display the program.
Theory: First of all we declare the header files, main function, print the message using
printf();, scanf() for read character and interprets, function and getch() used for holding the
console result.
Algorithm:
Step 1: start
Step 2: Enter a letter
Step 3: Read the letter.
Step 4: Print the appropriate result.
Step 5: End.
Source Code:
#include<conio.h>
#include<math.h>
int main()
{
char x;
printf("Enter a letter:");
scanf("%s",&x);
19
if(x=='A')
{
printf("Excellent");
}
else if(x=='B')
{
printf("Good");
}
else if(x=='c')
{
printf("Fair");
}
else if(x=='D')
{
printf("Poor");
}
return 0;
getch();
}
20
Lab No. 7
21
getch();
}
Question: Write a program to read from user and print it is vowel or not.
Objective: To display a program entered letter is vowel or not.
Theory: First of all we declare the header files, main function, print the message using
printf();, scanf() for read character and interprets, function and getch() used for holding the
console result.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int c;
printf("Enter a character:");
scanf("%d",&c);
switch(c)
{
case 1:'a';
case 2:'A'; printf("vowel"); break;
case 3:'e';
case 4:'E'; printf("Vowel"); break;
case 5:'i';
case 6:'I'; printf("Vowel"); break;
22
case 7:'o';
case 8:'O'; printf("Vowel"); break;
case 9:'u';
case 10:'U'; printf("Vowel"); break;
default: printf("Entered character is not vowel"); break;
}
return 0;
getch();
}
Conclusion: A program to read from user and print it is vowel or not successfully run.
23
Lab No. 8
24
Theory: First of all we declare the header files, main function, print the message using
printf();, scanf() for read character and interprets, function and getch() used for holding the
console result.
Algorithm:
Step 1: start
Step 2: Enter a number.
Step 3: Read a number and condition check.
Step 4: Print the result.
Step 5: End.
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int i,n,m;
printf("Enter a number:");
scanf("%d",&n);
for(i=1;i<=10;i++);
{
m=i*n;
printf("Your result is = %d",m);
}
return 0;
getch();
}
25
Conclusion: A program to calculate multiplication using for loop successfully run.
26
pow= x*pow;
printf("%d^%d=%d",pow);
}
return 0;
getch();
}
Conclusion: a program to calculate power and print successfully run.
27
Lab No. 9
Question: Write a program to read number from user and then calculate sum of digits in it
using while loop.
Objective: To display a number from user and calculation of sum.
Theory: First of all we declare the header files, main function, print the message using
printf();, scanf() for read character and interprets, function and getch() used for holding the
console result.
Algorithm:
Step 1: start
Step 2: Enter a number.
Step 3: Read a number and condition check.
Step 4: Print the result.
Step 5: End.
Source Code:
#include <stdio.h>
int main() {
int n,r,sum=0;
printf("Enter a number:");
scanf("%d",&n);
while(n!=0)
r=n%10;
sum= 0+4/sum+r;
n= n%10;
printf("The sum of entered number is = %d",sum);
return 0;
}
Conclusion: a program to read number from user and then calculate sum of digits
successfully run.
Question: Write a program to read number from user then print multiplication table using do
while loop.
28
Objective: To display a multiplication table using do while loop.
Theory: First of all we declare the header files, main function, print the message using
printf();, scanf() for read character and interprets, function and getch() used for holding the
console result.
Algorithm:
Step 1: start
Step 2: Enter a number.
Step 3: Read a number and condition check.
Step 4: Print the number.
Step 5: End.
Source Code:
#include <stdio.h>
int main() {
int n,m,i=0;
printf("Enter a number:");
scanf("%d",&n);
do
{
m=n*i;
printf("%d",m);
i++;
}
while(i<=10);
return 0;
}
29
Conclusion: a program to read number from user then print multiplication table successfully
run.
Question: Write a program to calculate the factorial of a given number using for loop.
Objective: To print and display the factorial of given number..
Theory: First of all we declare the header files, main function, print the message using
printf();, scanf() for read character and interprets, function and getch() used for holding the
console result.
Algorithm:
Step 1: Start
Step 2: To enter a number.
Step 3: Address and read the condition of for loop.
Step 4: print the output.
Step 5: Stop
Source Code:
#include<stdio.h>
int main(){
int i,f=1,num;
for(i=1;i<=num;i++)
f=f*i;
30
Conclusion: a program to calculate factorial successfully run.
31
}
return 0;
}
Conclusion: a program to print symbolic pattern successfully run.
32
Lab No. 10
Question: WAP to declare a function 'add' and perform addition operation using function.
Objective: To print and display addition function.
Theory: First of all we declare the header files, main function, print the message using
printf();, scanf() for read character and interprets, function and getch() used for holding the
console result.
Algorithm:
Step 1: Start
Step 2: Enter a two number.
Step 3: Add the entered two number
Step 4: Print the result.
Step 5: Stop.
Source Code:
#include<stdio.h>
#include<conio.h>
int add(int,int);
void main()
{
int a,b,sum;
printf("\n Enter the two number: ");
scanf("%d%d",&a,&b);
sum=add(a,b);
printf("\nAddition of tow number is: %d",sum);
getch();
}
int add(int x, int y)
{
int add=x+y;
return(add);
}
33
Conclusion: A program to calculate sum successfully run.
Question: WAP to declare function 'area' and calculate area of circle using function.
Objective: To display and print the area of circle.
Theory: First of all we declare the header files, main function, print the message using
printf();, scanf() for read character and interprets, function and getch() used for holding the
console result.
Algorithm:
Step 1: Start
Step 2: Enter a radius of circle.
Step 3: Calculate the statement
Step 4: Print the outcome of calculation
Step 5: Stop.
Source Code:
#include<stdio.h>
#include<conio.h>
float area(float);
void main()
{
float r,a;
printf("\n Enter the radius of cirlce: ");
scanf("%f",&r);
a=area(r);
printf("\nArea of a circle is : %f",a);
34
getch();
}
float area(float r)
{
float a;
a=3.14*r*r;
return(a);
}
Question: WAP to read integer from user. Calculate and print its factorial on monitor.
Objective: To print and display the calculation of factorial.
Theory: : First of all we declare the header files, main function, print the message using
printf();, scanf() for read character and interprets, function and getch() used for holding the
console result.
Algorithm
Step 1: Start
Step 2:
Step 3:
Step 4:
Step 5:
Source Code:
#include<stdio.h>
#include<conio.h>
int factorial(int);
35
void main()
{
int x,t;
printf("\nEnter the number : ");
scanf("%d",&x);
t=factorial(x);
printf("\n%d!=%d",x,t);
getch();
}
int factorial(int n)
{
int f;
if(n==1)
{
return 1;
}
else
{
f=n*factorial(n-1);
}
return f;
}
36
Lab No. 11
Question: WAP to read 10 integer in array, calculation sum and average of numbers in array.
Objective: To print and display 10 integer number and calculate sum and average.
Theory: First of all we declare the header files, main function, print the message using
printf();, scanf() for read character and interprets, function and getch() used for holding the
console result.
Algorithm:
Step 1: Start
Step 2: Enter a ten number
Step 3: Sum a given ten number
Step 4: Calculate it's average and print the given output.
Step 5: Stop.
Source Code;
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,sum=0;
float avg;
for(i=0;i<10;i++)
{
printf("Enter Number %d:",i+1);
scanf("%d",&a[i]);
sum=sum+a[i];
}
avg=sum/10;
printf("\nSum=%d\nAverage=%f",sum,avg);
}
getch();
37
}
Conclusion: Program to read ten integer and calculate sum and average successfully run
Question: WAP to read 3X3 matrix then calculate and print addition of matrix.
Objective: To print and display 3*3 matrix and calculate addition.
Theory: First of all we declare the header files, main function, print the message using
printf();, scanf() for read character and interprets, function and getch() used for holding the
console result.
Algorithm:
Step 1: Start
Step 2:
Step 3:
Step 3:
Step 4:
Step 5:
Source code:
#include<stdio.h>
38
#include<conio.h>
Void main()
{
int a[3][3], b[3][3], sum[3][3], i,j;
printf("Enter a First Matrix: ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
printf("Enter a Second Matrix: ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("\n Addition of Matrix is: ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
sum[i][j]=a[i][j]+b[i][j];
printf("%d\t",sum[i][j]);
}
getch();
}
Conclusion: Program to read 3*3 matrix and to calculate addition successfully run.
39
Lab No. 12
Question: WAP to read a string from user and print number of total character in it.
Objective: To print and display a string from user with number of character.
Theory: First of all we declare the header files, main function, print the message using
printf();, scanf() for read character and interprets, function and getch() used for holding the
console result.
Algorithm
Step 1: Start
Step 2: Enter a string
Step 3: Calculate the string
Step 4: condition check and print the output
Step 5: Stop
Source Code:
#include<stdio.h>
#include<conio.h>
int main()
{
char str[30];
int i=0;
int count=0;
printf("\nEnter The String: ");
gets(str);
while(str[i]!='\0')
{
count++;
i++;
}
printf("\n Total character in string is : %d",count);
getch();
}
40
Conclusion: Program to print string and calculate number of string successfully run.
Bibliography
41