Вы находитесь на странице: 1из 29

CONTENTS

S.N TOPIC
O
1. Write a c++ program that input's a student's marks in five subjects (out
of 100) and print the percentage
2. Write a program to find largest of three numbers

Write a program to print the Fibonacci series of specified terms

WRITE A PROGRAM TO CHECK WHETHER A YEAR IS A LEAP YEAR OR NOT

Write a program to check whether a number is palindrome or not

Write a program to check whether the number is prime or not

Write a program to swap two numbers


Write a program to swap two strings

Write a program to calculate the string length

Write a program to find the maximum element in an array

write a program to enter to insert an element in an array

Write a program to delete an element in an array

Write an program to implement linear search in an array list

Write a program to implement binary search in an array list

Write a program to arrange elements of an array list in ascending order


using bubble sort
Write a program to arrange elements of an array list in ascending order
using insertion sort
Write a program to add two matrices.

Write a program to multiply two matrices

Write a program to transpose two matrices

Write a program to create a file using file stream


1. Write a c++ program that input's a student's marks in five subjects (out
of 100) and print the percentage.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int mark[5], i;
float sum=0;
cout<<"Enter marks obtained in Physics, Chemistry, Maths, CS, English :";
for(i=0; i<5; i++)
{
cin>>mark[i];
sum=sum+mark[i];
}
float avg=sum/5;
float perc;
perc=(sum/500)*100;
cout<<"Average Marks = "<<avg;
cout<<"\nPercentage = "<<perc<<"%";
getch();
}

OUTPUT
2. Write a program to find largest of three numbers.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a, b, c, big;
cout<<"Enter three numbers : ";
cin>>a>>b>>c;
//let a is the biggest
big=a;
if(big<b)
{
if(b>c)
{
big=b;
}
else
{
big=c;
}
}
else if(big<c)
{
if(c>b)
{
big=c;
}
else
{
big=b;
}
}
else
{
big=a;
}
cout<<"Biggest number is "<<big;
getch();
}

OUTPUT
3. Write a program to print the Fibonacci series of specified terms.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a=0, b=1, c=0, limit;
cout<<"Upto How many term ? ";
scanf("%d",&limit);
cout<<"Fabonacci Series : "<<a<<" "<<b<<" "; // first two term
c=a+b;
limit=limit-2; // decrease the limit by 2. since two numbers already
printed
while(limit)
{
cout<<c<<" ";
a=b;
b=c;
c=a+b;
limit--;
}
getch();
}

OUTPUT
4. WRITE A PROGRAM TO CHECK WHETHER A YEAR IS A LEAP YEAR OR
NOT.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int yr;
cout<<"Enter year :";
cin>>yr;
if((yr%4==0) && (yr%100!=0))
{
cout<<"This is a Leap Year";
}
else if(yr%100==0)
{
cout<<"This is not a Leap Year";
}
else if(yr%400==0)
{
cout<<"This is a Leap Year";
}
else
{
cout<<"This is not a Leap Year";
}
getch();
}

OUTPUT

5. Write a program to check whether a number is palindrome or not.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num, rem, orig, rev=0;
cout<<"Enter a number : ";
cin>>num;
orig=num;
while(num!=0)
{
rem=num%10;
rev=rev*10 + rem;
num=num/10;
}
if(rev==orig) // check if original number is equal to its reverse
{
cout<<"Palindrome";
}
else
{
cout<<"Not Palindrome";
}
getch();
}

OUTPUT

6. Write a program to check whether the number is prime or not.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num,i,count=0;
cout<<"Enter a number:";
cin>>num;
for(i=2;i<num;i++)
{
if(num%i==0)
{
count++;
break;
}
}
if(count==0)
{
cout<<"This is a prime number";
}
else
{
cout<<"This is not a prime number";
}
getch();
}
OUTPUT

