0% found this document useful (0 votes)
42 views7 pages

2CS101 Detailed List of Tutorials

Uploaded by

aditya jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views7 pages

2CS101 Detailed List of Tutorials

Uploaded by

aditya jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Nirma University

Institute of Technology
B.Tech. Semester I (CSE, EC and IC)
2CS101 Computer Programming
List of Tutorials

Mentioned tutorial exercises are indicative, depending on the understanding variations of the same
exercises can be carried out.
Sr. Tutorials Mapped
No. COs
1. Number system-based conversions
• Decimal to Other Base System
1
• Other Base System to Decimal
• Other Base System to Non-Decimal
2 Designing flowchart
A. Build logic and design flowchart for the following:
a. To check whether the number is prime or composite
b. To check whether the n-digit number is Armstrong number
c. To check whether the number is perfect number
d. To generate Fibonacci series
e. To add digits of a given number
f. To reverse a given number
g. To swap two numbers using a temporary number and without using a temporary number
1
h. To find greatest among two/three numbers
B. To discuss the output of following program segments:
main( ) main( ) main()
{ { {
printf ( "nn \n\n nn\n" ) ; int a, b ; signed char chr;
printf ( "nn /n/n nn/n" ) ; printf ( "Enter values of a and b" ) ; chr = 128;
} scanf ( " %d %d ", &a, &b ) ; printf("%d\n", chr);
printf ( "a = %d b = %d", a, b ) ; return 0;
} }
3 Based on operators and expressions
A. Which of the following are invalid variable names and why?
1. BASIC SALARY
2. 1basic
3. basic-hra
4. #MEAN
5. 422
6. population_in_2006
7. team’svictory
8. FLOAT
9. hELLO
B. To discuss the output of following program segments:
main( ) main( ) main()
{ { {
float a = 5, b = 2 ; int a, b ; enum days {MON=-1, TUE, 1,2,3
int c ; a = -3 - - 3 ; WED=6, THU, FRI, SAT};
c=a%b; b = -3 - - ( - 3 ) ; printf("%d, %d, %d, %d, %d,
printf ( "%d", c ) ; printf ( "a = %d b = %d", a, b ) ; %d\n", MON, TUE, WED, THU,
} } FRI, SAT);
}
main() main() main()
{ { {
int x = 10, y = 20, z = 5, i; int x=55; unsigned int m = 32;
i = x < y < z; printf("%d, %d, %d\n", printf("%x\n", ~m);
printf("%d\n", i); x<=55, x=40, x>=10); }
} }
main() main() main()
{ printf("%f", ((int)5.1) % { printf("%d", 1 << 2 + 3 << 4); { printf("%d",
((int)2.1)); } } printf("Nirma")); }
main() main() main()
{ { {
int i=-3, j=2, k=0, m; int i=-3, j=2, k=0, m; int x=12, y=7, z;
m = ++i && ++j && ++k; m = ++i || ++j && ++k; z = x!=4 || y == 2;
printf("%d, %d, %d, %d\n", i, printf("%d, %d, %d, %d\n", printf("z=%d\n", z);
j, k, m); i, j, k, m); }
} }
main() main() main()
{ { {
int x=4, y, z; int k, num=30; enum days {MON=-1, TUE,
y = --x; k = (num>5 ? (num <=10 ? WED=6, THU, FRI, SAT};
z = x--; 100 : 200): 500); printf("%d, %d, %d, %d, %d,
printf("%d, %d, %d\n", x, y, printf("%d\n", num); %d\n", ++MON, TUE, WED,
z); } THU, FRI, SAT);
} }

4 Decision making structures


