Uzma Nisar - 033
Uzma Nisar - 033
1. Amicable numbers are found in pairs. A given pair of numbers is Amicable if the sum of the
proper divisors (not including itself) of one number is equal to the other number and vice – versa.
For example, 220 & 284 are amicable numbers first we find the proper divisors of 220:
1+ 2 + 4 + 5 + 10 + 11 + 20 + 22 + 44 + 55 + 110 = 284
1 + 2 + 4 + 71 + 142 = 220
#include <iostream>
using namespace std;
int main(){
int N1, N2;
int sum1 = 0;
int sum2 = 0;
cout<<"please enter the first number: ";
cin>>N1;
cout<<"please enter the second number: ";
cin>>N2;
for(int i = 1; i <= N1/2; i++){
if(N1 % i == 0){
sum1 = sum1+i;
}
}
for(int i = 1; i <= N2/2; i++){
if(N2 % i == 0){
sum2 = sum2+i;
}
}
if(N1 == sum2 && sum1 == N2){
cout<<"This is Amicable";
}
else
Assignment#1 2
Department of Computer Science – Comsats University Islamabad
cout<<"Not Amicable";
}
return 0;
}
2. Write a C program to check if a number has three consecutive 5s. If yes, print YES, else print
NO.
Example:
Number: 1353554
Result: NO
Number: 345559
Result: YES
#include <iostream>
using namespace std;
int main(){
Assignment#1 3
Department of Computer Science – Comsats University Islamabad
Assignment#1 4
Department of Computer Science – Comsats University Islamabad
arr[rand()%6] +=1;
}
for(int i=0;i<6;i++)
{
cout<<(i+1)<<" rolled "<<arr[i]<<" times "<<endl;
}
return 0;
}
Assignment#1 5
Department of Computer Science – Comsats University Islamabad
4. Write a c program which reads a paragraph from user and count number of words in a
paragraph.
#include <iostream>
using namespace std;
int main()
{
int count=0;
cout<<"Please write a paragraph which you want to count"<<"\n";
string paragraph;
getline(cin,paragraph);
cout<<paragraph<<"\n";
cout<<"Length of the paragraph is: " <<paragraph.length();
return 0;
}
5. Write a c program which reads values of array and print array in reverse order.
include <iostream>
using namespace std;
int main(){
int array[8]={1,5,4,8,9,2,3,7};
int i;
cout<<"The array in the original order is: ";
for(i=0;i<8;i++){
cout<<array[i];
Assignment#1 6
Department of Computer Science – Comsats University Islamabad
cout<<"\n";
cout<<"The Array in the Reverse order is:";
for(i=7;i>=0;i--){
cout<<array[i];
}
return 0;
}.
Assignment#1 7
Department of Computer Science – Comsats University Islamabad
7. Write a program in C to find the sumof (productsof) lower and upper triangular elements
of a matrix.
#include <iostream>
using namespace std;
int main()
{
int num[20][20], i,j,n,matrixproduct=1, matrixproduct1=1;
cout << "Enter number of rows and columns of Matrix: ";
Assignment#1 8
Department of Computer Science – Comsats University Islamabad
cin >> n;
cout << "\nEnter Matrix Elements! ";
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
cout << "\nEnter elements in matrix: ", i,j;
cin >> num[i][j];
}
}
cout << "Matrix is as follows \n";
Assignment#1 9
Department of Computer Science – Comsats University Islamabad
{
matrixproduct1*=num[i][j];
}
}
}
cout << "product of lower triangular matrix " << matrixproduct1 << endl;
int res= matrixproduct+matrixproduct1;
cout << "Sum of upper and lower triangular matrices are : "<< res;
return 0;
}
Assignment#1 10
Department of Computer Science – Comsats University Islamabad
8. Determine the size, minimum and maximum value following data types. Please specify if
your machine is 32 bit or 64 bits in the answer.
a) Char
b) Short
c) Unsigned char
d) Unsigned int
e) Int
f) Unsigned long
g) Float
#include <iostream>
using namespace std;
int main()
{
int i,value;
char c;
float f;
short s;
unsigned int ui;
unsigned char uch;
unsigned long unl;
value = 9816253748586;
cout<<"size of int" <<sizeof(i)<<endl;
cout<<"size of char" <<sizeof(c)<<endl;
cout<<"size of float" <<sizeof(f)<<endl;
cout<<"size of short" <<sizeof(s)<<endl;
cout<<"size of unsigned int" <<sizeof(ui)<<endl;
cout<<"size of unsigned char" <<sizeof(uch)<<endl;
cout<<"size of unsigned long" <<sizeof(unl)<<endl;
cout<<value;
return 0;
}
Assignment#1 11
Department of Computer Science – Comsats University Islamabad
#include <iostream>
using namespace std;
int main()
{
int x = 10, y = 5;
x = x + y;
y = x - y;
x = x - y;
cout << "After Swapping: x =" << x << ", y=" << y;
return 0;
}
Assignment#1 12
Department of Computer Science – Comsats University Islamabad
#include <iostream>
int main()
{
int firstNo;
int secondNo;
int compare;
Assignment#1 13
Department of Computer Science – Comsats University Islamabad
#include <iostream>
#include <cmath>
using namespace std;
int main() {
if (discriminant > 0) {
x1 = (-b + sqrt(discriminant)) / (2*a);
x2 = (-b - sqrt(discriminant)) / (2*a);
cout << "Roots are real and different." << endl;
cout << "x1 = " << x1 << endl;
cout << "x2 = " << x2 << endl;
Assignment#1 14
Department of Computer Science – Comsats University Islamabad
else if (discriminant == 0) {
cout << "Roots are real and same." << endl;
x1 = -b/(2*a);
cout << "x1 = x2 =" << x1 << endl;
}
else {
realPart = -b/(2*a);
imaginaryPart =sqrt(-discriminant)/(2*a);
cout << "Roots are complex and different." << endl;
cout << "x1 = " << realPart << "+" << imaginaryPart << "i" << endl;
cout << "x2 = " << realPart << "-" << imaginaryPart << "i" << endl;
}
return 0;
}
Assignment#1 15
Department of Computer Science – Comsats University Islamabad
12. Write a C program to check whether a character is an alphabet, digit, or special character.
#include <iostream>
int isAlphabet(char l)
{
int ascii=l;
if(( l>=65 && l<=90) || (l>=97 && l<=122))
{
return 1;
}
return 0;
}
int isDigit(char l)
{
int ascii=l;
if((l>=48 && l<=57))
{
return 1;
}
return 0;
}
int isSpecialChar(char l)
{
if(isDigit(l) || isAlphabet(l))
{
return 0;
Assignment#1 16
Department of Computer Science – Comsats University Islamabad
return 1;
}
int main()
{
char letter;
printf("Enter a Character :");
scanf("%c",&letter);
printf("is Alphabet:%d\n",isAlphabet(letter));
printf("is Digit:%d\n",isDigit(letter));
printf("is Special Character:%d\n",isSpecialChar(letter));
return 0; }
Assignment#1 17
Department of Computer Science – Comsats University Islamabad
#include <iostream>
using namespace std;
int main()
{
int number,i,digit,sum=0;
cout<<"Arm Strong Number from 1 to 999 are :";
for(i=1;i<1000;i++)
{ number=i;
while(number>0)
{
digit = number%10;
sum+=digit*digit*digit;
number=number/10; }
if(sum==i)
cout<<"\n"<<i;
sum=0;
}
}
Assignment#1 18
Department of Computer Science – Comsats University Islamabad
14. Write a C program to find the eligibility of admission for a professional course based on
the following criteria:
Eligibility Criteria : Marks in Maths >=65 and Marks in Phy >=55 and Marks in
Chem>=50 and Total in all three subject >=190 or Total in Maths and Physics >=140 -------
------------------------------ Input the marks obtained in Physics :65 Input the marks obtained
in Chemistry :51 Input the marks obtained in Mathematics :72 Total marks of Maths,
Physics and Chemistry : 188 Total marks of Maths and Physics : 137 The candidate is not
eligible.
#include <iostream>
using namespace std;
int main()
{
int maths,physics,chemistry;
cout<<"please enter the marks in Maths :";
cin>>maths;
cout<<"please enter the marks in Physics :";
cin>>physics;
cout<<"please enter the marks in Chemistry :";
cin>>chemistry;
Assignment#1 19
Department of Computer Science – Comsats University Islamabad
15. Using precedence rules, evaluate the following expressions and determine the value of the
variables (without running the code). Also rewrite them using parenthesis to make the order
explicit.
a) Assume (x=0xFF33,MASK=0xFF00).Expression: c=x & MASK ==0;
16. Determine the hierarchy of operations and evaluate the following expression:
Assignment#1 20
Department of Computer Science – Comsats University Islamabad
𝒇𝒊𝒓𝒔𝒕 𝒘𝒆 𝒆𝒗𝒂𝒍𝒖𝒂𝒕𝒆𝒅
(3 + 4 * x)/5 – 10 * (y - 5)*(a + b + c)/x + 9 *(4 / x + (9 + x)/y)
• Operators contained within pairs of parentheses are evaluated first.
• Parentheses can be nested, in which case the expression in the inner parentheses is evaluated
first.
• Multiplication, division, and remainder operators are applied next.
Order of operation is applied from left to right.
Addition and subtraction are applied last.
𝟒 𝟑+𝒅(𝟐+𝒂)
c) − 𝟗(𝒂 + 𝒃𝒄) +
𝟑(𝒕+𝟑𝟒) 𝒂+𝒃𝒅
17. Write a program in C to merge one sorted array into another sorted array.
#include<iostream>
using namespace std;
int main(){
int arr[5] = {0 , 1, 6, 6,9};
int arr1[6] = {2, 3,7,9,11,12};
int sorted[11];
int i=0,j=0;
cout<<"My sorted Array is: "<<endl;
for(int k = 0; k<11; k++){
Assignment#1 21
Department of Computer Science – Comsats University Islamabad
j++;
}
}
Assignment#1 22
Department of Computer Science – Comsats University Islamabad
if(Val == arr[j]){
arr[j] = 0;
}
}
}
for(int k = 0;k<16;k++){
cout<<arr[k];
}
return 0;
}
19. Write a C program, that reads list of n integer and print sum of product of consecutive
numbers.
#include <iostream>
int main()
{
int n,sum=0;
Assignment#1 23
Department of Computer Science – Comsats University Islamabad
}
cout<<"Sum of Product of value is : " <<sum;
return 0;
}
Assignment#1 24
Department of Computer Science – Comsats University Islamabad
20. Write a C program to find the sum of First, Third and Last element of array and then replace
the zero index with sum. Then find the product of second and fourth element and place it at index
1 of array. Then sort the values of array in descending order to print array. Also show stack trace
for this program.
#include<iostream>
using namespace std;
int main(){
int n, m, sum;
cout<<"please enter the size of array you want to make: ";
cin>>n;
int array[n];
cout<<"array initialization: "<<endl;
for(int i = 0; i <n; i++){
cout<<"please enter the "<<m<<" value: ";
cin>>array[i];
m++;
}
sum = array[0]+ array[2]+ array[n-1];
array[0] = sum;
array[1] = array[1]*array[3];
int temp;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(array[i]<array[j])
{
temp =array[i];
array[i]=array[j];
array[j]=temp;
}
Assignment#1 25
Department of Computer Science – Comsats University Islamabad
}
}
21. Write a program in C++ to check overflow/underflow during various arithmetical operation
#include <iostream>
using namespace std;
int main()
{
cout << "\n\n Check overflow/underflow during various arithmetical operation :\n";
cout << " Range of int is [-2147483648, 2147483647]"<< endl;
cout << "---------------------------------------------------------------------\n";
int n1 = 2147483647; // maximum range of integer
cout <<" Overflow the integer range and set in minimum range : " << n1 + 1 << endl;
cout <<" Increasing from its minimum range : " << n1 + 2 << endl;
Assignment#1 26
Department of Computer Science – Comsats University Islamabad
22. Write a program in C to find the pivot element of a sorted and rotated array using binary
search
#include <stdio.h>
int findPivotElem(int *arr1, int left_elem, int right_elem)
{
if (right_elem < left_elem)
return -1;
if (right_elem == left_elem)
return left_elem;
int mid_elem = (left_elem + right_elem)/2;
if (mid_elem < right_elem && arr1[mid_elem] > arr1[mid_elem + 1])
Assignment#1 27
Department of Computer Science – Comsats University Islamabad
return mid_elem;
if (mid_elem > left_elem && arr1[mid_elem] < arr1[mid_elem - 1])
return mid_elem-1;
if (arr1[left_elem] >= arr1[mid_elem])
{
return findPivotElem(arr1, left_elem, mid_elem-1);
} else
{
return findPivotElem(arr1, mid_elem + 1, right_elem);
}
}
int main()
{
int i;
int arr1[] = {14, 23, 7, 9, 3, 6, 18, 22, 16, 36};
int ctr = sizeof(arr1)/sizeof(arr1[0]);
printf("The given array is : ");
for(i = 0; i < ctr; i++)
{
printf("%d ", arr1[i]);
}
printf("\n");
printf("The Pivot Element in the array is : %d \n", arr1[findPivotElem(arr1, 0, ctr-1) + 1]);
return 0;
}
Assignment#1 28
Department of Computer Science – Comsats University Islamabad
23. Write a C program which read an upper-case character array from user and replace each
character with lower case ascii value of same character.
#include <iostream>
#include <math.h>
using namespace std;
void populateArray(char arr[], int arrSize)
{
for(int i=0; i<arrSize;i++)
{
cin>>arr[i];
}
}
void displayArray(char arr[], int arrSize)
{
for(int i=0; i<arrSize;i++)
{
cout<<arr[i]<<" ";
}
}
Assignment#1 29
Department of Computer Science – Comsats University Islamabad
Assignment#1 30
Department of Computer Science – Comsats University Islamabad
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int n;
Assignment#1 31
Department of Computer Science – Comsats University Islamabad
// loop to print *
for(int j = 1; j <= i * 2 - 1; j++)
{
cout << " * ";
}
// half pyramid
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= i; j++)
Assignment#1 32
Department of Computer Science – Comsats University Islamabad
{
cout << " * ";
}
cout << endl;
}
return 0;
}
25. Write a C program to find whether one array is subset of other array or both arrays are
mirror image of one another.
#include <bits/stdc++.h>
using namespace std;
bool isSubset(int arr1[], int arr2[],
int m, int n)
{
int i = 0;
int j = 0;
for (i = 0; i < n; i++) {
Assignment#1 33
Department of Computer Science – Comsats University Islamabad
return 1;
}
int main()
{
int n,m;
cout<<"\nEnter the size of Array 1 : "<<endl;
cin>>m;
cout<<"\nEnter the size of Array 2 : "<<endl;
cin>>n;
int arr1[m],arr2[n];
int i;
cout<<"\nInput the Array 1 elements : "<<endl;
for(i = 0; i < m; i++)
{
cin>>arr1[i];
}
cout<<"\nInput the Array 2 elements : "<<endl;
for(i = 0;i<n;i++)
{
cin>>arr2[i];
}
Assignment#1 34
Department of Computer Science – Comsats University Islamabad
m = sizeof(arr1) / sizeof(arr1[0]);
n = sizeof(arr2) / sizeof(arr2[0]);
getchar();
return 0;
}
26. Write a program in C to find the ceil & floor of any element in an array.
#include <iostream>
using namespace std;
int FindCeil(int arr1[], int n, int x)
{
int low = 0, high = n - 1, mid;
Assignment#1 35
Department of Computer Science – Comsats University Islamabad
Assignment#1 36
Department of Computer Science – Comsats University Islamabad
low = mid + 1;
}
} return floor;
}
int main()
{
int n;
cout<< "\nEnter the size of Array : " << endl;
cin>>n;
int arr1[n],i;
cout<<"\nInput the Array 1 elements : "<< endl;
for(i = 0; i < n; i++)
{
cin>>arr1[i];
}
int ctr = sizeof(arr1) / sizeof(arr1[0]);
cout<<"The given array is : "<<endl;
for(i = 0; i < ctr; i++)
{
cout<<arr1[i];
}
cout<<"\n";
Assignment#1 37
Department of Computer Science – Comsats University Islamabad
Assignment#1 38
Department of Computer Science – Comsats University Islamabad
Output:
original number before isolating rightmost 0 bit: 11
new number after isolating rightmost 0 bit: 4
#include <iostream>
using namespace std;
int isolate_rightmost_zerobit(int n)
{
int mask1=n+1; //add 1 to original number
Assignment#1 39
Department of Computer Science – Comsats University Islamabad
int main()
{
int num;
return 0;
}
Assignment#1 40
Department of Computer Science – Comsats University Islamabad
#include<cctype>
int main()
char string[10], x;
cin.getline(string, 10);
x = string[0];
x = tolower(x);
string[0] = x;
else
else
Assignment#1 41
Department of Computer Science – Comsats University Islamabad
else
return 0;
30. Write a program in C which is a Menu-Driven Program to compute the area of the various
geometrical shape.
#include<iostream>
int main()
Assignment#1 42
Department of Computer Science – Comsats University Islamabad
int choice,r,l,w,b,h;
float area;
cin>>choice;
switch(choice)
case 1:
cin>>r;
area=3.14*r*r;
break;
case 2:
cin>>b;
cin>>h;
area=.5*b*h;
break;
case 3:
Assignment#1 43
Department of Computer Science – Comsats University Islamabad
cin>>l;
cin>>w;
area=l*w;
break;
31. Write a program in C which is a Menu-Driven Program to do all arithmetic operations. Make
sure there is different function for each individual arithmetic operation. Values for arithmetic
operations should be read from the user. Program will only exit from the last available choice of
menu available in your program (exit). Your program must contain data validation checks on
every input from the keyboard.
Assignment#1 44
Department of Computer Science – Comsats University Islamabad
#include <iostream>
using namespace std;
void showChoices();
float add(float, float);
float subtract(float, float);
float multiply(float, float);
float divide(float, float);
int main()
{
float x, y;
int choice;
do
{
showChoices();
cin >> choice;
switch (choice)
{
case 1:
cout << "Enter two numbers: ";
cin >> x >> y;
cout << "Sum " << add(x,y) <<endl;
break;
case 2:
cout << "Enter two numbers: ";
cin >> x >> y;
cout << "Difference " << subtract(x,y) <<endl;
break;
case 3:
Assignment#1 45
Department of Computer Science – Comsats University Islamabad
return 0;
}
void showChoices()
{
cout << "MENU" << endl;
cout << "1: Add " << endl;
cout << "2: Subtract" << endl;
cout << "3: Multiply " << endl;
cout << "4: Divide " << endl;
cout << "5: Exit " << endl;
cout << "Enter your choice :";
}
Assignment#1 46
Department of Computer Science – Comsats University Islamabad
{
return a + b;
}
Assignment#1 47
Department of Computer Science – Comsats University Islamabad
Assignment#1 48
Department of Computer Science – Comsats University Islamabad
z=x*y;
s=x+y;
println("Product= " + z + “and” +" Sum= "+ s +".");
q) Int i=88;
printf ("%010d", i);
println ("value in 10 digits: + i);
r) println (%.10s", "12345678901234567890");
s) String firstname= Sara;
String lastname= Khan;
println("First Name: " + firstname + " Last Name: "+ lastname +".");
println ("First Name: %s", firstname);
println ("Last Name: %s", lastname);
t) printf("%s\n%s\n”, “Welcome to” , “ C programming”);
u) int a=6, b=12, c=12;
sum= a+b+c;
printf(“Sum is %d\n”, sum);
v) int num1=5;
int num2=6;
if (num1 !=num2)
printf(“%d != %d \n”, num1, num2 );
w) char str[] = "cprogrammingexercise";
printf("%20s\n", str);
printf("%-20s\n", str);
printf("%20.5s\n", str);
printf("%-20.5s\n", str);
x) int a = 0;
y) int a = 0;
scanf("%i", &a);
printf("%d\n", a);
scanf("%i", &a);
printf("%d\n", a);
Assignment#1 49
Department of Computer Science – Comsats University Islamabad
a) True
b) 2
c) Hello! How are you all?
d) \- this is a backslash.
e) Good Morning Geeks!
f) Hi Learners, welcome to "CUI".
g) *
*
***
h) 2504
i) |1234.1234 |
j) 10 % 4 = 2
k) 1.231234E+002
l) UpperCase Scientific Notation using 1.231234E+002:
m) Number: 2335
n) +461012
o) 3.142 3.142
p) Product= 72 and Sum =18.
q) value in 10 digits: 0000000088
r) 1234567890
s) First Name: Sara Last Name: Khan. First Name: Sara Last Name: Khan
t) Welcome to
C programming
u) Sum is 30
v) 5 != 6
w) cprogrammingexercise
cprogrammingexercise
cprog
cprog
x) 45
y) if I input value 4 in a then output is also 4
z) Hello Bob, you are 21 years old
Assignment#1 50
Department of Computer Science – Comsats University Islamabad
Assignment#1 51