7. Write a program to swap two numbers.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num1, num2, swap;
cout<<"Enter two number : ";
cout<<"\nFirst Number : ";
cin>>num1;
cout<<"Second Number : ";
cin>>num2;
swap=num1;
num1=num2;
num2=swap;
cout<<"The value of first and second number after swapping is \n";
cout<<"First Number = "<<num1<<"\n"<<"Second Number = "<<num2;
getch();
}

OUTPUT
8. Write a program to swap two strings.

#include <stdio.h>
#include <string.h>
#include <malloc.h>

int main()
{
char first[100], second[100], *temp;

printf("Enter the first string\n");


gets(first);

printf("Enter the second string\n");


gets(second);

printf("\nBefore Swapping\n");
printf("First string: %s\n",first);
printf("Second string: %s\n\n",second);

temp = (char*)malloc(100);

strcpy(temp,first);
strcpy(first,second);
strcpy(second,temp);

printf("After Swapping\n");
printf("First string: %s\n",first);
printf("Second string: %s\n",second);
return 0;
}

OUTPUT

9. Write a program to calculate the string length.

#include <stdio.h>
#include <string.h>

int main()
{
char a[100];
int length;

printf("Enter a string to calculate it's length\n");


gets(a);

length = strlen(a);

printf("Length of entered string is = %d\n",length);

return 0;
}

OUTPUT
10. Write a program to calculate the string length.

#include <stdio.h>
#include <string.h>

int main()
{
char a[100], b[100];

printf("Enter the string to check if it is a palindrome\n");


gets(a);

strcpy(b,a);
strrev(b);

if (strcmp(a,b) == 0)
printf("Entered string is a palindrome.\n");
else
printf("Entered string is not a palindrome.\n");

return 0;
}

OUTPUT
11. Write a program to find the maximum element in an array.

#include <stdio.h>

int main()
{
int array[100], maximum, size, c, location = 1;

printf("Enter the number of elements in array\n");


scanf("%d", &size);

printf("Enter %d integers\n", size);

for (c = 0; c < size; c++)


scanf("%d", &array[c]);

maximum = array[0];

for (c = 1; c < size; c++)


{
if (array[c] > maximum)
{
maximum = array[c];
location = c+1;
}
}
printf("Maximum element is present at location %d and it's value is %d.\n",
location, maximum);
return 0;
}

OUTPUT

12. write a program to enter to insert an element in an array.

#include <stdio.h>

int main()
{
int array[100], position, c, n, value;

printf("Enter number of elements in array\n");


scanf("%d", &n);

printf("Enter %d elements\n", n);

for (c = 0; c < n; c++)


scanf("%d", &array[c]);

printf("Enter the location where you wish to insert an element\n");


scanf("%d", &position);

printf("Enter the value to insert\n");


scanf("%d", &value);

for (c = n - 1; c >= position - 1; c--)


array[c+1] = array[c];

array[position-1] = value;
printf("Resultant array is\n");

for (c = 0; c <= n; c++)


printf("%d\n", array[c]);

return 0;
}

OUTPUT

13. Write a program to delete an element in an array.

#include <stdio.h>

int main()
{
int array[100], position, c, n;

printf("Enter number of elements in array\n");


scanf("%d", &n);

printf("Enter %d elements\n", n);

for ( c = 0 ; c < n ; c++ )


scanf("%d", &array[c]);

printf("Enter the location where you wish to delete element\n");


scanf("%d", &position);

if ( position >= n+1 )


printf("Deletion not possible.\n");
else
{
for ( c = position - 1 ; c < n - 1 ; c++ )
array[c] = array[c+1];
printf("Resultant array is\n");

for( c = 0 ; c < n - 1 ; c++ )


printf("%d\n", array[c]);
}

return 0;
}

OUTPUT

14. Write an program to implement linear search in an array list.

#include <stdio.h>

