0% found this document useful (0 votes)
4 views

SP Practice Code for Theory Mid

Uploaded by

unofficialzihad
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)
4 views

SP Practice Code for Theory Mid

Uploaded by

unofficialzihad
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/ 4

1.

Given an array of 15 integers, find the maximum of the array


Solution:

#include<stdio.h>
int main()
{
int arr[15], i;
for(i = 0; i < 15; i++)
scanf("%d", &arr[i]);

int mx = arr[0];
for(i = 0; i < 15; i++)
{
if(mx < arr[i])
{
mx = arr[i];
}
}
printf("%d", mx);
return 0;
}

2. Given an array of 15 integers, find the minimum of the array


Solution:
#include<stdio.h>
int main()
{
int arr[15], i;
for(i = 0; i < 15; i++)
scanf("%d", &arr[i]);

int mn = arr[0];
for(i = 0; i < 15; i++)
{
if(mn > arr[i])
{
mn = arr[i];
}
}
printf("%d\n", mn);
return 0;
}

3. Write a program to find a character is vowel or not, if vowel print ‘Yes’, otherwise print ‘No’
#include<stdio.h>
int main()
{
char ch;
scanf("%c", &ch);
if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' ||
ch == 'U')
printf("Yes\n");
else
printf("No\n");
return 0;
}
4. Write a program to find a character is vowel or not, if vowel print ‘Vowel’, if consonant print
‘Consonant’, otherwise print ‘Not an Alphabet’
#include<stdio.h>
int main()
{
char ch;
scanf("%c", &ch);
if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' ||
ch == 'U')
printf("Vowel\n");
else if((ch > 'a' && ch <= 'z') || (ch > 'A' && ch <= 'Z'))
printf("Consonant\n");
else
printf("Not an Alphabet\n");
return 0;
}
5. Write a program to find a number is even or odd, if even print ‘EVEN’ if odd print ‘ODD’

#include<stdio.h>
int main()
{
int n;
scanf("%d", &n);
if(n % 2 == 0)
printf("EVEN\n");
else
printf("ODD\n");
return 0;
}
6. Write a C program to calculate the first N fibonacci numbers
#include<stdio.h>
int main()
{
int n;
scanf("%d", &n);
//without using array
// int f0 = 0, f1 = 1, f2 = f0 + f1;
// printf("%d %d %d ", f0, f1, f2);
// for(int i = 3; i < n; ++i)
// {
// f0 = f1, f1 = f2;
// f2 = f0 + f1;
// printf("%d ", f2);
// }
//code snippet using array
int arr[n];
arr[0] = 0, arr[1] = 1, arr[2] = arr[0] + arr[1];
printf("%d %d %d ", arr[0], arr[1], arr[2]);
for (int i = 3; i < n; ++i)
{
arr[i] = arr[i-1] + arr[i-2];
printf("%d ", arr[i]);
}

return 0;
}

7. Write a c program to calculate the factorial of an integer N using function


#include<stdio.h>
int calcFactorial(int n)
{
int fact = 1;
for (int i = 1; i <= n; ++i)
{
fact = fact*i;
}
return fact;
}
int main()
{
int n;
scanf("%d", &n);
int fact = calcFactorial(n);
printf("%d\n", fact);
}

Pre increment & Post increment Practice


Finding/Guessing Output:
Code 01:
#include<stdio.h>
int main()
{
int x = 5, y = 10;
x += y++;
++x;
printf("x = %d, y = %d\n", x, y);
--y, x++;
printf("x = %d, y = %d\n", x, y--);
y %= x++ + 2;
printf("x = %d, y = %d\n", x, y);
return 0;

Code 02:
#include<stdio.h>
int main()
{
int x = 5, y = 10;
x *= --y;
++x;
printf("x = %d, y = %d\n", x, y);
--y, x++;
printf("x = %d, y = %d\n", x, y--);
y %= 3 + ++x;
printf("x = %d, y = %d\n", ++x, y);
return 0;
}
Code 03:

#include<stdio.h>
int main()
{
int x = 5, y = 10;
++y;
printf("x = %d, y = %d\n", x, y*2);
--y, x++;
printf("x = %d, y = %d\n", ++x, y);
y %= 10 - ++x;
printf("x = %d, y = %d\n", x, y);
X %= ++y;
printf("x = %d, y = %d\n", x, y--);
return 0;
}

You might also like