Assignment 3
Assignment 3
1. Write a C program to calculate all the Armstrong numbers between 1 to 999 using for
loop.
2. Write a C program to print the following pattern using while loop.
∗ ∗ ∗ ∗ ∗
∗ ∗ ∗ ∗
∗ ∗ ∗
∗ ∗
∗
3. Write a C program to find the sum of first 20 terms of the following series.
1 2 3
+ + + ⋯
1! 2! 3!
4. Write a C program to print all prime numbers from 1 to 500. (Hint: Use nested loops,
break, and continue)
5. Write a C program to print the following pattern using for loop.
A B C D E F G F E D C B A
A B C D E F F E D C B A
A B C D E E D C B A
A B C D D C B A
A B C C B A
A B B A
A A
6. Write down the output of the following programs?
a.
# 𝑖𝑛𝑐𝑙𝑢𝑑𝑒 < 𝑠𝑡𝑑𝑖𝑜. ℎ >
𝑣𝑜𝑖𝑑 𝑓𝑢𝑛(𝑖𝑛𝑡 𝑥){
𝑥 = 30;
}
𝑖𝑛𝑡 𝑚𝑎𝑖𝑛(){
𝑖𝑛𝑡 𝑦 = 20;
𝑓𝑢𝑛(𝑦);
𝑝𝑟𝑖𝑛𝑡𝑓("%𝑑", 𝑦);
𝑟𝑒𝑡𝑢𝑟𝑛 0;
}
b.
# 𝑖𝑛𝑐𝑙𝑢𝑑𝑒 < 𝑠𝑡𝑑𝑖𝑜. ℎ >
𝑣𝑜𝑖𝑑 𝑓𝑢𝑛(𝑖𝑛𝑡 ∗ 𝑝𝑡𝑟){
∗ 𝑝𝑡𝑟 = 30;
}
𝑖𝑛𝑡 𝑚𝑎𝑖𝑛(){
𝑖𝑛𝑡 𝑦 = 20;
𝑓𝑢𝑛(&𝑦);
𝑝𝑟𝑖𝑛𝑡𝑓("%𝑑", 𝑦);
𝑟𝑒𝑡𝑢𝑟𝑛 0;
}
c.
#𝑖𝑛𝑐𝑙𝑢𝑑𝑒 < 𝑠𝑡𝑑𝑖𝑜. ℎ >
𝑖𝑛𝑡 𝑚𝑎𝑖𝑛(){
𝑓𝑙𝑜𝑎𝑡 𝑎𝑟𝑟[5] = {12.5, 10.0, 13.5, 90.5, 0.5};
𝑓𝑙𝑜𝑎𝑡 ∗ 𝑝𝑡𝑟1 = &𝑎𝑟𝑟[0];
𝑓𝑙𝑜𝑎𝑡 ∗ 𝑝𝑡𝑟2 = 𝑝𝑡𝑟1 + 3;
𝑝𝑟𝑖𝑛𝑡𝑓("%𝑓 ",∗ 𝑝𝑡𝑟2);
𝑝𝑟𝑖𝑛𝑡𝑓("%𝑑", 𝑝𝑡𝑟2 − 𝑝𝑡𝑟1);
𝑟𝑒𝑡𝑢𝑟𝑛 0;
}
7. What is the function in C language? Explain the basic difference between call by value
and call by reference.
8. Write a C program to swap two numbers using call by reference.
9. Write a C program to find using recursion 𝑎𝑛 𝑎, 𝑛 ∈ 𝐼 + take the input from user.
10. Write a C program using function to find the approx. square root (⌊√𝑛⌋) of the number
input by user in minimum possible iteration.
11. Write down the output of the following programs?
a.
#𝑖𝑛𝑐𝑙𝑢𝑑𝑒 < 𝑠𝑡𝑑𝑖𝑜. ℎ >
𝑖𝑛𝑡 𝑚𝑎𝑖𝑛(){
𝑖𝑛𝑡 𝑖 = 5;
𝑝𝑟𝑖𝑛𝑡𝑓("%𝑑 %𝑑 %𝑑", 𝑖 + +, 𝑖 + +, 𝑖 + +);
𝑟𝑒𝑡𝑢𝑟𝑛 0;
}
b.
#𝑖𝑛𝑐𝑙𝑢𝑑𝑒 < 𝑠𝑡𝑑𝑖𝑜. ℎ >
𝑣𝑜𝑖𝑑 𝑓𝑜𝑜(𝑖𝑛𝑡 𝑛, 𝑖𝑛𝑡 𝑠𝑢𝑚){
𝑖𝑛𝑡 𝑘 = 0, 𝑗 = 0;
𝑖𝑓 (𝑛 == 0) 𝑟𝑒𝑡𝑢𝑟𝑛;
𝑘 = 𝑛 % 10;
𝑗 = 𝑛 / 10;
𝑠𝑢𝑚 = 𝑠𝑢𝑚 + 𝑘;
𝑓𝑜𝑜 (𝑗, 𝑠𝑢𝑚);
𝑝𝑟𝑖𝑛𝑡𝑓(%d,, 𝑘);
}
𝑖𝑛𝑡 𝑚𝑎𝑖𝑛(){
𝑖𝑛𝑡 𝑎 = 2048, 𝑠𝑢𝑚 = 0;
𝑓𝑜𝑜 (𝑎, 𝑠𝑢𝑚);
𝑝𝑟𝑖𝑛𝑡𝑓 ("%𝑑𝑛", 𝑠𝑢𝑚);
}