Sei sulla pagina 1di 71

Delhi Technological

University

Programming Fundamental

COE-118

NAME- SIMRAN
ROLL NO.- DTU/2K13/B02/0305

BATCH - B02

S.NO

TOPIC

DATE

Q1

Write a program to find simple


interest

01-08-2013

Q2

Write a program to find Sum and


Average of 10 numbers

01-08-2013

Q3

Write a program to find greatest of


10 numbers.

08-08-2013

Q4

Write a program to find whether a


number is a prime number or not.

15-08-2013

Q5

Write a program to implement


switch-case statement.

15-08-2013

Write a program to find Fibonacci

Q6

22-08-2013

Q7

Write a program to find sum of


digits of a 5 digit number

22-08-2013

Q8

Write a program to find reverse of


a 5 digit number

29-08-2013

Write a program to find area and


perimeter of circle, rectangle,
square and triangle using function.

29-08-2013

Q9

CHECKED
ON

Q10

Write a program to find factorial of


a number using recursion.

05-09-2013

INDEX
S.NO

TOPIC

DATE

Q11

Write a program to find number of


vowels in the entered string.

05-09-2013

Q12

Q13
Q14

Q15
Q16

Q17

Write a program to check whether


the entered string is palindrome or
not, using pointers.
Write a program to convert upper
case to lower case and vice versa.
Write a program to covert decimal
number to binary number and vice
versa.
Write a program to implement
Bitwise operators.
Write a program to print following:
1
12
123
1234
12345
Write a program for addition of
two 3*3 matrices.

12-09-2013

12-09-2013

19-09-2013

19-09-2013

26-09-2013

26-09-2013

CHECKED
ON

Q18

Write a program to find the


multiplication of two 3*3 matrix.

26-09-2013

Q19

Write a program to search a


number from array of numbers.

03-10-2013

Q20

Write a program to generate


employee details using structures.

03-10-2013

S.NO

TOPIC

DATE

Q21

Write a program to sort an array


using insertion/selection sort .

03-10-2013

Q22

Write a program to pass and


return a pointer to a function.

10-10-2013

Q23

Write a program to pass array to a


function call() as pointer to
calculate the sum of all elements
of array.

Q24

Write a program to read a file and


after converting all lowercase to
upper case write to another file.

10-10-2013

10-10-2013

CHECKED
ON

Q25

Write a program to find the length


of a string without using string
functions. In this program call a
function slen() and pass a string of
characters.

17-10-2013

Q26

Write a program to implement


constructor & destructor.

17-10-2013

Q27

Write a program to overload


operator +.

17-10-2013

Write a program to find area of


different shapes using function
overloading.

24-10-2013

Write a program to implement


multiple inheritance.

24-10-2013

Define a class right angled


triangle and determine its area
and perimeter through two
separate interfaces(functions).

24-10-2013

Q28
Q29
Q30

Q1. Write a program to find simple interest?


#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
int time;
float principal,rate,simplei;
printf(\nEnter the principal amount: );
scanf(%f,& principal);
printf(\nPlease enter the rate of interest : );
scanf(%f,& rate);
printf(\nPlease enter the time in years : );
scanf(%d,& time);
s=(( principal * rate * time)/100);
printf(\nThe simple interest generated is :
%f, simplei);
getch();
}

Q2. Write a program to find Sum and Average


of 10 numbers?
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
float i, sum=0, avg, num;
printf("\nEnter 10 numbers: ");
for(i=0;i<10;i++)
{
scanf("%f",&num);
sum=sum+num;
}
printf("\nSum: ");
printf("%f",sum);
avg=sum/10;
printf("\nAverage: ");
printf("%f",avg);
getch();
}

Q3. Write a program to find greatest of 10


numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[10],i,great;
printf("\nEnter ten numbers:");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
great=a[0];
for(i=0;i<10;i++)
{
if(a[i]>=great)
great=a[i];
}
printf("The greatest is:%d",great);
getch();
}

Q4. Write a program to find whether a number


is a prime number or not
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
long int n;
int i=2,prime=1;
printf("\nEnter a number");
scanf("%d",&n);
while((i<=n/2) && (prime==1))
{
if(n%i==0)
prime=0;
else
i++;
}
if(prime==0)
printf("\n Number is not a prime no.");
else
printf("\n It's a prime no.");
getch();
}

Q5. Write a program to implement switch-case


