Professional Documents
Culture Documents
Write A C
Write A C
Write A C
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
#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
#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
#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
#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("\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
#include <stdio.h>
#include <string.h>
int main()
{
char a[100];
int length;
length = strlen(a);
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];
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;
maximum = array[0];
OUTPUT
#include <stdio.h>
int main()
{
int array[100], position, c, n, value;
array[position-1] = value;
printf("Resultant array is\n");
return 0;
}
OUTPUT
#include <stdio.h>
int main()
{
int array[100], position, c, n;
return 0;
}
OUTPUT
#include <stdio.h>
int main()
{
int array[100], search, c, n;
return 0;
}
OUTPUT
#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
first = 0;
last = n - 1;
middle = (first+last)/2;
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;
return 0;
}
OUTPUT
#include <stdio.h>
int main()
{
int n, array[1000], c, d, t;
d--;
}
}
return 0;
}
OUTPUT
#include <stdio.h>
int main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];
return 0;
}
OUTPUT
#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];
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");
multiply[c][d] = sum;
sum = 0;
}
}
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);
return 0;
}
OUTPUT
#include <iostream>
#include <fstream>
int main()
{
fstream file; //object of fstream class
if(!file)
{
cout<<"Error in creating file!!!";
return 0;
}
return 0;
}
22. Write a program to write and read in a text file named sample.txt.
#include <iostream>
#include <fstream>
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;
}
if(!file)
{
cout<<"Error in opening file!!!"<<endl;
return 0;
}
while(!file.eof())
{
file>>ch; //read single character from file
cout<<ch;
}
return 0;
}
#include <iostream>
#include <fstream>
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();
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();
}