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

C code for checking a number if it's a Armstrong number or not

 Important Coding Questions (With C Language + Code Snippet) Source: Click This Link Definition of Armstrong Number:  What is an Armstrong Number? A number is thought of as an Armstrong number if the sum of its own digits raised to the power number of digits gives the number itself.  For example, 153, 370, 371, 407 are three-digit Armstrong numbers  and, 1634, 8208, 9474 are  four-digit  Armstrong numbers and there are many more. Input: 1634 Output: 1634 is an Armstrong number. Input: 152 Output: 152  is not an Armstrong number. Code Snippet: #include <stdio.h> int main () {     int num , originalNum , remainder , result = 0 ;     printf ( "Enter a three-digit integer: " );     scanf ( " %d " , & num );     originalNum = num ;     while ( originalNum != 0 ) {         remainder = originalNum % 10 ;         result += remainder * remainder * ...

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 ...

How to code if two numbers are co-prime or not?

 C code for two numbers to check if they are co-prime or not Question: How can we understand if two numbers are coprime or not? Ans:  A Prime Number is defined as a Number which has no factor other than 1 and itself. But, Co-prime Numbers are Considered in pairs and two Numbers are  Co-prime  if they have a Common factor as 1 only . Their HCF is 1. Now come to the main point that is how to code it in C language so that we can get the correct output. C code snippet: #include <stdio.h> int main (){     int num1 , num2 , hcf ;     printf ( "Enter the first number: " );     scanf ( " %d " , & num1 );     printf ( "Enter the second number: " );     scanf ( " %d " , & num2 );     for ( int i = 1 ; i <= num1 ; i ++ )      {       if ( num1 % i == 0 && num2 % i == 0 )       {         hcf = i ;       ...