statement.
#include<stdio.h>
#include<conio.h>
void main()
{clrscr();
int ch,a,b,c;
printf(" \t\t\t\tCALCULATOR\n\n \t\t THE MENU
IS : )
\t\n\t\t 1.
Add \n\t\t2. Substract\n\t\t3. Divide \n\t\t4.
Multiply\n\n Now..Enter the choice ");
scanf("%d",&ch);
printf("Enter 2 no.'s (where 1st is greater than
2nd) ");
scanf("%d\n%d",&a,&b);
switch(ch)
{case 1:
c=a+b;
printf(" SUM\t=\t%d",c);
break;
case 2:
{c=a-b;
printf(" DIFFERENCE\t=\t%d",c);}
break;
case 3:
{c=a/b;
printf(" QUOTIENT\t=\t%d",c);}
break;

case 4:

{c=a*b;
printf(" PRODUCT\t=\t%d",c);}
break;
default:
printf(" WRONG CHOICE : ( \n");
}
getch();
}

Q6. Write a program to find Fibonacci


#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
long int a[50], n, i;
a[0]=0;
a[1]=1;
printf("Enter number of digits: ");
scanf("%d",&n);
printf("%d\t%d\t",a[0],a[1]);
for(i=2;i<n;i++)
{
a[i]=a[i-1]+a[i-2];
printf("%d\t",a[i]);
}
getch();
}

Q7. Write a program to find sum of digits of a 5


digit number
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
int n ,sum=0 ,r;
printf("Enter a 5 digit number :");
scanf("%d",&n);
while (n!=0)
{
r=(n%10);
sum+=r;
n=(n/10);
}
printf("\nThe sum of the digits are :%d",sum);
getch();
}

Q8. Write a program to find reverse of a 5 digit


number
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n,q,r,sum=0;
printf("\nEnter 5 digit no.=");
scanf("%d",&n);
while(n!=0)
{
q=n/10;
r=n%10;
sum=(10*sum)+r;
n=q;
}
printf("\nReverse of no.=%d",sum);
getch();
}

Q9. Write a program to find area and perimeter of


circle, rectangle, square and triangle using function.
#include<math.h>
#include<stdio.h>
#include<conio.h>
void circle(float);
void rectangle(float,float);
void square(float);
void triangle(float,float,float);
void main()
{
float a,r,l,b,l1,l2,l3;
clrscr();
printf("\nPress 1: Area and circumference of circle");
printf("\nPress 2: Area and perimeter of rectangle");
printf("\nPress 3: Area and perimeter of square");
printf("\nPress 4: Area and perimeter of triangle\n");
scanf("%f",&a);
if(a==1)
{
printf("\nEnter radius of circle=");
scanf("%f",&r);
circle(r);
}
else if(a==2)
{
printf("\nEnter length and breadth of rectangle=");
scanf("%f",&l);

scanf("%f",&b);
rectangle(l,b);
}
else if(a==3)
{
printf("\nEnter side of square=");
scanf("%f",&l);
square(l);
}
else if(a==4)
{
printf("\nEnter sides of triangle=");
scanf("%f",&l1);
scanf("%f",&l2);
scanf("%f",&l3);
triangle(l1,l2,l3);
}
else
printf("\nINVALID CHOICE");
getch();
}
void circle(float r)
{
float area,cir;
cir=2*3.14*r;
area=3.14*r*r;
printf("\nArea of circle=%f",area);
printf("\nCircumference of circle=%f",cir);
}
void rectangle(float l,float b)
{
float area,peri;

peri=2*(l+b);
area=l*b;
printf("\nArea of rectangle=%f",area);
printf("\nPerimeter of rectangle=%f",peri);
}
void square(float l)
{
float area,peri;
peri= 4*l;
area=l*l;
printf("\nArea of square=%f",area);
printf("\nPerimeter of square=%f",peri);
}
void triangle(float l1,float l2,float l3)
{
float area,peri,p;
peri=l1+l2+l3;
p=peri/2;
area=sqrt(p*(p-l1)*(p-l2)*(p-l3));
printf("\nArea of triangle=%f",area);
printf("\nPerimeter of triangle=%f",peri);
}

Q10. Write a program to find factorial of a


number using recursion.
#include<conio.h>
#include<stdio.h>
long int fact(int);
void main()
{
long int a,f;
clrscr();
printf("\nEnter any number=");
scanf("%d",&a);
f=fact(a);
printf("%d! using recursion=%d",a,f);
getch();
}
long int fact(int x)
{
long int z;
if(x==1)
return(1);
else
{
z=x*fact(x-1);
return(z);
}
else
if(x==0)
return(1);
}

Q11. Write a program to find number of vowels


in the entered string.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char a[50];
int i,sum=0;
printf("\nEnter a string:");
gets(a);
printf("%s",a);
for(i=0;i<strlen(a);i++)
{
if(a[i]=='a'||a[i]=='A'||a[i]=='e'||a[i]=='E'||
a[i]=='i'||a[i]=='I'||a[i]=='o'||a[i]=='O'||a[i]=='u'||
a[i]=='U')
sum=sum+1;
}
printf("\nNumber of vowels=%d",sum);
getch();
}