int main()
{
int array[100], search, c, n;

printf("Enter the number of elements in array\n");


scanf("%d",&n);

printf("Enter %d integer(s)\n", n);

for (c = 0; c < n; c++)


scanf("%d", &array[c]);

printf("Enter the number to search\n");


scanf("%d", &search);

for (c = 0; c < n; c++)


{
if (array[c] == search) /* if required element found */
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if (c == n)
printf("%d is not present in array.\n", search);

return 0;
}

OUTPUT

15. Write a program to implement binary search in an array list.

#include <stdio.h>

int main()
{
int c, first, last, middle, n, search, array[100];

printf("Enter number of elements\n");


scanf("%d",&n);

printf("Enter %d integers\n", n);

for (c = 0; c < n; c++)


scanf("%d",&array[c]);

printf("Enter value to find\n");


scanf("%d", &search);

first = 0;
last = n - 1;
middle = (first+last)/2;

while (first <= last) {


if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;

middle = (first + last)/2;


}
if (first > last)
printf("Not found! %d is not present in the list.\n", search);

return 0;
}

OUTPUT

16. Write a program to arrange elements of an array list in ascending order using
bubble sort.

#include <stdio.h>

int main()
{
int array[100], n, c, d, swap;

printf("Enter number of elements\n");


scanf("%d", &n);

printf("Enter %d integers\n", n);

for (c = 0; c < n; c++)


scanf("%d", &array[c]);

for (c = 0 ; c < ( n - 1 ); c++)


{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}

printf("Sorted list in ascending order:\n");

for ( c = 0 ; c < n ; c++ )


printf("%d\n", array[c]);

return 0;
}

OUTPUT

17. Write a program to arrange elements of an array list in ascending order


using insertion sort.

#include <stdio.h>

int main()
{
int n, array[1000], c, d, t;

printf("Enter number of elements\n");


scanf("%d", &n);

printf("Enter %d integers\n", n);

for (c = 0; c < n; c++) {


scanf("%d", &array[c]);
}

for (c = 1 ; c <= n - 1; c++) {


d = c;

while ( d > 0 && array[d] < array[d-1]) {


t = array[d];
array[d] = array[d-1];
array[d-1] = t;

d--;
}
}

printf("Sorted list in ascending order:\n");

for (c = 0; c <= n - 1; c++) {


printf("%d\n", array[c]);
}

return 0;
}

OUTPUT

18. Write a program to add two matrices.

#include <stdio.h>

int main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];

printf("Enter the number of rows and columns of matrix\n");


scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");

for (c = 0; c < m; c++)


for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);

printf("Enter the elements of second matrix\n");

for (c = 0; c < m; c++)


for (d = 0 ; d < n; d++)
scanf("%d", &second[c][d]);

printf("Sum of entered matrices:-\n");


for (c = 0; c < m; c++) {
for (d = 0 ; d < n; d++) {
sum[c][d] = first[c][d] + second[c][d];
printf("%d\t", sum[c][d]);
}
printf("\n");
}

return 0;
}

OUTPUT

19. Write a program to multiply two matrices.

#include <stdio.h>

int main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];

printf("Enter the number of rows and columns of first matrix\n");


scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");

for (c = 0; c < m; c++)


for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);

printf("Enter the number of rows and columns of second matrix\n");


scanf("%d%d", &p, &q);

if (n != p)
printf("Matrices with entered orders can't be multiplied with each other.\n");
else
{
printf("Enter the elements of second matrix\n");

for (c = 0; c < p; c++)


for (d = 0; d < q; d++)
scanf("%d", &second[c][d]);

for (c = 0; c < m; c++) {


for (d = 0; d < q; d++) {
for (k = 0; k < p; k++) {
sum = sum + first[c][k]*second[k][d];
}

multiply[c][d] = sum;
sum = 0;
}
}

printf("Product of entered matrices:-\n");

for (c = 0; c < m; c++) {


for (d = 0; d < q; d++)
printf("%d\t", multiply[c][d]);

printf("\n");
}
}

return 0;
}

OUTPUT
20. Write a program to transpose two matrices.

#include <stdio.h>

