10 template code program
-
Upload
bint-el-maghrabi -
Category
Education
-
view
128 -
download
1
Transcript of 10 template code program
Write a Pr ogram that does the following: -
- Pr omotes the user to enter 5 number s.
- Print the Number s.
- Print the sum and average of these f ive number s.
#include<iostream.h>
void main()
{
int a,b,c,d,e,sum,avg;
cout<<"Enter the five numbers:";
cin>>a>>b>>c>>d>>e;
cout<<"the numbers you have entered are: ";
cout<<a<<" "<<b<<" ";
cout<<c<<" "<<d<<" "<<e<<endl;
sum = a + b + c + d + e;
avg = sum/5;
cout<<"the sum is "<<sum;
cout<<" and the average is "<<avg<<endl;
}
2
Write a Pr ogram that pr omotes the user to enter the length and
width of a rect angle and print i ts area.
#include <iostream.h>
void main()
{
double length,width;
cout<<"Enter the Length an the Width of the rectangle:";
cin>>length>>width;
cout<<"the area of the rectangle is "<<length*width<<endl;
}
3
Write a Pr ogram that f inds the area of a cir cle.
#include <iostream.h>
void main()
{
const float PI = 3.14;
double r;
cout<<"Enter the Radius of the Circle:";
cin>>r;
cout<<"the area of the Circle is "<<PI*r*r<<endl;
}
4
Write a Pr ogram to f ind the absolute value of
an integer
#include <iostream.h>
void main()
{
int a;
cout<<"enter a number:";
cin>>a;
if(a<0)
a *= -1;
cout<<"the absolute value of the number is: "<<a<<endl;
}
5
Write a Pr ogram that reads two dif ferent integer s
and print the largest
#include <iostream.h>
void main()
{
int a,b;
cout<<"enter two numbers:";
cin>>a>>b;
if(a>b)
cout<<a<<" is the largest"<<endl;
else
cout<<b<<" is the largest"<<endl;
}
6
Wr ite a Pr ogr am t hat r eads oper ation wi th i t's oper ands
and t hen p r int t he r esult
#include <iostream.h>
void main()
{
double a,b;
char x;
cout<<"enter the operation:";
cin>>a>>x>>b;
if(x=='+')
cout<<a+b<<endl;
else if (x=='-')
cout<<a-b<<endl;
else if (x=='*')
cout<<a*b<<endl;
else if (x=='/')
cout<<a/b<<endl;
else
cout<<"Unknown operation"<<endl;
}
7
Write a Pr ogram to read a character i f i t's in alphabet then
f ind i f i t ’s uppercase or lowercase. I f i t ’s a digit print that i t
is a digit .
E lse print that i t ’s a special character.
#include <iostream.h>
void main()
{
char x;
cout<<"enter the char:";
cin>>x;
if(x>='A' && x<='Z')
cout<<x<<" is Uppercase char"<<endl;
else if(x>='a' && x<='z')
cout<<x<<" is Lowercase char"<<endl;
else if(x>='0' && x<='9')
cout<<x<<" is a Digit"<<endl;
else
cout<<x<<" is a special char"<<endl;
} 8
Write a loop to print the number s f r om 1 to 10
#include <iostream.h>
void main()
{
int i = 0;
while(i<=10)
cout<<i++<<endl;
}
9
Write a Pr ogram to print the multiple of number 5
between 0 to 20
#include <iostream.h>
void main()
{
int i = 5;
while(i<=20)
{
cout<<i<<endl;
i+=5;
}
}
10
Write a loop to f ind the sum of number s f r om 1 to 20
#include <iostream.h>
void main()
{
int sum=0,i = 0;
while(i<=20)
sum +=i++;
cout<<"the sum is: "<<sum<<endl;
}
11
Write a loop to f ind the sum of odd number s f r om
20 to 300
#include <iostream.h>
void main()
{
int i = 20,sum = 0;
while(i<=300)
{
if(i%2==1)
sum += i;
i++;
}
cout<<"the sum is: "<<sum<<endl;
}
12
Find the sum of even number f r om 1 to 30
#include <iostream.h>
void main()
{
int i = 1,sum = 0;
while(i<=30)
{
if(i%2==0)
sum += i;
i++;
}
cout<<"the sum is: "<<sum<<endl;
}
13
Write a loop to f ind factorials for an entered number
#include <iostream.h>
void main()
{
int x,y;
cout<<"enter the number:";
cin>>x;
y=x;
while(x!=0)
{
if(y%x==0)
cout<<x<<endl;
x--;
}
}
14
Find the maximum between 20 number s
#include <iostream.h>
void main()
{
int x,y;
cout<<"enter the number:";
cin>>x;
int i = 0;
while(i<=20)
{
if(x>y)
{
y =x;
cout<<"the Maximum until now is "<<y<<endl;
}
cin>>x;
i++;
}
} 15
Write a for loop to print number 1 to 40 each 5 on l ine
#include <iostream.h>
#include <iomanip.h>
void main()
{
for(int i=1;i<=40;i++)
{
cout<<setw(3)<<i;
if(i%5==0)
cout<<endl;
}
}
16
Write a Pr ogram that reads a number and per form the following
- print the number digits in rever se or der.
- Find the sum of i t's digits.
- Find the average of i t's digits
#include<iostream>
#include<iomanip>
using namespace std;
void main()
{
int x,y,z=0,avg=0,sum=0;
cin>>x;
while(x!=0)
{
y = x%10;
x/=10;
cout<<y<<endl;
sum += y;
z++;
}
avg = sum/z;
cout<<"the sum is "<<sum<<endl;
cout<<"the avg is "<<avg<<endl;
} 17
Wr ite a Pr ogr am t o r ead a s e t of non zer o(when r ead zer o, s t op t he p r ogr am)
and f ind:
s um – av er age - m aximum v alue - m inimum v alue -
t he number of v alues - s um of number s t hat d iv ide on 5
#include<iostream>
#include<iomanip>
using namespace std;
void main()
{
int x,y,max,min,z=0,avg=0,sum=0,sum5=0;
cin>>x;
max = x;
min = x;
while(x!=0)
{
if(x>max)
max = x;
if(x<min)
min =x;
18
19
sum+=x;
z++;
if(x%5==0)
sum5+=x;
cin>>x;
}
avg = sum/z;
cout<<"the sum is "<<sum<<endl;
cout<<"the average is "<<avg<<endl;
cout<<"the maximum value is "<<max<<endl;
cout<<"the minimum value is "<<min<<endl;
cout<<"the numbers of values is "<<z<<endl;
cout<<"the sum of numbers that divide on 5 is
"<<sum5<<endl;
}
Write a Loop that dr aws the following shape:
*
**
***
****
*****
#include<iostream.h>
void main()
{
for(int i = 0;i<5;i++)
{
for(int j = 0;j<=i;j++)
cout<<'*';
cout<<endl;
}
}
20
Write a Loop that dr aws the following shape
#include<iostream.h>
void main()
{
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;
}
}
21
لمدخل ا لصفر للعدد زوجية من ا ل ا طباعة األعداد
#include <iostream.h>
int main()
{
int i,num;
cout<<"Enter the last number : ";
cin>>num;
for (i=0;i<=num;i++)
if (i%2==0)
cout<<i<<" ";
cout<<endl;
return 0;
}
22
لمدخل ا لفردية من الصفر للعدد ا :طباعة األعداد
#include <iostream.h>
int main()
{
int i,num;
cout<<"Enter the last number : ";
cin>>num;
for (i=0;i<=num;i++)
if (i%2!=0)
cout<<i<<" ";
cout<<endl;
return 0;
}
23
لعدد ا رنامج تقوم أنت بإدخال أي عدد ويختبر هو ذلك ب
نه موجب أو سالب أو صفر من حيث أ ..
#include <iostream.h>
int main()
{
float num;
cout<<"Enter the number : ";
cin>>num;
if(num>0)
cout<<num <<" is Positive Number"<<endl;
else if(num<0)
cout<<num <<" is Negative Number"<<endl;
else
cout<<num<<" is Zero"<<endl;
return 0;
}
24
رنامج رقام ويقوم ب أ تقوم من خالله بإدخال عشرة إيجاد أكبر وأصغر عدد بينهم :هو ب
#include<iostream.h> int main() { float num,max=0,min=3200; cout<<"Enter 10 numbers : "; for(int i=0;i<10;i++) { cin>>num; if(max<num) max=num; if(min>num) min=num; } cout<<"The Maximum Numbers is : "<<max<<endl; cout<<"The Minimum Numbers is : "<<min<<endl; return 0; }
25
:بطريقة أخرى:تقوم بإدخال عددين ويوجد األكبر منهما
#include<iostream.h>
int main()
{
float num1,num2,max;
cout<<"Enter num1 : ";
cin>>num1;
cout<<"Enter num2 : ";
cin>>num2;
max=(num1>=num2)?num1:num2;
cout<<"The Maximum Numbers is : "<<max<<endl;
return 0;
}
26
:تقوم بإدخال عددين ويوجد األصغر بينهما
#include<iostream.h>
int main()
{
float num1,num2,min;
cout<<"Enter num1 : ";
cin>>num1;
cout<<"Enter num2 : ";
cin>>num2;
min=(num1<=num2)?num1:num2;
cout<<"The Minimum Numbers is : "<<min<<endl;
return 0;
}
27
باسوورد ثم يقوم ل ا إدخال لمستخدم ا مج يطلب من ا ن ر ب
ه ر ا ب خت ا ب ..
#include<iostream.h>
#include<string.h>
int main()
{
const char a[6]="admin";
char pass[6];
cout<<"Enter Your Password : ";
cin>>pass;
if(strcmp(pass,a)==0)
cout<<"Welcom to my Program.."<<endl;
else
cout<<"You are Not Allowed to Access This Program"<<endl;
return 0;
}
28
لمضروب ا
include <iostream.h>#
int main()
{
int num,fact=1;
cout<<"Enter The Number: ";
cin>>num;
for(int i=2;i<=num;i++)
fact*=i;//fact=fact*i
cout<<"Factorial = "<<fact<<endl;
return 0;
}
29
لمضروب باستخدام :while ا
#include <iostream.h>
int main()
{
int i=1,num,fact=1;
cout<<"Enter The Number: ";
cin>>num;
while(i<=num)
{
fact*=i;//fact=fact*i
i++;
}
cout<<"Factorial = "<<fact<<endl;
return 0;
}
30
لمستخدم إدخال مجموعة من األعداد رنامج يطلب من ا بلمتوسط لهذه ا تنتهي بالصفر ثم يقوم بحساب نه يتجاهله األعداد وإذا صادف عدد سالب فإ
لحسبان باستخدام ثم Continue واليضعه في العملية ا يكمل
#include<iostream.h> int main() { int x,sum=0,count=0; cout<<"Enter the numbers : "<<endl; for (;;) { cin>>x; count+=1; if (x<0) { count-=1; continue; } else sum=sum+x;
31
32
if(x==0)
{
count-=1;
break;
}
}
cout<<"The Avarege = "<<sum/count<<endl;
return 0;
}
لمستطيل عن طريق رنامج يقوم بحساب مساحة ا ب ..استدعاء بروسيجر آخر يقوم بالحساب
#include<iostream.h>
float cal(float length,float width);
void main()
{
float length,width,area;
cout<<"Enter the rectangle length:";
cin>>length;
cout<<"Enter the rectangle width:";
cin>>width;
area=cal(length,width);
cout<<"Rectangle Area = "<<area<<endl;
}
float cal(float length,float width)
{
float w;
w = length * width;
return w;
} 33
رنامج تقوم انت بإدخال أسمك ثم يقوم هو بطباعة بلذي ادخلته ا ارة ترحيبيه يظهر فيه اسمك .عب
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout<<"Enter your name: ";
cin>>name;
cout<<"Hello "<<name<<endl;
return 0;
}
34