Q12. Write a program to check whether the


entered string is palindrome or not, using
pointers.

Q13. Write a program to convert upper case to


lower case and vice versa.
#include <stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf("\nEnter a character : ");
scanf("%c", &ch);
if(ch>=65&&ch<=90)
ch=ch+32;
else if(ch>=97&&ch<=122)
ch=ch-32;
else
{
printf("Enter valid character");
getch();
exit(0);
}
printf("\nConverted character = %c", ch);
getch();
}

Q14. Write a program to covert decimal number


to binary number and vice versa.
#include<stdio.h>
#include<conio.h>
void main()
{
long num,numb,rem,i=1,j=0;
clrscr();
printf("Enter a decimal integer:-");
scanf("%d",&num);
while(num>0)
{
rem=num%2;
j=j+rem*i;
num=num/2;
i=i*10;
}
printf("\nBinary Equivalent= %d\n",j);
j=0;
i=1;
printf("\nEnter a binary number(1s and 0s):-");
scanf("%d",&numb);
while(numb>0)
{
rem=numb%10;
j=j+rem*i;
numb=numb/10;
i=i*2;
}
printf("\nDecimal equlvalent= %d",j);
getch();

Q15. Write a program to implement Bitwise operators.


#include<stdio.h>
#include<conio.h>
void main()
{
long int num,nnumb,numb,c,nnum;
clrscr();
printf("Enter a Number=");
scanf("%d",&num);
printf("Enter a Number=");
scanf("%d",&numb);
printf("\nMenu:\nPress 1 for Left Shift");
printf("\nPress 2 for Right Shift");
printf("\nPress 3 for OR operator");
printf("\nPress 4 for AND operator");
printf("\nPress 5 for XOR operator");
printf("\nEnter your choice:-");
scanf("%d",&c);
switch(c)
{
case 1:nnum=num<<1;
printf("Left shift of %d by 1= %d",num,nnum);
nnumb=numb<<1;
printf("\nLeft shift of %d by 1=
%d",numb,nnumb);break;
case 2:nnum=num>>1;
printf("Right shift of %d by 1= %d",num,nnum);
nnumb=numb>>1;

printf("\nRight shift of %d by 1=
%d",numb,nnumb);break;
case 3:nnum=num|numb;
printf("%d OR %d= %d",num,numb,nnum);break;
case 4:nnum=num&numb;
printf("%d AND %d=
%d",num,numb,nnum);break;
case 5:nnum=num^numb;
printf("%d XOR %d=
%d",num,numb,nnum);break;
default:printf("\nINVALID INPUT");break;
}
getch();
}

Q16. Write a program to print following:


1
12
123
1234
12345
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=0;i<5;i++)
{
printf("\n");
for(j=0;j<=i;j++)
printf("%d",j+1);
}
getch();
}

Q17. Write a program for addition of two 3*3


matrices.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[3][3],b[3][3],c[3][3],i,j;
printf("Enter elements of first array");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
}
printf("First array is:\n");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",a[i][j]);
}
printf("\nEnter elements of second array");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);
}
printf("Second array is:\n");
for(i=0;i<3;i++)

{
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",b[i][j]);

}
printf("\nNew array found by addition of two arrays:\n");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
printf("%d\t",c[i][j]);}
}
getch();
}

Q18. Write a program to find the multiplication


of two 3*3 matrix.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[3][3],b[3][3],c[3][3],i,j;
printf("Enter elements of matrix A:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{ printf("Enter element a%d%d : ",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
printf("\nEnter elements of matrix B:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{ printf("Enter element b%d%d : ",i+1,j+1);
scanf("%d",&b[i][j]);
}}
printf("\nDisplaying Matrix A:\n");
for(i=0;i<3;i++)
{ printf("\n");
for(j=0;j<3;j++)

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

printf("\nDisplaying Matrix B:\n");


for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
{ printf("%d\t",b[i][j]);
}}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{ c[i][j]=0;
for(int k=0;k<3;k++)
{ c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}}}
printf("\nMatrix A * Matrix B = Matrix C:\n");
for(i=0;i<3;i++)
{ printf("\n");
for(j=0;j<3;j++)
{ printf("%d\t",c[i][j]);
}}
getch();}

Q19. Write a program to search a number from


array of numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i;
int ele,temp=0,pos=0;
clrscr();
printf("\nEnter the array elements\n");
for (i=0;i<5;i++)
scanf("%d",&a[i]);
printf("Enter the element to be search\n");
scanf("%d",&ele);
for (i=0; i<5; i++)
{
if (a[i]==ele)
{
temp=1;
pos=i+1;
}
}
if (temp==1)
printf("\nElement found at position %d",pos);
else
printf("Element not found\n");
getch();
}