A. Develop logic for the following:
a. To print a three-digit integer in reverse and in words. (e.g. if input is 345, output should be Five
Four Three)
b. Write a program to find out whether a given year is leap or not.
Hint: A leap year is a year which is evenly divisible by 4, but if it is evenly divisible by 100, then it
is not a leap year, but if it is evenly divisible by 400, then it is a leap year.
B. To discuss the output of following program segments:
main( ) main( ) main()
{ { {
int a = 300, b, c ; int x = 3, y = 5 ; int x = 3;
if (a >= 400 ) if ( x == 3 ) float y = 3.0;
b = 300 ; printf ( "\n%d", x ) ; if(x == y)
c = 200 ; else ; printf("x and y are equal");
printf("%d, %d, %d\n", a, b, c); printf ( "\n%d", y ) ; else
} } printf("x and y are not equal");
}
main() main() main()
{ { {
int i = 1; int i=3; int i=4;
switch(i) switch(i) switch(i)
{ { { 1,2,3
printf("Hello\n"); case 1: default:
case 1: printf("Hello\n"); printf("This is default\n");
printf("Hi\n"); case 2: case 1:
break; printf("Hi\n"); printf("This is case 1\n");
case 2: case 3: break;
printf("\nBye\n"); continue; case 2:
break; default: printf("This is case 2\n");
} printf("Bye\n"); break;
} } case 3:
} printf("This is case 3\n");
}
}
main() main() main()
{ { {
float a = 0.7; int a = 11, b = 5; int i = 0;
if(0.7 > a) if(a = 5) b++; if (i == 0)
printf("Hi\n"); printf("%d %d", ++a, b++); {
else } printf("Hello");
printf("Hello\n"); continue;
} }
}
5 Looping constructs
A. Develop logic for the following:
a. To print following patterns:
1 * 1
AB *** 1 2
234 ***** 12 3 1,2,3
CDEF ****** 1234
b. Calculate sum of series.

c. To display Pascal Triangle.


