Skip to main content

Academy Of Technology (2nd Internal)

 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. 


Thank you so much. Best of luck for your upcoming exam. Don't forget to share it with your friends. 


Comments

Popular posts from this blog

What is the Difference Between Web Development and Android Development?

 What is the Difference Between Web Development and Android Development? While many people believe that web development and Android development are the same thing, this couldn’t be further from the truth. While both fields involve code and computers, the two actually have significantly different processes, tools, and skillsets required to complete their work. In this article, we’ll explore some of the key differences between web development and Android development in order to help you decide which profession may be better suited to your skill level and interests. Web Development vs Android Development The Ultimate Android vs Web Developers Showdown : There’s a massive amount of discussion around which type of development, web or android, is better. Both sides have passionate proponents who make compelling arguments. We don’t want to get into that argument. Instead, we’re going to look at both types of development and point out some strengths and weaknesses that make each different ...

Frequency Of Elements in an Array

  Algorithm Step 1: Create a map of integer to store key-value pairs. map<int,int> Step 2: Now iterate over all the elements of the array and count their frequency by the help of map Step 3: Now make a iterator for map to iterate over the map items Step 4: This is the final step. Just print all the elements and their frequency stored in map respectively  Code

C code to calculate product of even digits of a given number

 C code to calculate product of even digits of a given number Code Steps: 1. First input the number 2. Now initialize a variable =1, that stores the multiplication  3. Now start a while loop until number = 0 4. Inside while loop just find out all the digits by finding the remainder & check it if it is divisible by 2  5. If the number is divisible by 2 then multiply that number with the variable that was initialize = 1 6. At last return the multiplication storing variable.  C Code Snippet #include <stdio.h> int main (){     int num ;     int product = 1 ;     printf ( "Enter the number: " );     scanf ( " %d " , & num );     int temp = num ;     while ( num != 0 ){                 int digit = num % 10 ;         num = num / 10 ;         if ( digit % 2 == 0 ){         ...