ES-CS201 (Programming Solution Of Question Bank)
This is only for 2nd Semester Students
Hi, I am Arnab. Here you can find out full solution of codes given in the question bank of AOT for ES-CS201 subject. So don't panic for your examination. Just copy down the codes from my website & run in your local machine to get more clear concepts. If you have any query please contact me without any hesitation from here.
So guys are you ready to get full marks???
Let's start
Q1. Write a C program to calculate the sum of all negative and positive elements in an array.
// Code to calculate all positive and negative values in an array
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
int arr[n];
for(int i = 0; i < n; i++){
scanf("%d",&arr[i]);
}
int sum_of_pos = 0;
int sum_of_neg = 0;
for(int i: arr){
if(i<0){
sum_of_neg += i;
}
else{
sum_of_pos += i;
}
}
printf("%d\n", sum_of_pos);
printf("%d\n", sum_of_neg);
return 0;
}
Q2. Write a C program to check whether a year is leap year or not.
// Code to check leapyear
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
if(n%4==0){
if(n%100==0){
if(n%400==0){
printf("Leapyear");
}
else{
printf("Not a Leapyear");
}
}
else{
printf("Leapyear");
}
}
else{
printf("Not a Leapyear");
}
return 0;
}
Q3. Write a function to find the summation of 1st
n natural numbers and test the function by calling from
main function.
//Sum of first n numbers
#include <stdio.h>
int sum_of_n_numbers(int n){
int sum = 0;
for(int i = 1; i <= n; i++){
sum += i;
}
return sum;
}
int main(){
int n;
scanf("%d",&n);
printf("%d",sum_of_n_numbers(n));
return 0;
}
Q4. Write a function to find Fibonacci sequence upto n term.
//Fibonacci Series
#include <stdio.h>
int fibbonacci(int n){
if(n<=1){
return n;
}
else{
return fibbonacci(n-1) + fibbonacci(n-2);
}
}
int main(){
int n;
scanf("%d",&n);
for(int i = 0; i < n; i++){
printf("%d ",fibbonacci(i));
}
return 0;
}
Q5. Write a function to find factorial of a positive number.
//Find factorial
#include <stdio.h>
int factorial(int n){
int ans = 1;
for(int i = 1; i <= n; i++){
ans *= i;
}
return ans;
}
int main(){
int n;
scanf("%d",&n);
printf("%d", factorial(n));
return 0;
}
Q6. Write a C function to swap two integer data and call the function from the main() function. Don’t use
any third variable.
//swap two number without using 3rd variable
#include<stdio.h>
void swap(int a, int b){
printf("Swapping is in progress...\n");
a = a+b;
b = a-b;
a = a-b;
printf("a = %d\n",a);
printf("b = %d", b);
}
int main(){
int a,b;
printf("Enter 2 numbers: \n");
scanf("%d%d",&a,&b);
swap(a,b);
return 0;
}
Q7. Write a C program to check whether a number is Armstrong number or not using function.
//check armstrong number
#include<stdio.h>
#include<math.h>
void checkArmstrong(int n){
int temp = n;
int duplicate = n;
int count = 0;
while(temp>0){
count++;
temp = temp/10;
}
int armstrong = 0;
while(duplicate>0){
int x = duplicate%10;
duplicate = duplicate/10;
armstrong = armstrong + pow(x,count);
}
if(armstrong==n){
printf("armstrong");
}
else{
printf("not armstrong");
}
}
int main(){
int n;
scanf("%d",&n);
checkArmstrong(n);
return 0;
}
Q8. Find the largest number among n numbers using user defined function.
//find max of all numbers
#include <stdio.h>
#include <limits.h>
int max(int arr[], int n){
int max_element = INT_MIN;
for (int i = 0; i < n; i++)
{
if(arr[i]>max_element){
max_element = arr[i];
}
}
return max_element;
}
int main(){
int n;
scanf("%d",&n);
int arr[n];
for(int i = 0; i < n; i++){
scanf("%d",&arr[i]);
}
int max_element = max(arr,n);
printf("Max Number: %d",max_element);
return 0;
}
Q9. Write a C program to find all factors of a number using function.
//C program to find all the factors
#include <stdio.h>
void factors(int n){
for(int i = 1; i <= n; i++){
if(n%i==0){
printf("%d ",i);
}
}
}
int main(){
int n;
scanf("%d",&n);
printf("Factors of %d\n",n);
factors(n);
return 0;
}
Q10. Write a C code to insert an element at a particular position of an array.
//C code to insert any value to a given position in an array
#include<stdio.h>
int main(){
int n;
scanf("%d",&n);
int arr[1000];
for(int i = 0; i < n; i++){
scanf("%d",&arr[i]);
}
int index;
scanf("%d",&index); //at what index insert an element
int value; //which value you want to store
scanf("%d",&value);
if(index==n){
arr[index] = value;
}
else if(index>n){
printf("Error!!");
return 0;
}
else{
for (int k = 0; k < n; k++)
{
if(k==index){
for(int j = n-1; j >= index; j--){
arr[j+1] = arr[j];
}
arr[index] = value;
break;
}
}
}
for (int i = 0; i < n+1; ++i)
{
printf("%d ",arr[i]);
}
return 0;
}
Q11. Write a C code to calculate the GCD of two numbers.
//Find GCD of two numbers
#include<stdio.h>
void GCD(int a, int b){
int gcd = 1;
int max = a>b ? a : b;
for(int i = 2; i <= max; i++){
if(a%i==0&&b%i==0){
gcd = i;
}
}
printf("GCD: %d",gcd);
}
int main(){
int a,b;
scanf("%d%d",&a,&b);
GCD(a,b);
return 0;
}
Term Paper Question
Q1. (a) Can array be considered as subscripted variable? Justify your answer.
(b) Find the values of integer variables c and d with respect to the integer array a[5]= {22, 11, 33} and
two programming statements mentioned below : (You must mention the logic behind your
answer.)
i) c = a[2];
ii) d = 2[a];
Ans:
(a) All elements refer to same name. That is, each element can be identified with the same name including different index value (subscript value). Hence, an array is also called as a subscripted variable.
(b) Value of c & d both are same that is 33. As array is a subscripted variable so with same array name & same index value we can get the same output. That's why a[i] = i[a]. So, here a[2] = 2[a] = 33.
Comments
Post a Comment