Pps Assignment

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

INDEX

Program Signature
S No.
1. Write a program to display “Hello World”

2. Write a Program to use mathematical


operators
3. Write a Program to check if the number is
Odd or Even
4. Write a Program to check if the year is a leap
year
5. Write a program to find the sum of first 20
natural numbers
6. Write a Program to evaluate the square root
of a number

7. Write a Program to create and display a 2D


matrix

8. Write a Program to print two matrices


multiplication
9. Write a Program to use function with the
method
(1) Call by value
(2) Call by reference
10. Using recursion concept:
(1) Write a Program to calculate the
factorial of a number
(2) Write a Program to create the
Fibonacci series
11. Write a Program to perform swapping using
pointers
12 Write a Program to perform various tasks in
File handling
OUTPUT
PROGRAM 1:-

WRITE A PROGRAM TO DISPLAY HELLO WORLD

SYNTAX:-

#include<stdio.h>
#include<conio.h>
void main()
{
printf(“Hello World”);
getch();
clrscr();
}
OUTPUT
PROGRAM 2:-
Write a Program to use mathematical operators
SYNTAX:-
#include<stdio.h>
#include<conio.h>
void main()
{ int x,y,z;
printf(“enter two numbers”);
scanf(“%d%d”,&x,&y);
z=x+y;
printf(“sum of numbers=%d\n”,z);
z=x-y;
printf(“subtraction of numbers=%d\n”,z);
z=x*y;
printf(“multiplication of numbers=%d\n”,z);
z=x/y;
printf(“division of numbers=%d\n”,z);
getch();
clrscr();
}
OUTPUT
PROGRAM 3:-
Write a Program to check if the number is Odd or Even
SYNTAX:-

#include<stdio.h>
#include<conio.h>
void main(){
int n;
printf(“Enter a number:”);
scanf(“%d”,&n);
if(n%2==0)
{printf(The number is Even”);}
else
{printf(“The number is Odd”);}
getch();
clrscr();
}
OUTPUT
PROGRAM 4:-
Write a Program to check if the year is a leap year
Syntax:-
#include<stdio.h>

#include<conio.h>

void main(){

int year;

printf(“Enter the year:”);

scanf(“%d”,&year);

if(year%4==0){

if(year%100!=0 ||year%400==0){

printf(“The year %d is a leap year”,year);

else{

printf(“The year %d is not a leap year”,year);

else{

printf(“The year %d is not a leap year”,year);

getch();clrscr();
}

OUTPUT
PROGRAM 5:-
Write a program to find the sum of first 20 natural numbers

SYNTAX:-

#include<stdio.h>
#include<conio.h>
void main(){
int i, sum=0;
for(i=0;i<=20;i++) sum += i ;
printf("sum of natural numbers from 1 to 20 = %d",sum);
getch();
clrscr();
}
OUTPUT
PROGRAM 6:-
Write a Program to evaluate the square root of a number
SYNTAX:-

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main(){
int n, sqroot;
printf(“enter a number: “);
scanf (“%d”,&n);
sqroot=sqrt(n);
printf ("Square root of %d is %d",n,sqroot);
getch();
clrscr();
}
OUTPUT
PROGRAM 7:-
Write a Program to create and display a 2D matrix
SYNTAX:-
#include<stdio.h>

#include<conio.h>

void main(){

int i, j,a[3][3];

for(i=0;i<3;i++){

printf("enter the row %d elements", i+1);

for(j=0;j<3; j++){

scanf("%d",&a[i][j]); }

printf("2D Matrix formed is: \n");

for(i=0;i<3; i++){

for(j=0; j<3; j++) {

printf("%d",a[i][j]);}

printf("\n");

}
getch();

clrscr();
}

OUTPUT
PROGRAM 8:-
Write a Program to print two matrices multiplication
SYNTAX:-
#include<stdio.h>
#include<conio.h>
Void main(){
int a[3][3],b[3][3],c[3][3]={0},I,j,k;
printf(“For the first matrix\n”);
for(i=0;i<3;i++){
printf(“Enter the row %d elements”,i+1);
for(j=0;j<3;j++){
scanf(“%d”,&a[i][j]);
}
}
printf(“\n For the second matrix\n”);
for(i=0;i<3;i++){
printf(“Enter the row %d elements”,i+1);
for(j=0;j<3;j++){
scanf(“%d”,&a[i][j]);
}
}

for(i=0;i<3;i++){
for(j=0;j<3;j++){
for(k=0;k<3;k++){
c[i][j]+=a[i][k]*b[k][j];
}
}
}
printf(“Matrix formed is:\n”);
for(i=0,i<3;i++){
for(j=0;j<3;j++){
printf(“%d”,c[i][j]);
}
printf(“\n”);
}
getch();clrscr();
}
OUTPUT
PROGRAM 9:-
Write a Program to use function with the method
(1) Call by value
(2) Call by reference
SYNTAX:-
#include<stdio.h>

#include<conio.h>

void callbyval(int n){

n+=50;

return; }

void calbyref(int *n){

*n +=50;

return; }

void main(){

int n;

printf(“Enter a number:”);

scanf(“%d”,&n);

callbyval(n);

printf(“After using call by value function, value of n=%d\n”,n);

callbyref(&n);
printf(“After using call by reference function, value of n=%d”,n);

getch(); clrscr();

OUTPUT
PROGRAM 10:-
(1). Write a Program to calculate the Factorial of a number
SYNTAX:-
#include<stdio.h>
#include<conio.h>
int recursion(int num){
if(num<=1) return1;
return num*recursion(num-1);
}
void main(){
int num,ans=0;
printf(“Enter the number:”);
scanf(“%d”,&num);
ans=recursion(num);
printf(“Factorial of %d is %d”,num,ans);
getch();
clrscr();
}

OUTPUT
PROGRAM 10:-
Write a Program to print the Fibonacci series
SYNTAX:-
#include<stdio.h>
#include<conio.h>
int fib(int n){
if(n<=1) return n;
return fib(n-1)+fib(n-2);
}
void main(){
int n,i;
printf(“Enter the number of terms you want to print:”);
scanf(“%d”,&n);
printf(“Fibonacci series formed is\n”);
for(i=0;i<n;i++){
if(i==n-1) printf(“%d”,fib(i));
else printf(“%d”,fib(i));
}
getch(); clrscr();
}

OUTPUT
PROGRAM 11:-
Write a Program to perform swapping using pointers
SYNTAX:-
#include<stdio.h>

#include<conio.h>

void swap(int*a,int*b){

Int temp= *a;

*a=*b;

*b= temp;

return;

void main(){

int a,b;
printf(“Enter the two numbers:”);

scanf(“%d%d”,&a&b);

printf(“Before swapping, value of a is %d and value of b is %d

\n”,a,b);

swap(&a,&b);
printf(“After swapping, value of a is %d and value of b is %d

\n”,a,b);
getch(); clrscr();

OUTPUT
PROGRAM 12:-
Write a program to perform various tasks in File handling.
SYNTAX:-
#include<stdio.h>

#include<conio.h>
void main(){

int num,num2;

FILE*fptr;

fptr=fopen(“filename.txt”,”w”);

printf(“Enter two number:”);

fputc(‘r’,fptr);

fgetc(fptr);

printf(“\n”);

scanf(“%d%d”,&num,&num2);
fprintf(fptr,”%d%d”,&num&num2);

fscanf(fptr,”%d”,num);

fclose(fptr);

getch(); clrscr();

You might also like