L-I
a program to find area of a triangle
#include<iostream.h>
void main()
{
int b,h,area;
cin>>b>>h;
area=1/2*b*h; solution:(1*b*h)/2
cout<<area;
}
------------------------------------------------------------------------------------------------------------------
L-II
C++ Program Write a Program to Enter Char or Number and Check its ASCII Code
#include <iostreem>
void main()
{
char ascii;
int numeric;
cout << "Give character: ";
cin >> ascii;
cout << "Its ascii value is: " <<int ((ascii) << endl; (solution=(int) ascii)
cout << "Give a number to convert to ascii: ";
cin >> numeric;
cout << "The ascii value of " << numeric << " is " << char (numeric) (solution=(char) numeric;)
}
(or)
Program to enter a string and find its length
#include <iostream.h>
#include <conio.h>
#include <string.h>
void main()
{
clrscr();
char x; solution=char x[10];int slength;
cout << "Enter the string : " << endl;
cin>>x;
str1en(x); slength=
getch(); cout << "The length of the string " << x << " is " << slength << "." << endl;
}
--------------------------------------------------------------------------------------------------------------
L-III
How to swap two numbers without using a temporary variable?(strictly with following operators only)
#include <iostreem.h>
void main(]
{
int x = 10, y = 5;
// Code to swap 'x' (1010) and 'y' (0101)
x = y*x; // x now becomes 15 (1111) ( solution : x = x * y; // x now becomes 50
y = y/x; // y becomes 10 (1010 ) y = x / y; // y becomes 10
x = y/x; // x becomes 5 (0101) x = x / y; // x becomes 5 )
cout<<"After Swapping: x ="<<x<<" y ="<< y;
}
----------------------------------------------------------------------------------------------------------------
L-IV
a program to convert from lower case to uppercase
#include<iostream.h>
void main()
{
char ch;
cin>>ch;
ch=(ch+22) *(ch-27) (solution=ch=ch-32)
cout<<ch;
}
-----------------------------------------------------------------------------------------------------------------
L-V
Program to enter an integer and print out its successor
#include <iostream.h>
#include <conio.h>
void main()
{
int x;
cout << "Enter an integer : ";
cin>>x;
cout << "The successor of " << x << " is ";
x=++x-x+x/x-- (solution=x++,clrscr()should be removed)
clrscr();
getch();
}
--------------------------------------------------------------------------------------------------------------------
L-VI
biggest of three numbers.
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int x,y,z,biggest;
cout << "Enter 3 integers : ";
cin>>x>>y>>z;
biggest=y<z?(y>z&&z:y):(x>z||x:z); (solution=biggest=x>y?(x>z?x:z):(y>z?y:z);)
cout << "The biggest integer out of the 3 integers you typed ";
cout << x << ", " << y << " & " << z << " is : " << "\n" << biggest << "\n";
getch();
}
-----------------------------------------------------------------------------------------------------------------------
L-VII
Program to switch between different cases
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int choice;
cout << "1. Talk" << endl;
cout << "2. Eat" << endl;
cout << "3. Play" << endl;
cout << "4. Sleep" << endl;
cout << "Enter your choice : " << endl;
cin>>choice;
switch(choice)
{
case1 : cout << "You chose to talk...talking too much is a bad habit." << endl;
break;
case2 : cout << "You chose to eat...eating healthy foodstuff is good." << end1; (solution=end 1 is given,space b/w case and label, break statement removed)
case3 : cout << "You chose to play...playing too much everyday is bad." << end1;
break;
case4 : cout << "You chose to sleep...sleeping enough is a good habit." << endl;
break;
default : cout << "You did not choose anything...so exit this program." << endl;
}
getch();
}
-------------------------------------------------------------------------------------------------------------
L-VIII
Program to enter 10 integers in a single-dimension array and then print out the array in ascending order.
#include <i0stream.h>
#include <coni.h>
void main()
{
clrscr();
int array[10],t;
for(int x=1;x<10;x++);
{
cout << "Enter Integer No. " << x+1 << " : " << endl;
cin>>array[x];
}
for (x=0;x<10;x++);
{
for(int y=0;y<=9;y++);
{
if(array[y]>=array[y+1])
{
t=array[y];
array[y]=array[y+1];
array[y+1]=t;
}
}
}
cout << "Array in ascending order is : ";
for (x=0;x<=10;x++);
cout << endl << array[y];
getch();
}
Solution:
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int array[10],t;
for(int x=0;x<10;x++)
{
cout << "Enter Integer No. " << x+1 << " : " << endl;
cin>>array[x];
}
for (x=0;x<10;x++)
{
for(int y=0;y<9;y++)
{
if(array[y]>array[y+1])
{
t=array[y];
array[y]=array[y+1];
array[y+1]=t;
}
}
}
cout << "Array in ascending order is : ";
for (x=0;x<10;x++)
cout << endl << array[x];
getch();
}
---------------------------------------------------------------------------------------------------------------------------------------
L-IX
Program to compute the fibonacci series
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int a,b,x,y,num1,ct;
a=1;
b=0;
cout << "Enter the number of terms (less than 25) : " << endl;
cin>>num1;
cout << a << endl;
cout << b << endl;
for(ct=1;ct<=num-2-1;ct++)
{
y=a+b;
y=a;
a=b;
b=x;
}
getch();
}
Solution:
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int a,b,x,y,num1,ct;
a=0;
b=1;
cout << "Enter the number of terms (less than 25) : " << endl;
cin>>num1;
cout << a << endl;
cout << b << endl;
for(ct=1;ct<=num1-2;ct++)
{
x=a+b;
cout << x << endl;
y=a;
a=b;
b=x;
}
getch();
}
-----------------------------------------------------------------------------------------------------------------------------------------------
L-X
Quadratic equation.
a program to find area of a triangle
#include<iostream.h>
void main()
{
int b,h,area;
cin>>b>>h;
area=1/2*b*h; solution:(1*b*h)/2
cout<<area;
}
------------------------------------------------------------------------------------------------------------------
L-II
C++ Program Write a Program to Enter Char or Number and Check its ASCII Code
#include <iostreem>
void main()
{
char ascii;
int numeric;
cout << "Give character: ";
cin >> ascii;
cout << "Its ascii value is: " <<int ((ascii) << endl; (solution=(int) ascii)
cout << "Give a number to convert to ascii: ";
cin >> numeric;
cout << "The ascii value of " << numeric << " is " << char (numeric) (solution=(char) numeric;)
}
(or)
Program to enter a string and find its length
#include <iostream.h>
#include <conio.h>
#include <string.h>
void main()
{
clrscr();
char x; solution=char x[10];int slength;
cout << "Enter the string : " << endl;
cin>>x;
str1en(x); slength=
getch(); cout << "The length of the string " << x << " is " << slength << "." << endl;
}
--------------------------------------------------------------------------------------------------------------
L-III
How to swap two numbers without using a temporary variable?(strictly with following operators only)
#include <iostreem.h>
void main(]
{
int x = 10, y = 5;
// Code to swap 'x' (1010) and 'y' (0101)
x = y*x; // x now becomes 15 (1111) ( solution : x = x * y; // x now becomes 50
y = y/x; // y becomes 10 (1010 ) y = x / y; // y becomes 10
x = y/x; // x becomes 5 (0101) x = x / y; // x becomes 5 )
cout<<"After Swapping: x ="<<x<<" y ="<< y;
}
----------------------------------------------------------------------------------------------------------------
L-IV
a program to convert from lower case to uppercase
#include<iostream.h>
void main()
{
char ch;
cin>>ch;
ch=(ch+22) *(ch-27) (solution=ch=ch-32)
cout<<ch;
}
-----------------------------------------------------------------------------------------------------------------
L-V
Program to enter an integer and print out its successor
#include <iostream.h>
#include <conio.h>
void main()
{
int x;
cout << "Enter an integer : ";
cin>>x;
cout << "The successor of " << x << " is ";
x=++x-x+x/x-- (solution=x++,clrscr()should be removed)
clrscr();
getch();
}
--------------------------------------------------------------------------------------------------------------------
L-VI
biggest of three numbers.
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int x,y,z,biggest;
cout << "Enter 3 integers : ";
cin>>x>>y>>z;
biggest=y<z?(y>z&&z:y):(x>z||x:z); (solution=biggest=x>y?(x>z?x:z):(y>z?y:z);)
cout << "The biggest integer out of the 3 integers you typed ";
cout << x << ", " << y << " & " << z << " is : " << "\n" << biggest << "\n";
getch();
}
-----------------------------------------------------------------------------------------------------------------------
L-VII
Program to switch between different cases
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int choice;
cout << "1. Talk" << endl;
cout << "2. Eat" << endl;
cout << "3. Play" << endl;
cout << "4. Sleep" << endl;
cout << "Enter your choice : " << endl;
cin>>choice;
switch(choice)
{
case1 : cout << "You chose to talk...talking too much is a bad habit." << endl;
break;
case2 : cout << "You chose to eat...eating healthy foodstuff is good." << end1; (solution=end 1 is given,space b/w case and label, break statement removed)
case3 : cout << "You chose to play...playing too much everyday is bad." << end1;
break;
case4 : cout << "You chose to sleep...sleeping enough is a good habit." << endl;
break;
default : cout << "You did not choose anything...so exit this program." << endl;
}
getch();
}
-------------------------------------------------------------------------------------------------------------
L-VIII
Program to enter 10 integers in a single-dimension array and then print out the array in ascending order.
#include <i0stream.h>
#include <coni.h>
void main()
{
clrscr();
int array[10],t;
for(int x=1;x<10;x++);
{
cout << "Enter Integer No. " << x+1 << " : " << endl;
cin>>array[x];
}
for (x=0;x<10;x++);
{
for(int y=0;y<=9;y++);
{
if(array[y]>=array[y+1])
{
t=array[y];
array[y]=array[y+1];
array[y+1]=t;
}
}
}
cout << "Array in ascending order is : ";
for (x=0;x<=10;x++);
cout << endl << array[y];
getch();
}
Solution:
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int array[10],t;
for(int x=0;x<10;x++)
{
cout << "Enter Integer No. " << x+1 << " : " << endl;
cin>>array[x];
}
for (x=0;x<10;x++)
{
for(int y=0;y<9;y++)
{
if(array[y]>array[y+1])
{
t=array[y];
array[y]=array[y+1];
array[y+1]=t;
}
}
}
cout << "Array in ascending order is : ";
for (x=0;x<10;x++)
cout << endl << array[x];
getch();
}
---------------------------------------------------------------------------------------------------------------------------------------
L-IX
Program to compute the fibonacci series
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int a,b,x,y,num1,ct;
a=1;
b=0;
cout << "Enter the number of terms (less than 25) : " << endl;
cin>>num1;
cout << a << endl;
cout << b << endl;
for(ct=1;ct<=num-2-1;ct++)
{
y=a+b;
y=a;
a=b;
b=x;
}
getch();
}
Solution:
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int a,b,x,y,num1,ct;
a=0;
b=1;
cout << "Enter the number of terms (less than 25) : " << endl;
cin>>num1;
cout << a << endl;
cout << b << endl;
for(ct=1;ct<=num1-2;ct++)
{
x=a+b;
cout << x << endl;
y=a;
a=b;
b=x;
}
getch();
}
-----------------------------------------------------------------------------------------------------------------------------------------------
L-X
Quadratic equation.
No comments:
Post a Comment