Programming Lab (MS-02

)
S.No Program name Signature Remarks

1. Write a program to fine sum and average of two
numbers.

2. Write a program to calculate simple interest.

3. Write a program to print memory occupied by
int ,float and char variables.

4. Write a program to check palindrome number.

5 Write a program for changing value of two
variable without use of third variable.

6 Write a program to print all prime numbers from
1 to N.

7 Write a program to check whether number is
POSITIVE, NEGATIVE OR ZERO until user doesn't
want to exit.
8 Write a program to swap two numbers using
pointers.

9 Write a program to demonstrate example of array
of pointers.

10 Write a program to demonstrate example of array
of pointers.
1. Write a program to fine sum and average of two numbers.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,sum,avg;
printf("Enter two numbers");
scanf("%d%d",&a,&b);
sum=a+b;
avg=(a+b)/2;
printf("sum=%d average=%d",sum,avg);
getch();
}

OUTPUT:
2. Write a program to calculate simple interest.
#include<stdio.h>
#include<conio.h>
void main()
{
float p,r,t,si;
clrscr();
printf("Enter p,r,t");
scanf("%f%f%f",&p,&r,&t);
si=(p*r*t)/100;
printf("simple Interest = %f",si);
getch();
}

OUTPUT:
3. Write a program to print memory occupied by int ,float and char variables.

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
float f;
char c;
int m;
clrscr();
m=sizeof(i);
printf("\nMemory occumpied by integer =%d",m);
m=sizeof(f);
printf("\nMemory occumpied by float =%d",m);
m=sizeof(c);
printf("\nMemory occumpied by character =%d",m);
getch();
}

OUTPUT:
4. Write a program to check palindrome number.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,b,a;
printf("Enter a number");
scanf("%d",&n);
a=n;
b=0;
while(n!=0)
{
r=n%10;
b=b*10+r;
n=n/10;
}
if(b==a)
{
printf("Number is palindrom");
}
else
{
printf("Number is not palindrom");
}
getch(); }

OUTPUT:
5. Write a program for changing value of two variable without use of third
variable.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
printf("Enter two number");
scanf("%d%d",&a,&b);
printf("\nBefore swapping\na= %d b=%d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("\nAfter swapping\na= %d b=%d",a,b);
getch();
}

OUTPUT:
6. Write a program to print all prime numbers from 1 to N.
#include <stdio.h>
#include <conio.h>
int main()
{
int i, j, n, p;
clrscr();
printf("Enter limit");
scanf("%d", &n);
printf("\nAll prime numbers between 1 to %d are:\n", n);
for(i=2; i<=n; i++)
{
p = 1;
for(j=2; j<=i/2; j++)
{
if(i%j==0)
{
p = 0;
break;
}
}
if(p==1)
{
printf("%d\t", i);
}
}
getch();
}
7. Write a program to check whether number is POSITIVE, NEGATIVE OR
ZERO until user doesn't want to exit.

#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
int a;
clrscr();
do
{
printf("\n Enter a number \n ");
scanf("%d",&a);
if(a==0)
{
printf("ZERO");
}
else if(a>0)
{
printf("PPOSITIVE");
}
else
{
printf("Negative");
}
printf("\n do you wish to continue");
ch=getche();
}

while(ch=='y');
getch();
}
8. Write a program to swap two numbers using pointers.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,temp;
int *p,*q;
clrscr();
printf("Enter two numbers");
scanf("%d%d",&a,&b);
printf("\n Before sawping a=%d b=%d",a,b);
p=&a;
q=&b;
temp=*p;
*p=*q;
*q=temp;
printf("\n After sawping a=%d b=%d",a,b);
getch();
}

OUTPUT:
9. Write a program to read and print student detail using structure pointer,
demonstrate example of structure with pointer.

#include<stdio.h>
#include<conio.h>
struct student
{
int roll;
int marks;
char *name;
};
void main()
{
struct student *p,a;
clrscr();
p=&a;
printf("Enter roll,marks and name of student");
scanf("%d%d%s",&p->roll,&p->marks,p->name);
printf("Roll=%d Marks= %d name= %s",p->roll,p->marks,p->name);
getch();
}

OUTPUT:
10. Write a program to demonstrate example of array of pointers.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[5]={4,23,6,9,10};
int i;
int *p;
clrscr();
p=a;
for(i=0;i<5;i++)
{
printf("\n %d",*p);
p++;
}
getch();
}

OUTPUT: