LECTURE NOTES ON PROGRAMMING FUNDAMENTAL USING C++ LANGUAGE By Dr. Samaher Hussein Ali

advertisement
The University of Babylon
Department of Software
LECTURE NOTES ON PROGRAMMING
FUNDAMENTAL USING C++ LANGUAGE
By
Dr. Samaher Hussein Ali
Collage of Information Technology, University of Babylon, Iraq
Samaher_hussein@yahoo.com
21 January 2013
1
Examples:
Q1//write a program to find febonatshy serial
1 1 2 3 5 8 13 21 34 …..
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,n,a=0,b=1,c;
cout<<" pls enter toaller sequnce : ";
cin>>n;
cout<<b;
for(i=0;i<n;i++)
{
c=b;
b=b+a;
a=c;
cout<<' '<<b;
}
getch();
}
21 January 2013
Dr. Samaher Hussein Ali
Notes of Lecture 62
Examples:
Q2//Calculator program
#include<iostream.h>
#include<conio.h>
void main()
{
Clrscr();
float a,b,c;
char sign;
cin>>a;
cin>> sign;
cin>>b;
switch (sign)
{
case '+': cout<<"="<<(c=a+b) <<endl ; break;
case '/': cout<<"="<<(c=a/b) <<endl ; break;
case '-': cout<<"="<<(c=a-b) <<endl ; break;
case '*': cout<<"="<<(c=a*b) <<endl ; break;
}
getch();
}
21 January 2013
Dr. Samaher Hussein Ali
Notes of Lecture 63
Examples:
Q3//Write a program to find N! .
#include <iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c=1;
cin>>b;
for(a=1;a<=b;a++)
c=c*a;
cout<<c<<endl;
}
21 January 2013
Dr. Samaher Hussein Ali
Notes of Lecture 64
Examples:
Q4//write a program to find power of number
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x,y;
float w=1;
cout<<"enter the number";
cin>>x;
cout<<"\nenter the power ";
cin>>y;
for(int i=1;i<=y;i++)
w=w*x;
cout<<"the resulte="<<w;
getch();
}
21 January 2013
Dr. Samaher Hussein Ali
Notes of Lecture 65
Examples:
Q5//what the output of this code
#include<iostream.h>
#include<conio.h>
void main()
{
Clrscr();
int a=3,b=2,c=a*b;
b++;
a=b++;
b++;
cout<<a<<endl<<b<<endl<<c<<endl;
getch();
}
Output:
a=3
b=5
c=6
21 January 2013
Dr. Samaher Hussein Ali
Notes of Lecture 66
Examples:
Q6// find the output that generation the following shape
#include<iostream.h>
*
*
#include<conio.h>
* *
void main()
*
{
* *
clrscr();
*
*
int x;
int k=0;
for(int i =1;i<=5;i++)
{
for(int j=1;j<=5;j++)
if((i+j==6)||(i==j))
cout<<"*";
else
cout<<" ";
cout<<"\n";
}
21 January 2013
Dr. Samaher Hussein Ali
Notes of Lecture 67
Examples:
Q7// Write Program to Print the Following Shape
1
12
123
1234
12345
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
for(int i=1;i<=6;i++)
{
for(int j=1;j<i;j++)
cout<<j;
cout<<endl;
}
}
21 January 2013
Dr. Samaher Hussein Ali
Notes of Lecture 68
Examples:
Q8 //Test Character Small or Capital
#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
char a,b;
cout<<"enter a";
cin>>a;
if(a>=65 && a<=90)
cout<<"capital";
if(a>=97 && a<=122)
cout<<"small";
}
21 January 2013
Dr. Samaher Hussein Ali
Notes of Lecture 69
Examples:
Q9// Test Number if Prime or Not
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x,result=0;
cin>>x;
for(int i=2;i<x;i++)
if(x%i==0)
result=1;
if(result==1)
cout<<"not prime";
else
cout<<"prime";
}
21 January 2013
Dr. Samaher Hussein Ali
Notes of Lecture106
Examples:
Q10 //Write Program to print the Following Form
1
2 3
4 5 6
7 8 9 10
#include<iostream.h>
void main()
{
int k=1;
for(int i=0;i<3;i++)
{
cout<<" ";
for(int =0;j<3;j++)
{
cout<<k++;
cout<<" ";
}
cout<<endl;
}
}
21 January 2013
Dr. Samaher Hussein Ali
Notes of Lecture116
Examples:
Q11//Write Program Find Cube and Square to Five Digit
#include <iostream.h>
#include <conio.h>
void main()
{
int a,b;
for(int i=0;i<5;i++)
{
cin>>x;
cout<<"squar"<<x*x;
cout<<"cube"<<x*x*x;
}
}
Q12 // Find the Result of Multiplying of Two number Without using *sign
#include<iostream.h>
#include<conio.h>
void main()
{
int x,y,sum=0;
cout<<"enter the first no:";
cin>>x;
cout<<"enter the second no:";
cin>>y;
while(y>0)
{
sum=sum+x;
y--;
}
21 January
2013
Dr. Samaher Hussein Ali
}
Notes of Lecture126
Examples:
Q13// Write a program to find the greatest common divisor (GCD) between two numbers
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x,y;
cout<<"enter the value of x&&y";
cin>>x>>y;
while(x<0)
{
cout<<"enter paositive value of first number";
cin>>x;
}
while(y<0)
{
cout<<"enter paositive value of second number";
cin>>y;
}
while(x!=y)
{
if(x>y)
x=x-y;
if(y>x)
y=y-x;
}
cout<<y;
}
21 January 2013
Dr. Samaher Hussein Ali
Notes of Lecture136
Examples:
Q14// Write program to reverse any number; such as 1234-4321
#include<iostream.h>
void main()
{
int x,y;
cout<<"enter the number";
cin>>x;
while(x!=0)
{
y=x%10;
x=x/10;
cout<<y;
}
}
Q15// Write a program to calculate the number of orders a certain number; such as 1765=4
void main()
{
clrscr();
int x,y=0;
cout<<"enter the number";
cin>>x;
while(x!=0)
{
x=x/10;
y=y+1;
}cout<<y;
}
21 January 2013
Dr. Samaher Hussein Ali
Notes of Lecture146
Examples:
Q16//Find dividing two numbers without using the process of division
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x,y,m=0;
cout<<"enter the number";
cin>>x>>y;
if((x>y)&&(y!=0))
{
while(x!=0)
{
x=x-y;
m=m+1;
}
}
cout<<m;
}
21 January 2013
Dr. Samaher Hussein Ali
Notes of Lecture156
Examples:
Q17// Write a program to print and find a solution following sequence of numbers
1/1 + 4/2 + 3/3 + 6/4 + 5/5 + 8/6 + 7/7+…………n
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,a,b,sum=0;
cout<<"enter the value of n";
cin>>n;
for(int i=1;i<=n;i++)
{
if(i%2==0)
{
a=i+2;
b=i;
}
else
{
a=i;
b=i;
}
sum=sum+(a/b);
cout<<a<<"/"<<b<<"+";
} cout<<endl;
cout<<"sum="<<sum;
}
21 January 2013
Dr. Samaher Hussein Ali
Notes of Lecture166
Examples:
Q18 // Write Program to print the following form.
*
***
*****
***
*
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
for(int i = 1;i<=3;i++)
{
for(int j =1;j<=3-i;j++)
cout<<' ';
for(int k = 1;k<=(2*i)-1;k++)
cout<<'*';
cout<<endl;
}
for( i = 2;i>=1;i--)
{
for( int j =1;j<=3-i;j++)
cout<<' ';
for( int k = 1;k<=(2*i)-1;k++)
cout<<'*';
cout<<endl
}
}
21 January 2013
Dr. Samaher Hussein Ali
Notes of Lecture176
Download