Q20. Write a program to generate employee


details using structures.
#include<stdio.h>
#include<conio.h>
struct add
{
int h_no,pin;
char city[20],block[5];
};
struct emp
{
float sal,age,code;
add a1;
char name[50];
};
void main()
{
int i,j;
emp e[20];
clrscr();
printf("\nEnter number of employees:");
scanf("%d",&j);
for(i=0;i<j;i++)
{
printf("\nEnter %d Employee detail",i+1);
printf("\nEnter Employee name:");
scanf("%s",e[i].name);
printf("Enter Employee code:");
scanf("%d",&e[i].code);
printf("Enter age of Employee:");

scanf("%d",&e[i].age);
printf("Enter Employee address\nEnter house number-");
scanf("%d",&e[i].a1.h_no);
printf("Enter block-");
scanf("%s",e[i].a1.block);
printf("Enter city-");
scanf("%s",e[i].a1.city);
printf("Enter pincode-");
scanf("%d",&e[i].a1.pin);
printf("Enter Employee salary:");
scanf("%d",&e[i].sal);
}
getch();
}

Q21. Write a program to sort an array using


selection sort .
#include<conio.h>
#include<stdio.h>
void main()
{int i,n,j,small=0,pos=0,temp,list[10];
clrscr();
printf("enter the value of n =\t");
scanf("%d",&n);
printf("enter the value of %d array members
=\t",n);
for(i=0;i<n;i++)
{scanf("%d",&list[i]);}
for(i=0;i<n-1;i++)
{small=list[i];
pos=i;
for(j=i+1;j<n;j++)
{if(small>list[j])
{small=list[i];
pos=j;}
}
temp=list[i];
list[i]=list[pos];
list[pos]=temp;
}
printf("\n the sorted list of array is =\t");
for(i=0;i<n;i++)
printf("%d\n",list[i]);
getch();
}

Q22. Write a program to pass and return a


pointer to a function.

#include<conio.h>
#include<stdio.h>
int *big(int *a,int *b)
{
if(a>b)
return a;
else
return b;
}
void main()
{
int *a1,*a2,*big;
clrscr();
printf(Enter the no.s=\t);
scanf(%d%d,&a1,&a2);
big=bigg(a1,a2);
printf(The bigger no. iz = \t%d,big);
getch();
}

Q23. Write a program to pass array to a


function call() as pointer to calculate the sum
of all elements of array.
#include<conio.h>
#include<stdio.h>
int fooadd(int a[],int size)
{
int i,*p;
int sum=0;
p=a;
for(i=0;i<size;i++)
{sum+=*p;
++p;
}
return sum;
}
void main()
{
int a[10],ln,i,s;
clrscr();
printf("Enter the length of array=\t");
scanf("%d",&ln);
printf("Enter the array=\t");
for(i=0;i<ln;i++)
scanf("%d",&a[i]);
s=fooadd(a,ln);
printf("The sum of array=\t%d",s);
getch();
}

Q24. Write a program to read a file and after


converting all lowercase to upper case write to
another file.
#include<stdio.h>
#include<process.h>
void main()
{
FILE *fp1,*fp2;
char a;
clrscr();
fp1=fopen("test.txt","r");
if(fp1==NULL)
{
puts("cannot open this file");
exit(1);
}
fp2=fopen("test1.txt","w");
if(fp2==NULL)
{
puts("Not able to open this file");
fclose(fp1);
exit(1);
}
do
{
a=fgetc(fp1);
a=toupper(a);
fputc(a,fp2);
}while(a!=EOF);
fcloseall();
getch();
}

Q25. Write a program to find the length of a


string without using string functions. In this
program call a function slen() and pass a string
of characters.
#include<conio.h>
#include<stdio.h>
int slen(char *str)
{
int i=0;
while(str[i++]!=0){}
return i-1;
}
void main()
{
int ln,i;
char *a="foobar";
clrscr();
ln=slen(a);
printf("THe string length is=\t%d\n",ln);
getch();
}

Q26. Write a program to implement constructor


& destructor.
#include<conio.h>
#include<iostream.h>
#include<stdio.h>
class mahclas
{
public:
int x;
mahclas();
~mahclas();
};
mahclas::mahclas()
{x=10;}
mahclas::~mahclas()
{cout<<"Destructing....\n";}
void main()
{
clrscr();
mahclas c1;
mahclas c2;
cout<<c1.x<<" "<<c2.x<<endl;
getch();
}

Q27. Write a program to overload operator +.

Q28. Write a program to find area of different


shapes using function overloading.

Q29. Write a program to implement multiple


inheritance.

Q30. Define a class right angled triangle and


determine its area and perimeter through two
separate interfaces(functions).