ATT LAB (Answers) Internal
ATT LAB (Answers) Internal
ATT LAB (Answers) Internal
(Answers)
Input Output
a.5 2 4 7 12 21
#include<stdio.h>
int main()
{
int n,i,pr=2;
scanf("%d",&n);
printf("2 ");
for(i=0;i<n-1;i++)
{
pr=(pr*2)-i;
printf("%d ",pr);
}
}
Input Output
a.5 1 4 27 16 125
#include<stdio.h>
#include<math.h>
int main()
{
int n,i,a,b;
printf("Enter n value");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(i%2!=0)
{
a=pow(i,3);
printf("%d ",a);
}
else
{
b=pow(i,2);
printf("%d ",b);
}
}
return 0;
}
Input Output
a.10 1010
#include<stdio.h>
int convert(int dec)
{
if (dec == 0)
{
return 0;
}
else
{
return (dec % 2 + 10 * convert(dec / 2));
}
}
int main()
{
int dec, bin;
scanf("%d",&dec);
bin = convert(dec);
printf("%d",bin);
return 0;
}
4. Write a C program to find whether the given year is a leap year or not.
FORMAT: Input consist of 1 integer. Output consist of 1 string.
Input Output
a.2016
2016 is a leap year
#include<stdio.h>
int main()
{
int a;
scanf("%d", &a);
if(a%4==0)
printf("%d is a leap year", a);
else
printf("%d is not a leap year", a);
}
#include<stdio.h>
#include<malloc.h>
int** create(int m, int n)
{
int **a, i;
a= (int **)malloc(m*sizeof(int *));
for(i=0; i<m; i++)
{
*(a+i) = (int *)malloc(n*sizeof(int ));
}
return a;
}
void read(int **a, int m, int n)
{
int i, j;
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
scanf("%d", (*(a+i)+j));
}
}
}
void findMax(int** a,int m,int n)
{
int i,j,max;
for(i=0;i<m;i++)
{
max=-9999;
for(j=0;j<n;j++)
{
if(max < (*(*(a+i)+j)))
{
max=*(*(a+i)+j);
}
}
printf("%d\n",max);
}
}
int main()
{
int **a, m, n;
scanf("%d",&m);
scanf("%d",&n);
a = create(m,n);
read(a,m,n);
findMax(a,m,n);
return 0;
}
6. Write a C++ program to find the sum of first 'n' natural numbers by recursion.
INPUT & OUTPUT FORMAT: The input corresponds to the value of n. The output corresponds to the sum
of the values up to n.
Input Output
15
a.5
#include<iostream>
Using namespace std
int sum(int n)
{
if(n != 0)
return n + sum(n - 1);
else
return 0;
}
int main()
{
int n,result;
cin>>n;
result=sum(n);
cout<<result;
return 0;}
7.Write a program to find the sum of boundaries of a given matrix INPUT & OUTPUT FORMAT: Input
consists of 2 integers and 1 2D-array. Integers correspond to the size of rows and columns.
Input Output
3
3
582 Sum of boundaries is 41
368
159
#include<stdio.h>
#include<stdlib.h>
int main()
{
int **a,r,c,i,j;
scanf("%d",&r);
scanf("%d",&c);
a=(int**)malloc(r*sizeof(int*));
for(i=0;i<r;i++)
*(a+i)=(int*)malloc(c*sizeof(int));
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",*(a+i)+j);
}
}
i=0;int sum1=0;
for(j=0;j<c;j++)
sum1=sum1+*(*(a+i)+j);
i=r-1;int sum2=0;
if(i!=0)
{
for(j=0;j<c;j++)
sum2=sum2+*(*(a+i)+j);
}
j=0; int sum3=0;
for(i=1;i<r-1;i++)
sum3=sum3+*(*(a+i)+j);
j=c-1; int sum4=0;
for(i=1;i<r-1;i++)
sum4=sum4+*(*(a+i)+j);
printf("Sum of boundaries is %d",sum1+sum2+sum3+sum4);
return 0;
}
8.Write a C program to perform the binary search on an array using recursion.
Input Output
5
1
2
5
3
4
5
5
#include <stdio.h>
int recSearch ( int a[], int l, int r, int index)
{
if ( r < l)
return -1;
if ( a[l] == index)
return 1;
if (a[r] == index)
return 1;
return recSearch ( a, l+1, r-1, index);
}
int main()
{
int a[100];
int n;
scanf("%d",&n);
for ( int i = 0; i < n; i++ )
{
scanf("%d",&a[i]);
}
int x;
scanf("%d",&x);
int index = ( recSearch ( a , 0 , n-1 , x));
if ( index != -1){
printf("%d",x);}
else{
printf("The number is not present in the list");
}
}
9. Write a program to delete the vowels in the given string and display the string without vowels.
Input Output
face Fc
#include<stdio.h>
int main()
{
char s[100];
scanf("%s",&s);
for(int i=0;s[i]!='\0';i++){
s[i]=tolower(s[i]);
if(s[i]!='a'&&s[i]!='e'&&s[i]!='o'&&s[i]!='i'&&s[i]!='u'){
printf("%c",s[i]);
}
}
}
10. Write a C program to create a structure called employee with name, employee id, name, age
designation and salary as data members. Accept employee details and display it.
Input Output
Enter name:
Enter ID:
Enter age:
Rajarajan
Enter designation:
001
Enter Salary:
24
Employee Details
Manager
Name of the Employee : Rajarajan
20000
ID of the Employee : 1
Age of the Employee : 24
Designation of the Employee : Manager
Salary of the Employee : 20000.00
#include<stdio.h>
struct employee{
char name[100];
int id,age;
char desg[100];
int sal;
};
int main(){
struct employee e;
printf("Enter name:\nEnter ID:\nEnter age:\nEnter designation:\nEnter Salary:\n");
scanf("%s",&e.name);
scanf("%d %d %s %d",&e.id,&e.age,&e.desg,&e.sal);
printf("Employee Details");
printf("\nName of the Employee:%s",e.name);
printf("\nID of the Employee:%d",e.id);
printf("\nAge of the Employee:%d",e.age);
printf("\nDesignation of the employee:%s",e.desg);
printf("\nSalary of the Employee:%d",e.sal);
}
11. Given a string, write a program to find the first element which is non -repetitive i.e that element must
not be present anywhere else in the string. INPUT & OUTPUT FORMAT: The first line of the input consists
of a string. Assume the maximum size of the string as 50. The output displays a character which is non-
repetitive. If all the characters in an input string are repetitive, then display "All characters are
repetitive"(without quotes)
Input Output
teeterson R
#include<stdlib.h>
#include<stdio.h>
int *get_char_count(char *str)
{
int *count = (int *)calloc(sizeof(int), 256);
int i;
for (i = 0; *(str+i); i++)
count[*(str+i)]++;
return count;
}
free(count);
return index;
}
int main()
{
char str[256];
scanf("%s",&str);
int index = first_non_repeating_character(str);
if (index == -1)
printf("All the characters are repetitive");
else
printf("%c", str[index]);
getchar();
return 0;
}
12. These days kids are introduced to computers at a very early age. The kids are taught about alphabets,
digits and blank spaces. The teacher asked the students to count the vowels, consonants, digits and white
spaces in a string. The teacher found it a bit difficult to evaluate these tests and she knew that the 12th
class students are learning C programming. So she assigned this task to them to count the vowels,
consonants, digits and white spaces in a string. Can you please help them out? Write a program to count
the vowels, consonants, digits, white spaces, and symbols in a string. INPUT & OUTPUT FORMAT: Input
consists of a string. Assume the maximum length of the string is 200. The characters in the string can
contain both uppercase and lowercase. Refer sample input and output for formatting specifications.
Input Output
Vowels : 10
Consonants : 19
This program is very easy 2 complete White spaces : 6
Digits : 1
Symbols : 0
#include<stdio.h>
#include<string.h>
int main()
{
char str[1000];
int i, vowels, consonants, digits, spaces, symbols;
vowels = consonants = digits = spaces = symbols = 0;
gets(str);
for(i=0; str[i]!='\0'; ++i)
{
if(str[i]>='0' && str[i]<='9')
{
++digits;
}
else if (str[i]==' ')
{
++spaces;
}
else if(str[i]=='a' || str[i]=='e' || str[i]=='i' ||
str[i]=='o' || str[i]=='u' || str[i]=='A' ||
str[i]=='E' || str[i]=='I' || str[i]=='O' ||
str[i]=='U')
{
++vowels;
}
else if((str[i]>='a'&& str[i]<='z') || (str[i]>='A'&& str[i]<='Z'))
{
++consonants;
}
else
{
++symbols;
}
}
printf("Vowels:%d",vowels);
printf("\nConsonants:%d",consonants);
printf("\nWhite Spaces:%d", spaces);
printf("\nDigits:%d",digits);
printf("\nSymbols:%d", symbols);
return 0;
}
13. Write a C program to sort the given array in ascending order. FUNCTIONAL REQUIREMENTS: void
sort(int, int *);
Input Output
#include<stdio.h>
int main()
{
int i, j, a, t, n[100];
scanf("%d", &t);
for (i = 0; i < t; ++i)
scanf("%d", &n[i]);
for (i = 0; i < t; ++i)
{
for (j = i + 1; j < t; ++j)
{
if (n[i] > n[j])
{
a = n[i];
n[i] = n[j];
n[j] = a;
}
}
}
printf("Sorted array is:\n");
for (i = 0; i < t; ++i)
printf("%d\n", n[i]);
return 0;
}
14. Write a C program to find the power of a number using recursion.
Input Output
5
25
2
#include <stdio.h>
int power(int n1, int n2);
int main() {
int base, a, result;
scanf("%d", &base);
scanf("%d", &a);
result = power(base, a);
printf("%d", result);
return 0;
}
int power(int base, int a) {
if (a != 0)
return (base * power(base, a - 1));
else
return 1;
}
15. Write a C program to find whether the given number is a Fibonacci number. INPUT & OUTPUT
FORMAT: Input consists of 1 integer. If it is a Fibonacci number display “Fibonacci Number” or display “Not
Fibonacci Number”. The output consists of 1 integer.
Input Output
3 Fibonacci Number
#include<stdio.h>
#include<math.h>
int main()
{
int n,a,b,c,d;
scanf("%d",&n);
a=(5*n*n+4);
b=(5*n*n-4);
c=sqrt(a);
d=sqrt(b);
if(c*c==a || d*d==b)
{
printf("Fibonacci Number");
}
else
{
printf("Not Fibonacci Number");
}
return 0;
}