6. Looping constructs
A. To discuss the output of following program segments:
main( ) main( ) main()
{ { {
int i ; while ( '1' < '2' ) int i = 0;
for ( i = 1 ; i <= 5 ; printf ( printf ( "\nIn while loop" ) ; for(; i<=5; i++);
"\n%c", 65 ) ) ; } printf("%d", i);
i++ ; }
}
main( ) main( ) main()
{ { {
int x = 1 ; char x ; short int i = 0;
while ( x == 1 ) for ( x = 0 ; x <= 255 ; x++ ) for(i<=5 && i>=-1; ++i;
{ printf ( "\nAscii value %d i>0)
x=x-1; Character %c", x, x ) ; printf("%u,", i);
printf ( "\n%d", x ) ; } }
}
}
main() main() main()
{ { {
int i = 5; int i = 0, j = 0; int x;
while(i-- >= 0) while (i < 2) for(x=-1; x<=10; x++)
printf("%d,", i); { { 1,2,3
i = 5; l1 : i++; if(x < 5)
printf("\n"); while (j < 3) continue;
while(i-- >= 0) { else
printf("%i,", i); printf("Loop\n"); break;
while(i-- >= 0) goto l1; printf("Nirma");
printf("%d,", i); } }
} } }
}
main() int main() main( )
{ { {
int i=1; int x; int x = 4, y = 0, z ;
for(;;) for(x=-1; x<=10; x++) while ( x >= 0 )
{ { {
printf("%d\n", i++); if(x < 5) x-- ;
if(i>10) continue; y++ ;
break; else if ( x == y )
} break; continue ;
} printf("Nirma"); else
} printf ( "\n%d
return 0; %d", x, y ) ;
} }
}
7. Arrays
A. Develop logic for the following: 1,2,3
a. To find number of odd and even elements in a one-dimensional array.
b. To sort one dimensional array in an ascending or descending order as per the user choice.
c. To insert an element in 1-D array at specified location.
d. To perform matrix addition and matrix multiplication.
B. To discuss the output of following program segments:
main() main()
{ {
int a[5] = {5, 1, 15, 20, 25}; int arr[5], i=0;
int i, j, m; while(i<5)
i = ++a[1]; arr[i]=++i;
j = a[1]++;
m = a[i++]; for(i=0; i<5; i++)
printf("%d, %d, %d", i, j, m); printf("%d, ", arr[i]);
} }
main() What will be the output of the program if the
{ array begins at 65472 and each integer
int arr[] = {12, 14, 15, 23, 45}; occupies 2 bytes?
printf("%u, %u\n", arr, &arr); main()
} {
int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 1, 7, 8, 9, 0};
printf("%u \n", a+1);
}
main() main()
{ {
float arr[ ] = {12.4, 2.3, 4.5, 6.7}; int i;
printf("%d\n", sizeof(arr)/sizeof(arr[0])); int arr[5] = {1};
} for (i = 0; i < 5; i++)
printf("%d ", arr[i]);
}
Does C perform array out of bound checking? main()
main() {
{ int a[ ][ ] = {{1,2},{3,4}};
int i; int i, j;
int arr[5] = {0}; for (i = 0; i < 2; i++)
for (i = 0; i <= 5; i++) for (j = 0; j < 2; j++)
printf("%d ", arr[i]); printf("%d ", a[i][j]);
} }
main() main()
{ {
int size = 4; int a[2][3] = {1, 2, 3, 4, 5};
int arr[size]; int i = 0, j = 0;
if(arr[0]) for (i = 0; i < 2; i++)
printf("Initialized to ZERO"); for (j = 0; j < 3; j++)
else printf("%d ", a[i][j]);
printf("Not initialized to }
ZERO");
}
#define SIZE 5 main ()
main() {
{ int i, j;
int expr; int a [8] = {1, 2, 3, 4, 5, 6, 7, 8};
double value[SIZE]={1.0,2.0,3.0,4.0,5.0}; for(i = 0; i < 3; i++) {
expr=1|2|3; a[i] = a[i] + 1;
printf("%d ",expr); i++;
printf("%f",value[expr]); }
} i--;
for (j = 7; j > 4; j--) {
int i = j/2;
a[i] = a[i] - 1;
}
printf ("%d, %d", i, a[i]);
}
8. String manipulation
A. Write a program to suppress all capital letters at the bottom of character array. E.g. input is Nirma
University, output should be irma niversityNU.
B. To discuss the output of following program segments:
main( ) main( ) main ()
{ { {
int array[26], i ; char c[2] = "A" ; char a [6] = "world";
for ( i = 0 ; i <= 25 ; i++ ) printf ( "\n%c", c[0] ) ; int i, j;
{ printf ( "\n%s", c ) ; for (i = 0, j = 5; i < j; a [i++]
array[i] = 'A' + i ; } = a [j--]);
printf ( "\n%d %c", array[i], printf ("%s\n", a);
array[i] ) ; }
}
}
main() main() main()
{ { {
char str1[] = "abcd"; char str1[20] = "Hello", char str[10] = "Angel";
char str2[] = "abcd"; str2[20] = " World"; str[6] = 'd';
if(str1==str2) printf("%s\n", printf("%s\n", str);
printf("Equal"); strcpy(str2, strcat(str1, }
else str2)));
printf("Unequal"); }
} 1,2,3
main() main() main()
{ { {
char str1[] = "dills"; char str[20] = char sentence[80];
char str2[20]; "GeeksQuiz"; int i;
char str3[] = "Daffo"; printf ("%d", sizeof(str)); printf("Enter a line of
int i; } text\n");
i = strcmp(strcat(str3, gets(sentence);
strcpy(str2, str1)), "Daffodills"); for(i=strlen(sentence)-1; i
printf("%d\n", i); >=0; i--)
} putchar(sentence[i]);
}
main() main() main()
{ { {
char str[] = "Nagpur"; char p[20]; int i;
str[0]='K'; char s[] = "string"; char a[] = "\0";
printf("%s, ", str); int length = strlen(s); if(printf("%s", a))
str = "Kanpur"; int i; printf("\nThe string is
printf("%s", str+1); for (i = 0; i < length; i++) not empty\n");
} p[i] = s[length - i]; else
printf("%s", p); printf("\nThe string is
} empty\n");
}
9. User defined functions and parameter passing
A. Write pseudocode for following:
a. Write a procedure which accepts a string and an integer. It should display the passed string the
specified integer number of times.
b. Write a procedure which accepts a one-dimensional array as an argument and returns the sum
of all elements to the main function.
B. To discuss the output of following program segments:
main( ) main( ) main() 1,2,3
{ { {
printf ( "\nC to it that C survives" printf ( "\nOnly stupids use C?"); int fun(int);
); display( ) ; int i = fun(10);
main( ) ; } printf("%d\n", --i);
} display( ) }
{ int fun(int i)
printf ( "\nFools too use C!" ) ; {
main( ) ; } return (i++); }
int i; int reverse(int); int sumdig(int);
int fun(); main() main()
main() { {
{ int no=5; int a, b;
while(i) reverse(no); a = sumdig(123);
{ } b = sumdig(123);
fun(); int reverse(int no) printf("%d, %d\n", a, b);
main(); { }
} if(no == 0) int sumdig(int n)
printf("Hello\n"); return 0; {
} else int s, d;
int fun() printf("%d,", no); if(n!=0)
{ reverse (no--); {
printf("Hi"); } d = n%10;
} n = n/10;
s = d+sumdig(n);
}
else
return 0;
return s;
}
int check(int); int func1(int); int addmult(int ii, int jj)
main() int main() {
{ { int kk, ll;
int i=45, c; int k=35; kk = ii + jj;
c = check(i); k= ll = ii * jj;
printf("%d\n", c); func1(k=func1(k=func1(k))); return (kk, ll);
} printf("k=%d\n", k); }
int check(int ch) return 0; main()
{ } {
if(ch >= 45) int func1(int k) int i=3, j=4, k, l;
return 100; { k = addmult(i, j);
else k++; l = addmult(i, j);
return 10; return k; printf("%d %d\n", k, l);
} } }
10. Structure and Union
A. To discuss the output of following program segments:
main( ) struct sample {
{ int a = 0;
struct gospel char b = 'A';
{ float c = 10.5;
int num ; };
char mess1[50] ; main()
char mess2[50] ; {
}m; struct sample s;
m.num = 1 ; printf("%d, %c, %f", s.a, s.b, s.c);
strcpy ( m.mess1, "If all that you have is hammer" ) }
;
1,2,3
strcpy ( m.mess2, "Everything looks like a nail" ) ;
/* assume that the strucure is located at address
1004 */
printf ( "\n%u %u %u", &m.num, m.mess1,
m.mess2 ) ;
}
main() struct st
{ {
typedef struct tag { int x;
char str[10]; struct st next;
int a; };
} har; main()
{
har h1, h2 = { "IHelp", 10 }; struct st temp;
h1 = h2; temp.x = 10;
h1.str[1] = 'h'; temp.next = temp;
printf("%s, %d", h1.str, h1.a); printf("%d", temp.next.x);
} }

11. Pointers
A. To discuss the output of following program segments.
main() main()
{ {
int i=3, *j, k; char str[20] = "Hello";
j = &i; char *const p=str;
printf("%d\n", i**j*i+*j); *p='M';
} printf("%s\n", str);
}
main() main()
{ {
int x=30, *y, *z; char str[] = "peace";
y=&x; /* Assume address of x is 500 and char *s = str;
integer is 4 byte size */ printf("%s\n", s++ +3);
z=y; }
*y++=*z++;
1,2,3
x++;
printf("x=%d, y=%d, z=%d\n", x, y, z);
}
main() main()
{ {
int i, a[] = {2, 4, 6, 8, 10}; char str1[] = "Computer";
change(a, 5); char str2[] = "Nirma";
for(i=0; i<=4; i++) char *s1 = str1, *s2=str2;
printf("%d, ", a[i]); while(*s1++ = *s2++)
} printf("%s", str1);
void change(int *b, int n) printf("\n");
{ }
int i;
for(i=0; i<n; i++)
*(b+1) = *(b+i)+5;
}
12. File Processing
A. Write C program for the following:
a. To calculate the length of a file 1,2
b. To concatenate two files
c. To copy content of one file in to the another file

You might also like