Lab Reports c programming
Lab Reports c programming
Write a program and algorithm and draw the follow chart for displaying the statement
"Hello World!".
Algorithm:
Step 1: Start
Step 2: Display Hello World!
Step 3: Stop
Follow Chart:
Start
End
Program:
#include<stdio.h>
#inclide<conio.h>
void main()
{
printf("Hello World!");
getch();
}
Output:
Write a program for adding two variables, display the result and draw the follow chart.
Algorithm:
Step 1: Start
Step 2: Input the value of a and b
Step 3: Calculate c=a+b
Step 4: Display c
Step 5: Stop
Follow Chart:
Start
Input a and b
c=a+b
Display c
End
Program:
#include <stdio.h>
#include<conio.h>
int main()
{
Output:
Write an algorithm and draw the follow chart to find the area of circle.
Algorithm:
Step 1: Start
Step 2: Input Radius of circle
Step 3: Calculate Area = pi*r*r
Step 4: Display Area of circle
Step 5: Stop
Flow Chart:
Start
Area = pi*r*r
End
Program:
#include<stdio.h>
#include<conio.h>
#define PI 3.14159
int main()
Output:
Write an algorithm and draw the flow chat to find the area of rectangle.
Algorithm:
Step 1: Start
Step 2: Input length and width
Step 3: Calculate Area = length and width
Step 4: Print Area
Step 5: Stop
Flow Chart:
Start
End
Program:
#include <stdio.h>
int main()
{
float length, width, area;
printf("Enter the length of the rectangle: ");
scanf("%f", &length);
area = length*width;
printf("The area of the rectangle with length is : %f\n", area);
return 0;
}
Output:
Write an algorithm for division of two variables and display the result also draw the follow
chart.
Algorithm:
Step 1: Start
Step 2: Input the value of a and b
Step 3: Calculate c=a/b
Step 4: Display c
Step 5: Stop
Flow Chart:
Start
c = a/b
Display c
End
Program:
#include <stdio.h>
#include<conio.h>
int main()
{
int a, b, c;
Output:
Write an algorithm for multiply of two variables and display result and draw a follow
chart.
Algorithm:
Step 1: Start
Step 2: Input the value of x and y
Step3: Calculate z = x*y
Step 4: Display z
Step 5: Stop
Flow Chart:
Start
z = x*y
Display z
End
Program:
#include <stdio.h>
#include<conio.h>
int main()
{
Output:
Write an algorithm for subtraction of two variables and display the result and draw the
flow chat.
Algorithm:
Step 1: Start
Step 2: Input the value of a and b
Step 3: Calculate c = a-b
Step 4: Display c
Step 5: Stop
Flow chart:
Start
c = a-b
Display c
End
Program:
#include <stdio.h>
#include<conio.h>
int main()
{
Output:
Write a program to calculate simple interest and draw the flow chart.
Algorithm:
Step 1: Start
Step 2: Input principle, time and rate
Step 3: Calculate s = (p*t*r)/100
Step 4: Display s
Step 5: Stop
Flow Chart:
Start
Input p, t and r
s = (p*t*r)/100
Display s
End
Program:
#include <stdio.h>
#include<conio.h>
int main()
{
int p, t, r, s;
Output:
Write an algorithm and draw the flow chart to declare that given age is adult or child.
Algorithm:
Step 1: Start
Step 2: Input age
Step 3: If age>=18 then display "Adult" else display "Child"
Step 4: Stop
Flow Chart:
Start
Input age
No Yes
Yes If age>=18
Ye s
End Ye s
Yes
#include <stdio.h>
int main() {
int age;
return 0;
}
Output:
Write a program to find the given number in odd number or even number and draw flow
chart.
Algorithm:
Step 1: Start
Step 2: Input number
Step 3: If number/2 == 0 then display number in even number else display number is odd
number.
Step 4: Stop
Flow Chart:
Start
Input Number
No Yes
If
Yes Number/2
Ye s
==0
End
Yes Ye s
if (number % 2 == 0)
{
printf("%d is an even number.\n", number);
}
else
{
printf("%d is an odd number.\n", number);
}
return 0;
}
Output:
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int max, a,b;
printf("Enter First number:");
scanf("%d",&a);
printf("Enter Second number:");
scanf("%d",&b);
max=a>b ? a:b;
printf("The greatest number is %d",max);
getch();
Output:
Write a program to find greatest number among two number using term and condition.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int max, a,b;
printf("Enter First number:");
scanf("%d",&a);
printf("Enter Second number:");
scanf("%d",&b);
printf("%d is greatest number.",a>b?a:b);
getch();
Output:
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
printf("Enter First number:");
scanf("%d",&a);
printf("Enter Second number:");
scanf("%d",&b);
printf("Enter Third number:");
scanf("%d",&c);
if(a>b)
{
if(a>c)
{
printf("%d is a greatest number",a);
}
else
{
printf("%d is greatest number",c);
}
}
else
{
if (b>c)
{
printf("%d is a greatest number",b);
}
else
{
printf("%d is the greatest number",c);
}
}
getch();
}
Program:
#include<stdio.h>
#include<conio.h>
void main ()
{
int n;
printf("Enter a number:");
scanf("%d", &n);
if (n>0)
{
printf("%d is a positive number",n);
}
else if (n<0)
{
printf("%d is negative number",n);
}
else
{
printf("%d is a Zero",n);
}
getch();
Output:
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
printf("Enter a number:");
scanf("%d",&a);
if (a==0)
{
printf("You have enter Zero.");
}
else
{
printf("You number is not equal to zero.");
}
getch();
}
Output:
Program:
#include<stdio.h>
#include<conio.h>
void main ()
{
int n=10;
int *p;
p=&n;
printf("\n%d, %d, %d",n,*p,p);
getch();
Output:
Program:
#include<stdio.h>
#include<conio.h>
void main ()
{
int a=3000;
int *ptr1;
int **ptr2;
ptr1=&a;
ptr2=&ptr1;
printf("The value of a is: %d",a);
printf("\nThe value available at ptr1 is: %d",*ptr1);
printf("\nThe value available ate ptr2 is: %d",**ptr2);
getch();
Output:
Program:
#include<stdio.h>
#include<conio.h>
void main ()
{
int i,n;
printf("Enter the number you want to print the series:");
scanf("%d",&n);
while (i<=n)
{
printf("%d ",i);
i++;
}
getch();
Output:
Write a program to create 2X2 array and insert the element in it and display them.
Program:
#include <stdio.h>
int main()
{
int arr[2][2];
int i, j;
printf("Enter elements for the 2x2 array:\n");
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
printf("Enter element at position [%d][%d]: ", i, j);
scanf("%d", &arr[i][j]);
}
}
printf("\nThe 2x2 array is:\n");
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
printf("%d ", arr[i][j]);
}
printf("\n");
}
return 0;
}
Output:
Output:
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[10];
char l_name[10];
printf("Enter your name: ");
scanf("%s",&name);
printf("\nEnter your last name: ");
scanf("%s",&l_name);
strcat(name, " ");
strcat(name,l_name);
printf("\nYour complete name is: %s",name);
return 0;
}
Output:
Output:
Write a program to input two number and defining them are positive or negative using go
to.
Program:
#include <stdio.h>
#include <conio.h>
int main()
{
int num1, num2;
if (num2 < 0)
goto negative;
negative:
printf("\nBoth numbers are negative");
return 0;
}
Output:
Write a program to create the file "Shiv.txt" and write text "Welcome to my file".
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *ptr;
ptr=fopen("D:\\Shiv.txt","w");
if (ptr==NULL)
{
printf("\nFile is not found");
exit(1);
}
else
{
printf("\nFile has been successfully created");
}
fputs("\nWel-come to my file",ptr);
fclose(ptr);
getch();
}
Output:
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
char s[100];
FILE *ptr;
ptr=fopen("D:\\test.txt","r");
if (ptr==NULL)
{
printf("\nFile cannot be opened");
exit(1);
}
fgets(s,100,ptr);
printf("\nThe content of the file is:\t%s",s);
fclose(ptr);
getch();
}
Output:
Program:
#include <stdio.h>
int main()
{
int n;
printf("Enter a number: ");
scanf("%d", &n);
if (n < 2)
{
printf("There are no even numbers less than 2.\n");
return 0;
}
printf("Even numbers from 2 to %d:\n", n);
for (int i = 2; i <= n; i++)
{
if (i % 2 != 0)
{
continue;
}
printf("%d ", i);
}
return 0;
}
utput:O
Program:
#include <stdio.h>
int main()
{
printf("First 10 even numbers:\n");
int i;
for ( i = 2; i <= 20; i += 2)
{
if (i%2==0)
{
printf("%d ", i);
}
}
return 0;
}
Output:
Program:
#include<stdio.h>
#include<conio.h>
long fib(int);
void main()
{
int terms,i;
printf("\nEnter the terms you want to print the fabonacci series:\t");
scanf("%d",&terms);
for(i=1;i<=terms;i++)
printf("\t%d",fib(i));
getch();
}
long int fib(int x)
{
if (x==1)
{
return 0;
}
else if (x==2)
{
return 1;
}
else
{
return (fib(x-1)+fib(x-2));
}
}
Output:
Program:
#include <stdio.h>
int main()
{
int n;
printf("Enter the last number for series: ");
scanf("%d", &n);
printf("\nSeries from 1 to %d:\n", n);
for (int i = 1; i <= n; i++)
{
printf("%d ", i);
}
printf("\n");
return 0;
}
Output:
Write a program to read integer type of element from keyboard and store them in array
and also display them.
Program:
#include <stdio.h>
#include <conio.h>
int main()
{
int arr[5];
int i;
getch();
return 0;
}
Output:
Write a program to print your name for five times using for loop.
Program:
#include <stdio.h>
int main()
{
for (int i = 0; i < 5; i++)
{
printf("Shiv Prasad Sharma\n");
}
return 0;
}
Output:
Program:
#include <stdio.h>
int main()
{
int arr[100];
int i, n;
printf("Enter the number of elements you want to insert: ");
scanf("%d", &n);
Output:
Program:
#include <stdio.h>
int main() {
int n, i;
printf("Enter the number: ");
scanf("%d", &n);
i = 1;
printf("Multiplication table of %d:\n", n);
while (i <= 10) {
printf("%d * %d = %d\n", n, i, n * i);
i++;
}
return 0;
}
Output:
Write a program to find the factorial of any number using recursive function.
Program:
#include<stdio.h>
#include<conio.h>
long int fact(int);
void main()
{
int num;
long int factorial;
printf("\nEnter the number you want to find the factorial of:");
scanf("%d",&num);
factorial=fact(num);
printf("\nThe factorial of %d is: %d",num,factorial);
getch();
}
long int fact(int x)
{
if (x==1)
{
return 1;
}
else
{
return (x*fact(x-1));
}
}
Output:
Write a program to find the name of day by number using switch case statement.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
printf("Enter the number you want to display the day: ");
scanf("%d",&n);
switch(n)
{
case 1:
printf("\nIt's Sunday");
break;
case 2:
printf("\nIt's Monday");
break;
case 3:
printf("\nIt's Tuesday");
break;
case 4:
printf("\nIt's Wednesday");
break;
case 5:
printf("\nIt's Thursday");
break;
case 6:
printf("\nIt's Friday");
break;
case 7:
printf("\nIt's Saturday");
default:
printf("\nYou enter wrong choice");
}
getch();
return (0);
}
Write a program to result sheet with total mark, percentage and division.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
float nep, eng, math, phy, chem, bio, comp;
printf("Enter the mark in Nepali:");
scanf("%f",&nep);
printf("Enter the mark in English:");
scanf("%f",&eng);
printf("Enter the mark in Math:");
scanf("%f",&math);
printf("Enter the mark in Physical:");
scanf("%f",&phy);
printf("Enter the mark in Chemestry:");
scanf("%f",&chem);
printf("Enter the mark in Biology:");
scanf("%f",&bio);
printf("Enter the mark in Computer:");
scanf("%f",&comp);
int total;
total = nep+eng+math+phy+chem+bio+comp;
printf("\nYour total mark is:%d",total);
int percentage;
percentage = (nep+eng+math+phy+chem+bio+comp)/7;
printf("\nYour percentage is:%d",percentage);
if(percentage>=80)
{
printf("\nYou Score Distinction.");
}
else if(percentage>=60)
{
printf("\nYou Score First Division.");
}
else if(percentage>=40)
{
printf("\nYou Score Second Division.");
Output:
Program:
#include<stdio.h>
#include<conio.h>
void swap(int*,int*);
void main()
{
int x,y;
x=50; y=70;
printf("\nBefore function calling, a and b are: %d and %d",x,y);
swap(&x,&y);
printf("\nAfter function calling, a and b are: %d and %d",x,y);
getch();
return 0;
}
void swap(int *a, int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
return 0;
}
Output:
Program:
#include <stdio.h>
#include <conio.h>
int main() {
int x = 50, y = 70;
getch();
return 0;
}
Output:
Program:
#include<stdio.h>
#include<conio.h>
struct student
{
char name[30];
int roll;
float marks;
};
int main()
{
struct student s[3];
int i;
for (i=0;i<3;i++)
{
printf("Enter the name, roll number, marks of student: ");
scanf("%s %d %f",s[i].name,&s[i].roll,&s[i].marks);
}
printf("\nInputed details are: ");
for(i=0;i<3;i++)
{
printf("\nName: %s",s[i].name);
printf("\nRoll: %d",s[i].roll);
printf("\nMarks: %0.2f",s[i].marks);
}
getch();
}
Output:
Program:
#include <stdio.h>
int main()
{
FILE *file;
char str[100];
file = fopen("output.txt", "w");
if (file == NULL)
{
printf("Error opening file!\n");
return 1;
}
printf("Enter a string to write to the file: ");
fgets(str, sizeof(str), stdin);
fprintf(file, "%s", str);
fclose(file);
printf("String successfully written to file.\n");
return 0;
}
Output:
Program:
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
int length = strlen(str);
printf("String in reverse order: ");
for (int i = length - 1; i >= 0; i--) {
printf("%c", str[i]);
}
printf("\n");
return 0;
}
Output:
Write a program to read the name and mark of n numbers of student and store them in a
file.
Program:
#include <stdio.h>
int main() {
FILE *file;
char filename[100];
int n,i;
printf("Enter the filename to store student details: ");
scanf("%s", filename);
file = fopen(filename, "w");
if (file == NULL) {
printf("Error opening file!\n");
return 1;
}
printf("Enter the number of students: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
char name[100];
int marks;
return 0;
}