Pwc Journal [Fcs2324011]
Pwc Journal [Fcs2324011]
CERTIFICATE
Prof. In-Charge
Dr ManojKumar Singh
(PROGRAMMING WITH C)
Examination date:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,add=0,sub=0,mul;
float div;
clrscr();
printf("Enter the numbers for operation:-");
scanf("%d %d",&a,&b);
add=a+b;
sub=a-b;
mul=a*b;
div=a/b;
printf("The additiion of two numbers is :- %d\n",add);
printf("the subtraction of two number is :- %d\n",sub);
printf("The multiplication of two numbers is :-%d\n",mul);
printf("The division of two numbers is :- %f",div);
getch();
}
Output:-
#include<stdio.h>
#include<conio.h>
void main()
{
int x;
float l,b,s,r,area;
clrscr();
printf("Enter the digits for which you have to perform task\n");
printf("1.Area of rectangle .\n2.Area of square.\n3. Area of circle.\n");
scanf("%d",&x);
switch(x){
case 1:
printf("Enter the length and breadth of the rectangle:-");
scanf("%f%f",&l,&b);
area=l*b;
printf("The area of the rectnagle is:- %funit sqr\n",area);
break;
case 2:
printf("Enter the side of a square:-\n");
scanf("%f",&s);
area=s*s;
printf("The area of the square is :- %f unit sqr\n",area);
break;
case 3:
printf("Enter the radius of the circle :-\n");
scanf("%f",&r);
area=3.14*r*r;
printf("The area of the circle is :- %f unit sqr\n",area);
break;
default:printf("Invalid input");
break;
}getch();
}
Output:-
#include<stdio.h>
#include<conio.h>
void main()
{
int x;
float a,r,h,vol;
clrscr();
printf("Enter the digit for the operation you have to do\n");
printf("1. Volume of cube. \n2. Colume of sphere.\n3. Volume of cylinder.\n");
scanf("%d",&x);
switch(x)
{
case 1:
printf("Enter the side of a cube:-\n");
scanf("%f",&a);
vol=a*a*a;
printf("The volume of the cuibe is :- %f unit cube",vol);
break;
case 2:
printf("Enter the radius pf sphere:-\n");
scanf("%f",&r);
vol= 1.67*3.14*r*r*r;
printf("The volume of sphere is :- %f unit cube\n",vol);\
break;
case 3:
printf("Enter the radius and height of the cylinder :-\n");
scanf("%f%f",&r,&h);
vol=3.14*r*r*h;
printf("The volume of the cylinder is:- %f unit cube\n",vol);
break;
default :
printf("Invalid input\n");
break;
}
getch();
}
Output:-
Practical 2: - Demonstrate input and output function
A. Programs to input and output functions
Code:-
#include<stdio.h>
#include<conio.h>
void main()
{
int n1=0,n2=0;
clrscr();
printf("Enter the 2 numbers :-");
scanf("%d %d",&n1,&n2);
printf("The addition of the number %d and %d is :-%d",n1,n2,n1+n2);
getch();
}
Output:-
Practical 3: - Conditional statements and loops
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int x;
clrscr();
printf("Enter the number to check:-");
scanf("%d",&x);
if (x%2==0){
printf("The given numvber is even ");}
else{
printf("The given number is odd");}
getch();
}
Output:
#include<stdio.h>
#include<conio.h>
void main()
{
int x;
clrscr();
printf("Enter the number to check:-");
scanf("%d",&x);
if (x>0){
printf("The given number is positive"); }
else if (x<0){
printf("The given number is negative");}
else{
printf("The number is zero");}
getch();
}
Output:
C. Write a program to find a sum of squares of a digits of a number
Code:-
#include<stdio.h>
#include<conio.h>
void main()
{
int x,sum=0,r;
clrscr();
printf("Enter the number:-");
scanf("%d",&x);
while (x!=0){
r=x%10;
sum+=r*r;
x=x/10;}
printf("The sum of square of the digits is :- %d",sum);
getch();
}
Output:
#include<stdio.h>
#include<conio.h>
void main()
{
int x,r,rev=0;
clrscr();
printf("Enter the number:-");
scanf("%d",&x);
while (x!=0){
r=x%10;
rev=rev*10+r;
x=x/10;}
printf("The reverse of the number is :- %d",rev);
getch();
}
Output:
#include<stdio.h>
#include<conio.h>
void main()
{
int f(int);
int x,result=0;
clrscr();
printf("Enter the number to find the square");
scanf("%d",&x);
result=f(x);
printf("The square of the given number is :- %d",result);
getch();
}
int f(int x){
int z;
z=x*x;
return z;}
Output:
#include<stdio.h>
#include<conio.h>
void main()
{
int fact(int);
int x,result=0;
clrscr();
printf("Enter the number:-");
scanf("%d",&x);
result=fact(x);
printf("The factoral of the given number %d is %d ", x, result );
getch();
}
int fact(int x){
if (x==1){
return 1;}
else{
return x*fact(x-1);}}
Output:
#include<stdio.h>
#include<conio.h>
void main()
{
int sum(int);
int x,result=0;
clrscr();
printf("Enter the number:-");
scanf("%d",&x);
result= sum(x);
printf("The sum of the given %d natural numbers is %d",x,result);
getch();
}
int sum(int x){
if (x==1){
return 1;}
else{
return x+sum(x-1);}}
Output:
Practical 6 : - Arrays
A. Write a program to find a largest value stored in array.
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int x[5],max=0,i=0;
clrscr();
for (i=1;i<=5;i++){
printf("Enter the %dst number :- ",i);
scanf("%d",&x[i]);}
max=x[1];
for (i=2;i<=5;i++){
if (max<x[i]){
max=x[i];}}
printf("The largest number is :- %d",max);
getch();
}
Output:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],n,i,temp,j;
clrscr();
printf("Enter the size of the array:-");
scanf("%d",&n);
for (i=1;i<=n;i++){
printf("Enter the %dst digit:-",i);
scanf("%d",&a[i]);}
for (j=1;j<=n;j++){
for (i=1;i<=n;i++){
if (a[i]>a[i+1]){
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;}}}
printf("The sorted array is:-\n");
for (i=1;i<=n;i++){
printf("%d\n",a[i]);}
getch();
}
Output:
Practical 7: - Pointers
A. Write a programs to demonstrate the use of pointers.
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int num=10;
int*ptr;
clrscr();
ptr=#
Output:
Output:
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
int n;
clrscr();
fp=fopen("Number.dat","w");
if (fp==NULL){
printf("Can't open file");
exit(0);}
printf("Enter an Integer");
scanf("%d",&n);
fprintf(fp,"%d",n);
fclose(fp);
getch();}
Output:
B. Write a program to open a file , read from it, and close a file .
Code:
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
int n;
clrscr();
fp=fopen("Number.dat","r");
if (fp==NULL){
printf("n't open file");
exit(0);}
fscanf(fp,"%d",&n);
printf("Entered number is : %d",n);
getch();}
Output:
C. Write a program to read the name and marks pf ‘n’ number of students and
store them in a file.
Code:
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
int n,i,marks;
char name[50];
clrscr();
fp=fopen("students_record.txt","w");
if (fp==NULL){
printf("Can't open the file ");
exit(0);}
printf("Enter the number of students :-");
scanf("%d",&n);
for (i=1;i<=n;i++){
printf("Enter the name of the %dst student:-",i);
scanf("%s",&name);
printf("Enter the mark of the %dst student:-",i);
scanf("%d",&marks);
fprintf(fp,"%s\t%d\n",name,marks);}
printf("Data is successfully written to the file \n");
fclose(fp);
getch();
}
Output: