Você está na página 1de 27

#include<iostream.h> #include<stdio.h> #include<conio.

h> // Write a program to read a number of days // and convet this number to weeks and days /* #include<iostream.h> main() { int a , weeks , days ; cout << "Please enter number of days: "; cin >> a ; weeks = a / 7 ; cout << a << " days is equal to " << weeks << " weeks "; days = a % 7 ; cout << " and " << days << " days "; } // Write a program to calculate 13 / 2 as a real number #include<iostream.h> main() { float res; res = float(13) / 2 ; // res = 13.0 / 2 ; cout << res << "\n"; } // Write a program to read 3 integer numbers and print the maximum #include<iostream.h> main() { int a , b , c , max ; cout << "Please enter three integer numbers: "; cin >> a >> b >> c ; if(( a >= b) && ( a >= c)) { max = a ; } else if(b >= c) { max = b ; } else { max = c ; } cout << "Maximum = " << max << endl ;

} Write a program to read three numbers and print the middle number. Write a program to read a month number and print this month name Write a rogram to read a month number and print number of days in this month Write a program which ask you about your age in years and months and print your age in days Write a program to convert number which represnts days by weeks and days (using % , without using %) Show the steps to calculate 12 * 20 / 8 + 12 / 2 * 4 7 * (2 + 1)

//Write a program to add the numbers from 1 to 10 #include<iostream.h> main() { int sum , counter; counter = 1 ; sum = 0 ; do{ sum = sum + counter ; counter = counter + 1 ; }while(counter <= 10) ; cout << sum << endl; }

// another solution #include<iostream.h> main()

{ int sum , counter; counter = 1 ; sum = 0 ; while(counter <= 10) { sum = sum + counter ; counter = counter + 1 ; } cout << sum << endl; } //another solution #include<iostream.h> main() { int sum , counter; sum = 0 ; for(counter = 1 ; counter <= 10 ; counter = counter + 1 ) { sum = sum + counter ; } cout << sum << endl; }

What is the difference between counter++ and ++counter? Give examples to show it. Compare between different types of loops (while , do-while , for) Show when it is better to use each form, give examples.

#include<iostream.h> main() { float res ; res = 3.2 * 2 ; cout << res << endl ;

//Write a rogram to read a positive number, test and print //if this number is a prime number or not #include<iostream.h> void main (void) { int num , k , counter; do{ cout << "please enter positive number: "; cin >> num ; }while(num <= 0); counter = 0 ; for(k = 2 ; k <= num/2 ; k++) { if( num % k == 0 ) { counter++; break; } } if( counter == 0) else } */ //Write a program to print prime numbers less than 100 /* #include<iostream.h> void main (void) { int num , k , counter; for(num = 2 ; num <= 99 ; num++) { counter = 0 ; for(k = 2 ; k <= num/2 ; k++) { cout << "Prime number\n"; cout << "Not Prime \n";

if( num % k == 0 ) } if( counter == 0) } }

{ counter++; break; } cout << num << "\t";

Write a program to read a positive number less than 100 and factorize this number to its prime numbers. Write a program to read a fraction and to simpilify this fraction example if the user 20 and 30 (20/30) the computer print 2/3 // Write a program to print 6 rows, each row has 10 stars using // nested loops /* #include<iostream.h> void main (void) { int r , c ; for(r = 1 ; r <= 6 ; r++) { for(c = 1 ; c <= 10 ; c++) { cout << " * " ; } cout << endl ; } } Write a program to print multiplication table from 1 to 6 using nested loops Write a program to print a triangle of stars */ /* #include<iostream.h> main() { int x , y ; x=y=3; x++ ; ++y ; cout << "First " << x << "\t" << y << "\n"; cout << "Second " << x << "\t" << y << "\n";

int a , b ; a = b = 12 ; cout << "third " << a++ << "\t" << ++b << "\n"; cout << "Four " << a << "\t" << b << "\n"; int c , d , e , f ; c = d = 40 ; e = c++ ; f = ++d ; cout << "Fifth " <<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<"\n"; int m , n ; m = n = 66 ; m++ ; ++n ; cout<<"Six " << m << "\t" << n << "\n";

// Write a program to add positive numbers entered // by the user until entering negative number int x , sum ; sum = 0 ; cout << "Inintial value is " << x << endl ; x = 1000; for( ; x > 0 ; ) { cout << "Enter a number: "; cin >> x ; if(x > 0) sum = sum + x ; } cout << "Result of addition = " << sum ; } Write a program to calculate 1! + 2! + 3! + .... + 7! using nested loop

//Write a program to read 3 integer numbers and print //these numbers in a reverse order #include<iostream.h> main()

{ int a , b , c ; cout << "Please enter 3 numbers: "; cin >> a >> b >> c ; cout << "Numbers in a reverse order are: "; cout<< c <<" "<< b <<" "<< a << endl; } Write a program to read 10 integer numbers and print these numbers in a reverse order #include<iostream.h> main() { int k , a[10] ; cout << "Please enter 10 numbers: "; for(k = 0 ; k <= 9 ; k++) cin >> a[k] ; cout << "Numbers in a reverse order are: "; for(k = 9 ; k >= 0 ; k--) cout << a[k] << "\t"; }

Write a program to read scores of 10 students in mid term exam, then read scores of these students in final exam. Then find and print the total score and grade of each student. Validate input data (mid score 0~30 , final score from 0~70)

#include<iostream.h> main() { int i , mid[10] , final[10] , total[10]; char grade[10];2 cout<< "Enter 10 scores for mid term exam:\n"; for(i = 0 ; i <= 9 ; i++) { do{ cout<<"Enter score of mid term for student "<<i+1<< " : ";

cin >> mid[i] ; }while((mid[i] < 0)||(mid[i] > 30)); } cout<< "Enter 10 scores for final exam:\n"; for(i = 0 ; i <= 9 ; i++) { do{ cout<<"Enter score of final for student "<<i+1<< " : "; cin >> final[i] ; }while((final[i] < 0)||(final[i] > 70)); } cout<< "Total scores and grades:\n"; for(i = 0 ; i <= 9 ; i++) { total[i] = mid[i] + final[i] ; if(total[i] >= 85) grade[i] = 'A' ; else if(total[i] >= 75) grade[i] = 'B' ; else if(total[i] >= 65) grade[i] = 'C' ; else if(total[i] >= 50) grade[i] = 'D' ; else grade[i] = 'F' ; cout << total[i] << "\t" << grade[i] << endl; } }

Write a program to read two dimensions matrix 3*5, and print the transpose of this matrix. Write a program to add two matrises with dimensions 4*6. Write a program to multiply two matrices with dimensions 4*6 and 6*3. Write a program to read 10 integer numbers and print the numbers which are greater than the average. Write a program to sort 10 numbers in ascending order. Write a program to read 100 numbers and print these number after deleting repetion numbers.

// Write a program to calculate n! #include<iostream.h> main() { int n , x , y ; cout<<"Please enter value of n: "; cin >> n ; y=1; for(x = n ; x >= 1 ; x--) {y=y*x; } cout <<"Factorial = " << y << endl ; } Write a progaram to calculate 7! + 4! -6! #include<iostream.h> main() // bad program { int n , x , y , a , b , c , res ; n=7; y=1; for(x = n ; x >= 1 ; x--) {y=y*x; } a=y; n=4; y=1; for(x = n ; x >= 1 ; x--) {y=y*x; } b=y; n=6; y=1; for(x = n ; x >= 1 ; x--) {y=y*x; } c=y; res = a + b - c ; cout <<"Result = " << res << endl ;

#include<iostream.h> int fact(int); void main(void) { int res ; res = fact(7) + fact(4) - fact(6) ; cout << "Result = " << res << endl ; } int fact(int num) { int f , x ; f=1; for(x = num ; x >= 1 ; x-- ) f=f*x; return f; }

Write a program to calculate (15+20+25+.....+70)*(6+9+12+.....+24) using function #include<iostream.h> int add(int,int,int); int main(void) { int a , b , res ; a = add(15,5,70); b = add(6,3,24); res = a * b ; cout << "Result = " << res << endl ; return 1; }

int add(int first , int step , int last) { int x , sum = 0 ; for(x = first ; x <= last ; x += step) sum += x ; return sum; }

Write a program to print this figure ###### ###### ###### ###### Hello ***** ***** ***** Byebye ##### ##### ##### ##### Write a function to read 10 numbers and return the maximum Write a function to read 10 numbers and return the minimum Write a function to read 10 numbers and return maximum and minimum Write a program to read 10 numbers and print the maximum by calling a function which calculate and return the maximum.

#include<iostream.h> main() { int a , b , i , c ; cout << "Enter a , b : "; cin >> a >> b ; c=a/b; a=a%b; for(i = 2 ; (i <= a)&&(i <= b) ; )

{ if((a % i == 0)&&(b % i == 0)) { a=a/i; b=b/i; } else i++ ; } cout << c << "\t" << a << "\t" << b << endl ; }

#include<iostream.h> main() { int y[3][5] , z ; y[2][2] = 12 ; y[1][7] = 30; y[2][2] *= 4 ; cout << y[2][2] << "\t" << endl; }

Write a program to print this figure ##### ##### ##### ##### Hello ##### ##### ##### ##### Byebye ##### ##### ##### ##### void negoom(void);

void negoom(void) { int r , c ; for(r = 1 ; r <= 4 ; r++) { for(c = 1 ; c <= 5 ; c++) { cout << " # "; } cout << endl ; } } main() { negoom(); cout << "Hello" << endl ; negoom(); cout << "Byebye" << endl ; negoom(); } Write a program to print this figure ###### ###### ###### ###### Hello ##### ##### ##### Byebye ##### ##### ##### ##### void negoom(int,int); void negoom(int h , int w) { int r , c ; for(r = 1 ; r <= h ; r++) {

for(c = 1 ; c <= w ; c++) { cout << " # "; } cout << endl ; } } main() { negoom(4,6); cout << "Hello" << endl ; negoom(3,5); cout << "Byebye" << endl ; negoom(4,5); int x ; } Write a program to print 20 terms from the series 0 1 1 2 3 5 8 13 21....... Write a program to print numbers from 1 to 100 without using any loop. void printnumbers(int,int); main() { printnumbers(1,100); } void printnumbers(int F , int L) { cout << F << " "; if(F < L) printnumbers(F+1,L); } Write a program to add the numbers from 1 to 100 using recursion Write a program to print the steps of moving 5 plates from A to C using B Hanoi tower for(x=1; x<= 10 ; x++) printf("%3d ",x) ; cout << "Byebye with " << x ; main()

{ float y ; y = 1000.0 / 2 ; printf("Result = %8.1f \n", y) ; } Write a program to read 2 integer numbers and store these numbers in two variables a and b. Then call a function to add 10 to each number. The main program will print the numbers after addition void addten(int *pa, int *pb) { *pa = *pa + 10 ; *pb = *pb + 10 ; } main() { int a , b ; cout << "Please enter two integer numbers: "; cin >> a >> b ; addten(&a,&b); cout << "Number after addition are" << a << "\t" << b << endl; } Write a program to read 10 numbers, then call a function to calculate the square of each number. The main program will print the results

void square(int *pa) { for(int j = 0 ; j <= 9 ; j++) *(pa+j) = *(pa+j) * *(pa+j); } main() { int a[10] , i ; cout << "Please enter 10 integer numbers: ";

for( i = 0 ; i <= 9 ; i++) square(&a[0]);

cin >> a[i] ;

cout << "Numbers will be \n" ; for(i = 0 ; i <= 9 ; i++) cout << a[i] << "\t" ; } Write a program to read two groups of data, each group has 10 numbers, add each number to the coresponding number in the other group, print the resultant group. Use 3 functions (read,add,write). do not use global vaiables. (use passing refrence)

main() { int a[10],b[10],c[10]; read_data(a,10); read_data(b,10); add_data(a,b,c,10); write_data(c,10); } #define shokra "Thank you\n" void read_data(int *p , int n) { for(int i = 0 ; i < n ; i++) { cout << "Enter a number: "; cin >> *(p+i); } cout << shokra ; } void write_data(int *p , int n) { for(int i = 0 ; i < n ; i++) { cout << *(p+i) << endl ; } }

void add_data(int *p1 , int *p2 , int *p3 , int n) { for(int i = 0 ; i < n ; i++) *(p3 + i) = *(p1 + i) + *(p2 + i); } main() { #define k 10 int a[k],b[k],c[k]; read_data( a , k ); read_data( b, k ); add_data( a , b , c , k ); write_data( c , k ); } Write a program to caluulate n! using recursive function

int fact(int a) { int b ; b=1; if(a > 0) return b; } main() {

b = a * fact(a-1);

char choice ; int n , k ; do{ cout << "Enter value of n "; cin >> n ; k = fact(n); cout << k << endl ; cout <<"Do you want to calculate another factorial?"; cout << "Type y or n : "; cin >> choice; }while((choice == 'y')||(choice == 'Y'));

} 1. Write a program to read a month number and print number of days in this month (it is better to use array) 2. Write a program to print prime numbers less than 100 3. Write a program to print the numbers from 1 to 60 in 10 rows, each row contains 6 numbers. 4. Write a program to print numbers from 1 to 100 without using any loop. 5. Write a program to read 10 numbers, then call a function to multiply each number by 2. The main program will print the resultant numbers after multiplications. 6. Write a program to print this figure ###### ###### ###### ###### Hello ##### ##### ##### Byebye ##### ##### ##### ##### main() { int a[10]; add(&a[0]); } void add(int *pa) { *p = *p * 2 ; }

main() { int k = 1 ;

for(int r = 1 ; r <= 10 ; r++) { for(int c = 1 ; c <= 6 ; c++) { cout << k++ << "\t"; } cout << endl ; } }

main() { int k ; for(k = 1 ; k <= 60 ; k++) { cout << k << "\t"; if(k % 6 == 0) cout << endl ; } } 1 2 3 4 5 main() { for(int r = 0 ; r <= 9 ; r++) { for(int c = 1 ; c <= 6 ; c++) { cout << r+c << "\t"; } cout << endl ; } } 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 5 6 7 8 9 6 7 8 9 10

main() { int k ; do{ cout << "Enter month number: "; cin >> k ; }while((k < 1)||(k > 12)); if(k == 2) "28\n" ; else if((k==4)||(k==6)||(k==9)||(k==11)) cout << "30\n"; else "31\n"; } main() { int k ; do{ cout << "Enter month number: "; cin >> k ; }while((k < 1)||(k > 12)); switch (k) { case 2 : cout << "28 \n"; break ; case 4 : case 6 : case 9 : case 11 : cout << "30 \n"; break ; default: cout << "31 \n"; break ; } } cout << cout <<

main() { int k ; do{ cout << "Enter month number: "; cin >> k ;

}while((k < 1)||(k > 12)); int y[13]={0,31,28,31,40,31,30,31,31,30,31,30,31}; cout << y[k] << endl; }

Write a function to read id(int), gpa(float), and gender(char). Then write a program to read the data of 30 students. Then print these data. Do not use glabal variables or pointers.

struct onedata { int id ; float gpa ; char gender; }; onedata read(void) { onedata temp; cout << "Enter id: "; cin >> temp.id ; cout << "Enter gpa: "; cin >> temp.gpa ; cout << "Enter gender: "; cin >> temp.gender ; return temp ; } main() { onedata st[30] ; int i ; for(i = 0 ; i <= 29 ; i++) st[i] = read(); for(i = 0 ; i <= 29 ; i++ ) cout <<st[i].id<<"\t"<<st[i].gpa<<"\t"<<st[i].gender<<endl; }

Write a program to read id(int), gpa(float), and gender(char) of 10 sudents, then print id of the student who has the hieghest gpa. struct onedata { int id ; float gpa ; char gender; }; onedata read(void) { onedata temp; cout << "Enter id: "; cin >> temp.id ; cout << "Enter gpa: "; cin >> temp.gpa ; cout << "Enter gender: "; cin >> temp.gender ; return temp ; } main() { onedata st[10] ; int i ; for(i = 0 ; i <= 9 ; i++) st[i] = read(); float max = 0 ; int k ; for(i = 0 ; i <= 9 ; i++ ) if(max < st[i].gpa) { max = st[i].gpa ; k = i ; } cout <<st[k].id<<"\t"<<st[k].gpa<<"\t"<<st[k].gender<<endl; } Write a program to read id, gpa, gender of 10 students Then sort the data in desending order according to gpa. print the data after sorting

Write a program to count number of days between 1/1/1 and today. Write a program to read day/month/year and print the name of the week day.

Write a program to read id, gpa, and gender of two students. and print the data without using structure class student { private: int id ; float gpa ; char gender; public: void read(void) { cout << "Enter id: "; cin >> id ; cout << "Enter gpa: "; cin >> gpa ; cout << "Enter gender: "; cin >> gender ; } void write(void) { cout <<id<<"\t"<<gpa<<"\t"<<gender<<endl; } }; main() { student st1, st2 ; st1.read(); st2.read(); st1.write(); st2.write(); } Write a program to read 20 numbers, and store these numbers in

two different arrays, one for positive numbers and the other for negative numbers. Sort each group and print the numbers in each group after sorting. Use classes. Write a program to read an integer number and a float number from the user. Then call a function (or functions) to calculate the square of these two numbers. The main program will print the numbers and its squares. use function overloading.

int square(int k) { int n ; n=k*k; return n ; } float square(float k) { float n ; n=k*k; return n ; } main() { int x , sx ; float y , sy ; cout << "Enter integer number"; cin >> x ; cout << "Enter float number"; cin >> y ; sx = square(x); sy = square(y); cout << x << "\t" << sx << endl; cout << y << "\t" << sy << endl ; } Write a program to read n numbers and print the numbers and

power 5 of these numbers. The program will ask the user about value of n #include<math.h> main() { int i , n ; cout << "Please enter value of n: "; cin >> n ; int *y; y = new int[n] ; for(i = 0 ; i < n ; i++) cin >> y[i] ; for(i = 0 ; i < n ; i++) cout << y[i] << "\t" << pow(y[i],5) << endl ; }

Write a class called person who has 2 data members (id, gen) and 2 function members read and write. */ class person { protected: int id ; char gender; public: void read(void) { cout << "Enter id: "; cin >> id ; cout << "Enter gender: "; cin >> gender ; } void write(void) { cout <<id<<"\t"<<gender<<endl;

} }; //Inherit the person class in a new class called student who has //one more data member gpa. class student : public person { private: float gpa ; public: void read2(void) { read(); cout << "Enter gpa: "; cin >> gpa ; } void write2(void) { write(); cout << gpa << endl ; cout << id ; } }; main() { student st ; st.read2(); st.write2(); }

Você também pode gostar