int main()
{
int m, n, c, d, matrix[10][10], transpose[10][10];
printf("Enter the number of rows and columns of matrix\n");
scanf("%d%d", &m, &n);

printf("Enter the elements of matrix\n");

for (c = 0; c < m; c++)


for(d = 0; d < n; d++)
scanf("%d",&matrix[c][d]);

for (c = 0; c < m; c++)


for( d = 0 ; d < n ; d++ )
transpose[d][c] = matrix[c][d];

printf("Transpose of entered matrix :-\n");

for (c = 0; c < n; c++) {


for (d = 0; d < m; d++)
printf("%d\t",transpose[c][d]);
printf("\n");
}

return 0;
}

OUTPUT

21. Write a program to create a file using file stream.

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
fstream file; //object of fstream class

//opening file "sample.txt" in out(write) mode


file.open("sample.txt",ios::out);

if(!file)
{
cout<<"Error in creating file!!!";
return 0;
}

cout<<"File created successfully.";

//closing the file


file.close();

return 0;
}

File created successfully.

22. Write a program to write and read in a text file named sample.txt.

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
fstream file; //object of fstream class
//opening file "sample.txt" in out(write) mode
file.open("sample.txt",ios::out);

if(!file)
{
cout<<"Error in creating file!!!"<<endl;
return 0;
}

cout<<"File created successfully."<<endl;


//write text into file
file<<"ABCD.";
//closing the file
file.close();

//again open file in read mode


file.open("sample.txt",ios::in);

if(!file)
{
cout<<"Error in opening file!!!"<<endl;
return 0;
}

//read untill end of file is not found.


char ch; //to read single character
cout<<"File content: ";

while(!file.eof())
{
file>>ch; //read single character from file
cout<<ch;
}

file.close(); //close file

return 0;
}

File created successfully.

File content: ABCD.

23. Write a program to demonstrate example of tellg() and tellp() functions.

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
fstream file;
//open file sample.txt in and Write mode
file.open("sample.txt",ios::out);
if(!file)
{
cout<<"Error in creating file!!!";
return 0;
}
//write A to Z
file<<"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//print the position
cout<<"Current position is: "<<file.tellp()<<endl;
file.close();

//again open file in read mode


file.open("sample.txt",ios::in);
if(!file)
{
cout<<"Error in opening file!!!";
return 0;
}
cout<<"After opening file position is: "<<file.tellg()<<endl;

//read characters untill end of file is not found


char ch;
while(!file.eof())
{
cout<<"At position : "<<file.tellg(); //current position
file>>ch; //read character from file
cout<<" Character \""<<ch<<"\""<<endl;
}

//close the file


file.close();
return 0;
}

Current position is: 26

After opening file position is: 0

At position : 0 Character "A"

At position : 1 Character "B"


At position : 2 Character "C"

At position : 3 Character "D"

At position : 4 Character "E"

At position : 5 Character "F"

At position : 6 Character "G"

At position : 7 Character "H"

At position : 8 Character "I"

At position : 9 Character "J"

At position : 10 Character "K"

At position : 11 Character "L"

At position : 12 Character "M"

At position : 13 Character "N"

At position : 14 Character "O"

At position : 15 Character "P"

At position : 16 Character "Q"

At position : 17 Character "R"

At position : 18 Character "S"

At position : 19 Character "T"

At position : 20 Character "U"

At position : 21 Character "V"

At position : 22 Character "W"

At position : 23 Character "X"

At position : 24 Character "Y"

At position : 25 Character "Z"

24. Assuming that a text file named FIRST.TXT contains some text written
into it, write a function named copyupper(), that reads the file FIRST.TXT
and creates a new file named SECOND.TXT contains all words from the
file FIRST.TXT in uppercase.

void copyupper()
{
ifstream fin;

fin.open("FIRST.TXT");
ofstream fout;
fout.open("SECOND.TXT");
char ch;
while(!fin.eof())
{
fin.get(ch);
ch=toupper(ch);
fout<<ch;
}
fin.close();
fout.close();
}