1. Cloud IT Solution Page 347
Hello world C program
#include <stdio.h>
int main()
{
printf("Hello worldn");
return 0;
}
Output: Hello world
Infinite while loop
#include <stdio.h>
int main()
{
while (1) // This is always true so the loop will
execute forever
printf("Hello Worldn");
return 0;
}
Output:
Hello world
Infinite loop
Infinite for loop
#include <stdio.h>
#include <stdio.h>
int main () {
for( ; ; ) {
printf("This loop will run forever.n");
}
return 0;
}
Output:
This loop will run forever.
Infinite loop
Programming C
2. Cloud IT Solution Page 348
For and while loop
#include <stdio.h>
int main () {
int a;
/* for loop execution */
for( a = 0; a <= 5; a++){
printf(" %d ", a);
}
return 0;
}
#include <stdio.h>
int main () {
int a;
/* for loop execution */
a=0;
while(a<=5){
printf("%d ", a);
a++;
}
return 0;
}
Output:
0 1 2 3 4 5
Increment and decrement operator
#include <stdio.h>
int main()
{
int a = 10, b = 100;
float c = 10.5, d = 100.5;
printf("++a = %d n", ++a);
printf("--b = %d n", --b);
printf("++c = %f n", ++c);
printf("--d = %f n", --d);
printf(".............n");
a = 10;
b = 100;
c = 10.5;
d = 100.5;
printf("a++ = %d n", a++);
printf("b-- = %d n", b--);
printf("c++ = %f n", c++);
printf("d-- = %f n", d--);
3. Cloud IT Solution Page 349
printf(".............n");
printf("a = %d n", a);
printf("b= %d n", b);
printf("c= %f n", c);
printf("d= %f n", d);
return 0;
}
Output:
++a = 11
--b = 99
++c = 11.500000
++d = 99.500000
………………………………………….
a++ = 10
b-- = 100
c++ = 10.500000
d-- = 100.500000
……………………………………………..
a = 11
b = 99
c = 11.500000
d = 99.500000
Add two numbers using a function
#include<stdio.h>
long addition(long, long);
main()
{
long first, second, sum;
scanf("%ld%ld", &first, &second);
sum = addition(first, second);
printf("%ldn", sum);
return 0;
}
long addition(long a, long b)
{
long result;
result = a + b;
return result;
}
Input: 10 20
Output: 30
Add number using pointers
4. Cloud IT Solution Page 350
#include <stdio.h>
long add(long *, long *);
int main()
{
long first, second, *p, *q, sum;
printf("Input two integers to addn");
scanf("%ld%ld", &first, &second);
sum = add(&first, &second);
printf("(%ld) + (%ld) = (%ld)n", first, second, sum);
return 0;
}
long add(long *x, long *y) {
long sum;
sum = *x + *y;
return sum;
}
Input: 10 20
Output: 30
Odd or even check using conditional operator
#include <stdio.h>
int main()
{
int n;
printf("Input an integern");
scanf("%d", &n);
n%2 == 0 ? printf("Evenn") : printf("Oddn");
return 0;
}
Input: 10
Output: Even
Add, subtract, multiply and divide
#include <stdio.h>
int main()
{
int first, second, add, subtract, multiply;
float divide;
printf("Enter two integersn");
scanf("%d%d", &first, &second);
5. Cloud IT Solution Page 351
add = first + second;
subtract = first - second;
multiply = first * second;
divide = first / (float)second; //typecasting
printf("Sum = %dn", add);
printf("Difference = %dn", subtract);
printf("Multiplication = %dn", multiply);
printf("Division = %.2fn", divide);
return 0;
}
Input: 20 10
Output:
Sum=30
Difference=10
Multiplication=200
Division=2.00
Check vowel using switch statement
#include <stdio.h>
int main()
{
char ch;
printf("Input a charactern");
scanf("%c", &ch);
switch(ch)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
printf("%c is a vowel.n", ch);
break;
default:
printf("%c isn't a vowel.n", ch);
}
return 0;
}
6. Cloud IT Solution Page 352
Input: a
Output: a is a vowel
Switch statement
#include <stdio.h>
int main()
{
char alphabet;
printf("Enter an alphabet:");
scanf("%c",&alphabet);
switch(alphabet)
{
case 'a':
printf("Alphabet a is a vowel.n");
break;
case 'e':
printf("Alphabet e is a vowel.n");
break;
case 'i':
printf("Alphabet i is a vowel.n");
break;
case 'o':
printf("Alphabet o is a vowel.n");
break;
case 'u':
printf("Alphabet u is a vowel.n");
break;
default:
printf("You entered a consonant.n");
break;
}
return 0;
}
Input: Enter an alphabet: a
Output: Alphabet a is a vowel.
7. Cloud IT Solution Page 353
Leap year
#include <stdio.h>
int main()
{
int year;
printf("Enter a year to check if it is a leap yearn");
scanf("%d", &year);
if ( year%400 == 0) // Exactly divisible by 400 e.g.
1600, 2000
printf("%d is a leap year.n", year);
else if ( year%100 == 0) // Exactly divisible by 100 and
not by 400 e.g. 1900, 2100
printf("%d isn't a leap year.n", year);
else if ( year%4 == 0 ) // Exactly divisible by 4 and
neither by 100 nor 400 e.g. 2016, 2020
printf("%d is a leap year.n", year);
else // Not divisible by 4 or 100 or 400 e.g. 2017, 2018,
2019
printf("%d isn't a leap year.n", year);
return 0;
}
Input: Enter a year to check if it is a leap year: 2018
Output: 2018 isn't a leap year.
Palindrome number
#include <stdio.h>
int main()
{
int n, reverse = 0, t;
printf("Enter a number to check if it is a palindrome or
notn");
scanf("%d", &n);
t = n;
while (t != 0)
{
reverse = reverse * 10;
reverse = reverse + t%10;
t = t/10;
}
8. Cloud IT Solution Page 354
if (n == reverse)
printf("%d is a palindrome number.n", n);
else
printf("%d isn't a palindrome number.n", n);
return 0;
}
Input: Enter a number to check if it is a palindrome or not 121
Output: 121 is a palindrome number.
Perfect number
#include <stdio.h>
int main()
{
int number, rem, sum = 0, i;
printf("Enter a Numbern");
scanf("%d", &number);
for (i = 1; i <= (number - 1); i++)
{
rem = number % i;
if (rem == 0)
{
sum = sum + i;
}
}
if (sum == number)
printf("Entered Number is perfect number");
else
printf("Entered Number is not a perfect number");
return 0;
}
Input: Enter a Number 121
Output: Entered Number is perfect number.
Factorial using recursion
#include<stdio.h>
long factorial(int n);
int main()
{
int n;
long f;
9. Cloud IT Solution Page 355
printf("Enter an integer to find its factorialn");
scanf("%d", &n);
if (n < 0)
printf("Factorial of negative integers isn't
defined.n");
else
{
f = factorial(n);
printf("%d! = %ldn", n, f);
}
return 0;
}
long factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
Input: Enter an integer to find its factorial 3
Output: 3!=6
GCD and LCM
#include <stdio.h>
int main() {
int a, b, x, y, t, gcd, lcm;
printf("Enter two integersn");
scanf("%d%d", &x, &y);
a = x;
b = y;
while (b != 0) {
t = b;
b = a % b;
a = t;
}
gcd = a;
lcm = (x*y)/gcd;
printf("Greatest common divisor of %d and %d = %dn", x,
y, gcd);
10. Cloud IT Solution Page 356
printf("Least common multiple of %d and %d = %dn", x, y,
lcm);
return 0;
}
int gcd(int a,int b){
if(b==0) return a;
else return gcd(b,a%b);
}
Input: Enter two integers 4 2
Output:
Greatest common divisor of 4 and 2 =2
Least common multiple of 4 and 2=4
Binary to decimal
#include <stdio.h>
#include <math.h>
int convertBinaryToDecimal(long long n);
int main()
{
long long n;
printf("Enter a binary number: ");
scanf("%lld", &n);
printf("%lld in binary = %d in decimal", n,
convertBinaryToDecimal(n));
return 0;
}
int convertBinaryToDecimal(long long n)
{
int decimalNumber = 0, i = 0, remainder;
while (n!=0)
{
remainder = n%10;
n /= 10;
decimalNumber += remainder*pow(2,i);
i++;
}
return decimalNumber;
}
Input: Enter a binary number: 100
Output: 100 in binary =4 in decimal
11. Cloud IT Solution Page 357
Decimal to binary
#include <stdio.h>
#include <math.h>
long long convertDecimalToBinary(int n);
int main()
{
int n;
printf("Enter a decimal number: ");
scanf("%d", &n);
printf("%d in decimal = %lld in binary", n,
convertDecimalToBinary(n));
return 0;
}
long long convertDecimalToBinary(int n)
{
long long binaryNumber = 0;
int remainder, i = 1, step = 1;
while (n!=0)
{
remainder = n%2;
printf("Step %d: %d/2, Remainder = %d, Quotient =
%dn", step++, n, remainder, n/2);
n /= 2;
binaryNumber += remainder*i;
i *= 10;
}
return binaryNumber;
}
Input: Enter a decimal number: 4
Output: 4 in decimal =100 in binary
Find nCr and nPr using function
#include <stdio.h>
long factorial(int n);
long find_ncr(int n, int r);
long find_npr(int n, int r);
int main()
{
int n, r;
long ncr, npr;
12. Cloud IT Solution Page 358
printf("Enter the value of n and rn");
scanf("%d%d",&n,&r);
ncr = find_ncr(n, r);
npr = find_npr(n, r);
printf("%dC%d = %ldn", n, r, ncr);
printf("%dP%d = %ldn", n, r, npr);
return 0;
}
long find_ncr(int n, int r) {
long result;
result = factorial(n)/(factorial(r)*factorial(n-r));
return result;
}
long find_npr(int n, int r) {
long result;
result = factorial(n)/factorial(n-r);
return result;
}
long factorialloop(int n) {
int i;
long result = 1;
for (i = 1; i <= n; i++)
result = result*i;
return result;
}
long factorial(int n){
if(n<=0) return 1;
else return (n*factorial(n-1));
}
Input: Enter the value of n and r 4 2
Output:
4C2=6
4P2=12
13. Cloud IT Solution Page 359
Pattern matching
#include <stdio.h>
int main()
{
int i,j,k,count;
count=1;
printf("Enter value: ");
scanf("%d",&n);
for(i=1;i<=n;i++){
for(j=1;j<=n-i;j++){
printf(" ");//space
}
for(k=1;k<=count;k++){
printf("*");
}
printf("n");
count=count+2;
}
return 0;
}
Input: Enter value: 4
Output:
*
***
*****
*******
Pattern matching
#include <stdio.h>
int main()
{
int n, i, j;
printf("Enter number of rowsn");
scanf("%d",&n);
for ( i = 1 ; i <= n ; i++ )
{
for( j = 1 ; j <= i ; j++ )
14. Cloud IT Solution Page 360
printf("*");
printf("n");
}
return 0;
}
Input: Enter number of rows 5
Output:
*
**
***
****
*****
Pattern matching
#include <stdio.h>
int main()
{
int n, num = 1, i, j, k;
printf("Enter the number :");
scanf("%d",&n);
for ( i = 1 ; i <= n ; i++ )
{
num = i;
for ( j = 1 ; j<= n-i ; j++ )
printf(" ");
for ( k = 1 ; k <= i ; k++ )
{
printf("%d", num);
num++;
}
num--;
num--;
for ( k = 1 ; k < i ; k++)
{
printf("%d", num);
num--;
}
15. Cloud IT Solution Page 361
printf("n");
}
return 0;
}
Input: Enter the number 5
Output:
1
232
34543
4567654
567898765
Diamond pattern
#include <stdio.h>
int main()
{
int n, count = 1;
int i, j, k;
printf("Enter number of rows::");
scanf("%d", &n);
//Starting *****
for ( i= 1; i <= n; i++)
{
for (j = 1; j <= n-i; j++)
printf(" ");
for (k = 1; k<= count; k++)
printf("*");
count=count+2;
printf("n");
}
//ending *****
count=count-4;
for ( i= 1; i <= n-1; i++)
{
for (j = 1; j <= i; j++)
printf(" ");
16. Cloud IT Solution Page 362
for (k = 1; k<= count; k++)
printf("*");
count=count-2;
printf("n");
}
return 0;
}
Input: Enter the number 5
Output:
*
***
*****
***
*
Pascal pattern
#include <stdio.h>
#include<conio.h>
int factorial(int n);
int main(){
int i,j,n,result,count,k;
count=1;
printf("Enter the value is ::");
scanf("%d",&n);
count=0;
for(i=0;i<n;i++){
for(j=0;j<=n-i-2;j++){
printf(" ");
}
for(k=0;k<=i;k++){
printf("%ld
",factorial(i)/(factorial(k)*factorial(i-k)));
}
printf("n");
}
return 0;
}
int factorial(int n){
17. Cloud IT Solution Page 363
if(n<=1) return 1;
else return (n*factorial(n-1));
}
Input: Enter the value is :4
Output:
1
1 1
1 2 1
1 3 3 1
Prime number
#include <stdio.h>
#include<conio.h>
int prime(int);
int main(){
int i,j,n,result;
printf("Enter the value :");
scanf("%d",&n);
result=prime(n);
if(result==0) printf("is not a prime no");
else printf("is a prime no");
return 0;
}
int prime(int n){
int i;
if(n<=1) return 1;
for(i=2;i<=n/2;i++){
if(n%i==0) return 0;;
}
return 1;
}
Input: Enter the value: 4
Output: is not a prime no.
Prime factors
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
int main()
18. Cloud IT Solution Page 364
{
int i, j, num, isPrime;
printf("Enter any number to print Prime factors: ");
scanf("%d", &num);
printf("All Prime Factors of %d are: n", num);
/* Find all Prime factors */
for(i=2; i<=num; i++)
{
/* Check 'i' for factor of num */
if(num%i==0)
{
/* Check 'i' for Prime */
isPrime = 1;
for(j=2; j<=i/2; j++)
{
if(i%j==0)
{
isPrime = 0;
break;
}
}
/* If 'i' is Prime number and factor of num */
if(isPrime==1)
{
printf("%d, ", i);
}
}
}
return 0;
}
Input: Enter any number to print Prime factors:30
Output: 2,3,5
Armstrong number
#include <stdio.h>
int power(int, int);
int main()
{
int n, sum = 0, temp, remainder, digits = 0;
printf("Input an integern");
scanf("%d", &n);
19. Cloud IT Solution Page 365
temp = n;
// Count number of digits
while (temp != 0) {
digits++;
temp = temp/10;
}
temp = n;
while (temp != 0) {
remainder = temp%10;
sum = sum + power(remainder, digits);
temp = temp/10;
}
if (n == sum)
printf("%d is an Armstrong number.n", n);
else
printf("%d isn't an Armstrong number.n", n);
return 0;
}
int power(int n, int r) {
int i, p = 1;
for (i = 1; i <= r; i++)
p = p*n;
return p;
}
Input: Input an integer: 100
Output: 100 isn't an Armstrong number
Fibonacci series using recursion
#include<stdio.h>
int feb(int n);
int main()
{
int n, i = 0;
scanf("%d", &n);
// printf("Fibonacci series terms are:n");
20. Cloud IT Solution Page 366
for(i=0;i<n;i++){
printf("%d ",feb(i));
}
return 0;
}
int feb(int n)
{
if (n == 0 || n == 1)
return n;
else
return (feb(n-1) + feb(n-2));
}
Input: 5
Output: 0 1 1 2 3
Sum natural numbers (1 to N)
#include<stdio.h>
int main()
{
int i,N,sum;
/*read value of N*/
printf("Enter the value of N: ");
scanf("%d",&N);
/*set sum by 0*/
sum=0;
/*calculate sum of the series*/
for(i=1;i<=N;i++)
sum= sum+ i;
/*print the sum*/
printf("Sum of the series is: %dn",sum);
return 0;
}
Input: Enter the value of N: 10
Output: Sum of the series is: 55
Sum of the square of all natural numbers (1 to N)
#include<stdio.h>
int main()
{
int i,N;
21. Cloud IT Solution Page 367
unsigned long sum;
/*read value of N*/
printf("Enter the value of N: ");
scanf("%d",&N);
/*set sum by 0*/
sum=0;
/*calculate sum of the series*/
for(i=1;i<=N;i++)
sum= sum+ (i*i);
/*print the sum*/
printf("Sum of the series is: %ldn",sum);
return 0;
}
Input: Enter the value of N: 10
Output: Sum of the series is: 385
Sum of natural number/factorial of number of all natural numbers (1 to N)
1/1! + 2/2! + 3/3! + 4/4! + ... N/N!
#include<stdio.h>
/*function to find factorial of the number*/
unsigned long factorial(int num)
{
int i;
/*always assign 1, if variable multiplies with
values*/
unsigned long fact=1;
/*multiply num*num-1*num-2*..1*/
for(i=num; i>=1; i--)
fact= fact*i;
/*return factorial*/
return fact;
}
int main()
{
int i,N;
22. Cloud IT Solution Page 368
float sum;
/*read value of N*/
printf("Enter the value of N: ");
scanf("%d",&N);
/*set sum by 0*/
sum=0.0f;
/*calculate sum of the series*/
for(i=1;i<=N;i++)
sum = sum + ( (float)(i) / (float)(factorial(i))
);
/*print the sum*/
printf("Sum of the series is: %fn",sum);
return 0;
}
Input: Enter the value of N: 10
Output: Sum of the series is: 2.718282
Swapping without using third variable
#include <stdio.h>
int main()
{
int a, b;
printf("Input two integers (a & b) to swapn");
scanf("%d%d", &a, &b);
a = a + b;
b = a - b;
a = a - b;
printf("a = %dnb = %dn",a,b);
return 0;
}
Input: Enter the value of N: 10
Output: Sum of the series is: 2.718282
23. Cloud IT Solution Page 369
Maximum and minimum number
#include <stdio.h>
int find_maximum(int a[], int n);
int find_minimum(int a[], int n);
int main() {
int i, array[100], size, location, maximum;
printf("Input number of elements in arrayn");
scanf("%d", &size);
printf("Enter %d integersn", size);
for (i = 0; i < size; i++)
scanf("%d", &array[i]);
location = find_maximum(array, size);
maximum = array[location];
printf("Maximum element location = %d and value = %d.n",
location + 1, maximum);
return 0;
}
int find_maximum(int a[], int n) {
int max, index, i;
max = a[0];
index = 0;
for (i = 1; i < n; i++) {
if (a[i] > max) {
index = i;
max = a[i];
}
}
return index;
}
int find_minimum(int a[], int n) {
int i, min, index;
min = a[0];
index = 0;
for (i = 1; i < n; i++) {
if (a[i] < min) {
index = i;
min = a[i];
}
}
return index;
}
24. Cloud IT Solution Page 370
Input: Input number of elements in array: 5
10 20 30 100 200 1
Output: Maximum element=4 and value =100
Linear search
#include <stdio.h>
long linear_search(long a[], long n, long find);
int main()
{
long array[100], search, c, n, position;
printf("Input number of elements in arrayn");
scanf("%ld", &n);
for (c = 0; c < n; c++)
scanf("%ld", &array[c]);
printf("Input number to search=n");
scanf("%ld", &search);
position = linear_search(array, n, search);
if (position == -1)
printf("%d isn't present in the array.n", search);
else
printf("%d is present at location %d.n", search,
position+1);
return 0;
}
long linear_search(long a[], long n, long find) {
long i;
for (i = 0 ;i < n ; i++ ) {
if (a[i] == find)
return i;
}
return -1;
}
Input:
Input number of elements in array: 5
10 20 30 100 200 1
Input number to search=100
Output: 100 is present at location 4
25. Cloud IT Solution Page 371
Binary search
#include <stdio.h>
int BinarySearching(int arr[], int max, int element);
int main()
{
int count, element, limit, arr[50], position;
printf("Enter the Limit of Elements in Array:t");
scanf("%d", &limit);
for(count = 0; count < limit; count++)
{
scanf("%d", &arr[count]);
}
printf("Enter Element To Search:t");
scanf("%d", &element);
position = BinarySearching(arr, limit, element);
if(position == -1)
{
printf("Element %d Not Foundn", element);
}
else
{
printf("Element %d Found at Position %dn",
element, position + 1);
}
return 0;
}
int BinarySearching(int arr[], int max, int element)
{
int low = 0, high = max - 1, middle;
while(low <= high)
{
middle = (low + high) / 2;
if(element > arr[middle])
low = middle + 1;
else if(element < arr[middle])
high = middle - 1;
else
return middle;
}
return -1;
}
26. Cloud IT Solution Page 372
Input:
Enter the Limit of Elements in Array:5
10 20 30 100 200 300
Enter Element To Search:400
Output: Element 400 Not Found
Reverse array
#include <stdio.h>
int main()
{
int n, i, d, a[100], b[100],j;
printf("Enter the number of elements in arrayn");
scanf("%d", &n);
printf("Enter the array elementsn");
for (i = 0; i< n ; i++)
scanf("%d", &a[i]);
j=0;
for (i = n - 1; i>=0; i--)
b[j++] = a[i];
printf("Reverse array is:n");
for (i = 0; i < n; i++)
printf("%dn", b[i]);
return 0;
}
Input:
Enter the number of elements in array:5
Enter the array elements : 10 20 30 40 50
Output: Reverse array is: 50 40 30 20 10
Insert an element in an array
#include <stdio.h>
int main()
{
int array[100], position, n, value,i;
27. Cloud IT Solution Page 373
printf("Enter number of elements in array:");
scanf("%d", &n);
printf("Enter %d elements:", n);
for (i = 0; i < n; i++)
scanf("%d", &array[i]);
printf("Enter the location where you wish to insert an
elementn");
scanf("%d", &position);
printf("Enter the value to insertn");
scanf("%d", &value);
for(i=n - 1; i>=position-1;i--){
array[i+1] = array[i];
}
array[position-1] = value;
printf("Resultant array is:");
for (i = 0; i <= n; i++)
printf("% d", array[i]);
return 0;
}
Input:
Enter number of elements in array:5
Enter 5 elements: 1 2 3 4 5
Enter the location where you wish to insert an element: 4
Enter the value to insert: 100
Output: Resultant array is:1 2 3 100 4 5
Remove element from array
#include <stdio.h>
int main()
{
int array[100], position, i, n;
printf("Enter number of elements in arrayn");
scanf("%d", &n);
printf("Enter %d elementsn", n);
for ( i = 0 ; i < n ; i++ )
scanf("%d", &array[i]);
28. Cloud IT Solution Page 374
printf("Enter the location where you wish to delete
elementn");
scanf("%d", &position);
if ( position >= n+1 )
printf("Deletion not possible.n");
else
{
for ( i = position - 1 ; i < n - 1 ; i++ )
array[i] = array[i+1];
printf("Resultant array is:");
for( i = 0 ; i < n - 1 ; i++ )
printf("%dn", array[i]);
}
return 0;
}
Input:
Enter number of elements in array:5
Enter 5 elements: 1 2 3 4 5
Enter the location where you wish to delete element: 4
Output: Resultant array is:1 2 3 5
Merge and sorting array
#include <stdio.h>
int main() {
int a[100], b[100], m, n, sorted[200], i, j, k, temp;
printf("Input number of elements in first arrayn");
scanf("%d", &m);
printf("Input %d integersn", m);
for (i = 0; i < m; i++) {
scanf("%d", &a[i]);
}
printf("Input number of elements in second arrayn");
scanf("%d", &n);
printf("Input %d integersn", n);
for (i = 0; i < n; i++) {
scanf("%d", &b[i]);
}
k=0;
29. Cloud IT Solution Page 375
for(i=0;i<m;i++){
sorted[k++]=a[i];
}
for(i=0;i<n;i++){
sorted[k++]=b[i];
}
// Bubble sort
printf("Sorted array:n");
for (i = 0; i < m + n; i++) {
printf("%dn", sorted[i]);
}
for(i=0;i<m+n;i++){
for(j=i+1;j<m+n;j++){
if(sorted[i]>sorted[j]){
temp=sorted[i];
sorted[i]=sorted[j];
sorted[j]=temp;
}
}
}
printf("Sorting array:");
for (i = 0; i < m + n; i++) {
printf("%dn", sorted[i]);
}
return 0;
}
Input:
Input number of elements in first array:5
Input 5 integers: 1 2 3 4 5
Input number of elements in secod array:5
Input 5 integers: 100 20 30 11 12
Output: Sorting array:1 2 3 5 11 12 20 30 100
Example String
#include <stdio.h>
int main()
{
char array[100];
printf("Enter a stringn");
scanf("%s", array);
printf("Your entered string: %sn", array);
return 0;
}
30. Cloud IT Solution Page 376
Input: Enter a string : My name is mehedi
Output: Your entered string:My name is mehedi
Example String
#include <stdio.h>
int main()
{
char a[80];
gets(a);
printf("%sn", a);
return 0;
}
Input: My name is mehedi
Output: My name is mehedi
Example String
#include <stdio.h>
int main()
{
char s[100];
int i = 0;
gets(s);
while (s[i] != '0') {
printf("%c", s[i]);
i++;
}
return 0;
}
Input: My name is mehedi
Output: My name is mehedi
Reverse a string
#include <stdio.h>
#include <string.h>
int main()
{
char s[100];
int n, i;
gets(s);
31. Cloud IT Solution Page 377
n =strlen(s);
for(i = n -1; i>=0; i-- ){
putchar(s[i]);
}
return 0;
}
Input: mehedi
Output: idehem
Word count
#include<stdio.h>
#include<conio.h>
int main(){
int i,j,k,n;
char value[1000],reversevalue[1000],count;
gets(value);
count=1;
printf("n");
n=strlen(value);
for(j=0;j<=n-1;j++){
if (value[j] == ' ' || value[j] == 'n'){
count++;
}
}
printf("No of word : %d",count);
return 0;
}
Input: I love Bangladesh
Output: No of word: 3
File and character
#include <stdio.h>
#include <stdio.h>
int main()
{
FILE *fp;
char filename[100];
char ch;
int linecount, wordcount, charcount;
// Initialize counter variables
linecount = 0;
32. Cloud IT Solution Page 378
wordcount = 0;
charcount = 0;
// Prompt user to enter filename
printf("Enter a filename :");
gets(filename);
// Open file in read-only mode
fp = fopen(filename,"r");
// If file opened successfully, then write the string to
file
if ( fp )
{
//Repeat until End Of File character is reached.
while ((ch=getc(fp)) != EOF) {
// Increment character count if NOT new line or
space
if (ch != ' ' && ch != 'n') { ++charcount; }
// Increment word count if new line or space
character
if (ch == ' ' || ch == 'n') { ++wordcount; }
// Increment line count if new line character
if (ch == 'n') { ++linecount; }
}
if (charcount > 0) {
++linecount;
++wordcount;
}
}
else
{
printf("Failed to open the filen");
}
printf("Lines : %d n", linecount);
printf("Words : %d n", wordcount);
printf("Characters : %d n", charcount);
getchar();
return(0);
}
33. Cloud IT Solution Page 379
Input: Enter a filename: a.txt
Output: The contents of %s file are:
Lines:2
Words:20
Characters:100
Read a file
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch, file_name[25];
FILE *fp;
printf("Enter name of a file you wish to seen");
gets(file_name);
fp = fopen(file_name, "r"); // read mode
if (fp == NULL)
{
perror("Error while opening the file.n");
exit(EXIT_FAILURE);
}
printf("The contents of %s file are:n", file_name);
while((ch = fgetc(fp)) != EOF)
printf("%c", ch);
fclose(fp);
return 0;
}
Input: Enter name of a file you wish to see:a.txt
Output: The contents of %s file are:
How are you!
Marge two file
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fs1, *fs2, *ft;
char ch, file1[20], file2[20], file3[20];
printf("Enter name of first filen");
gets(file1);
34. Cloud IT Solution Page 380
printf("Enter name of second filen");
gets(file2);
printf("Enter name of file which will store contents of
the two filesn");
gets(file3);
fs1 = fopen(file1, "r");
fs2 = fopen(file2, "r");
if(fs1 == NULL || fs2 == NULL)
{
perror("Error ");
printf("Press any key to exit...n");
exit(EXIT_FAILURE);
}
ft = fopen(file3, "w"); // Opening in write mode
if(ft == NULL)
{
perror("Error ");
printf("Press any key to exit...n");
exit(EXIT_FAILURE);
}
while((ch = fgetc(fs1)) != EOF)
fputc(ch,ft);
while((ch = fgetc(fs2)) != EOF)
fputc(ch,ft);
printf("The two files were merged into %s file
successfully.n", file3);
fclose(fs1);
fclose(fs2);
fclose(ft);
return 0;
}
Input:
Enter name of first file:a.txt
Enter name of second file:b.txt
Enter name of file which will store contents of the two files: output.txt
Output: The two files were merged into output.txt file successfully
35. Cloud IT Solution Page 381
Delete file
#include<stdio.h>
main()
{
int status;
char file_name[25];
printf("Enter name of a file you wish to deleten");
gets(file_name);
status = remove(file_name);
if (status == 0)
printf("%s file deleted successfully.n", file_name);
else
{
printf("Unable to delete the filen");
perror("Error");
}
return 0;
}
Input: Enter name of a file you wish to delete :C:test
Output: Unable to delete the file
Check valid IP address
#include <stdio.h>
#include <stdlib.h>
int main ()
{
char ipAddr[100];
printf("Enter the ip address=");
scanf("%s",&ipAddr);
int a=0,b=0,c=0,d=0;
int con = sscanf(ipAddr, "%d.%d.%d.%d", &a, &b, &c, &d);
if ( 4 == con )
{
printf("scanned ip=%s had %d octetsn a=%d, b=%d, c=%d,
d=%dn", ipAddr, con, a, b, c, d);
if ((0<=a && a<=255) && (0<=b && b<=255) && (0<=c &&
c<=255) && (0<=d && d<=255) )
printf("valid ip addressn");
else
printf("not valid ip addressn");
}
else
{
36. Cloud IT Solution Page 382
printf("malformed ip addressn");
}
return 0;
}
Input: Enter the ip address=192.168.1.1
Output: valid ip address
37. Cloud IT Solution Page 383
1. Determine output:
Void main ()
{
float me = 1.1;
double you = 1.1;
if (me = you )
printf (“ I Can it”);
else
printf (“I can not it”);
}
a. I can it b. I can not it
c. Error d. None of these
2. Determine output:
void main ( )
{
static intvar = 5;
printf ( “%d” , var--);
if (var)
main();
}
a. 5 5 5 5 5 b. 5 4 3 2 1
c. Infinite Loop d. None of the
3. Who is father of C language?
a. BjarneStroustrup b. James A. Gosling d. Dennis Ritchie d. Dr. E.F. Codd
4. C language developed at_______?
a. AT & T’s Bell Laboratories of USA in 1972 b. AT & T’s Bell Laboratories of USA in 1970
c. Sun Microsystems in 1973 d. Cambridge University in 1972
5. For 16-bit compiler allowable range for integer constants is _______?
a. -3.4e38 to 3.4e38 b. -32767 to 3276 c. -32668 to 32667 d. -32768 to 32767
6. C programs are converted into machine language with the help of
a. An Editor b.A compiler c. An operating System d. None of these
7. C was primarily developed as
a.System programming Language b. General purpose language
c.Data processing language d. None of the above
8. Standard ANSI C recognizes ___________ number of keywords.
a. 30 b. 32 c. 24 d. 36
9. Which one of the following is not a reserved keyword for C?
a. auto b. case c. main d. default
Model Test
38. Cloud IT Solution Page 384
10. A C variable cannot start with
a. a number b. A special other than underscore
b. Both of the above d. An alphabet
11. Which one of the following is not a valid identifier?
a. _examveda b. 1examveda c. exam_veda d. examveda1
12. What is the correct value to return to the operating system upon the successful
completion of a program?
a. 1 b. -1 c. 0 d. program do no return a value
13. Which is the only function all C programs must contain?
a. start( ) b. system () c. main ( ) d. printf( )
14. Which of the following is not a correct variable type?
a. float b. real c. int d. double
15. Which of following is not a valid name for a C variable?
a. Examveda b. Exam_veda c. Exam veda d.Both A and B
16. What is right way to initialize array?
a. intnum[6] = { 2,4,12,5,45,5} b. int n{}={2,4,12,5,45,5}
c. int n{6}={2,4,12} d. int n(6) ={2,4,12,5,45,5}
17. What will be the output of the program?
#include<stdio.h>
void main()
{ int a [5] = {5, 1, 15, 20, 25};
int i, j, m;
i = ++a [1];
j = a[1]++;
m=a[i++];
printf (“ %d, %d, %d”, i, j, m);
}
a. 3, 2, 15 b. 2, 3, 20
c. 2, 1, 15 d. 1, 2, 5
18. An array elements are always stored in __________ memory locations.
a. Sequential b. Random c. Sequential and Random d.None of the above
19. Let x be an array. Which of the following operations are illegal?
(i) ++x (ii) x+1 (iii) x++ (iv) x*2
a. i, ii b. i, ii, iii c. ii, iii d. i, iii, iv
20. What is the maximum number of dimensions an array in C may have?
a. 2 b. 8 c. 20
d. Theoretically no limit. The only practical limits are memory size and compilers.
21. Size of the array need not be specified, when
a. Initialization is a part of definition b. It is a declaration
c. It is a formal parameter d. All of these
22. Array passed as an argument to a function is interpreted as
a. Address of the array b. Values of the first elements of the array
c. Address of the first element of the array d. Number of element of the array
39. Cloud IT Solution Page 385
23. If the two strings are identical, then strcmp ( ) function returns
a. 1 b. 0 c. -1 d. true
24. Which of following function is more appropriate for reading in a multi-word
string?
a. scanf ( ) b. gets ( ) c. printf ( ) d. puts ( )
25. int a [5] = {1, 2, 3}
What is the value of a[ 4] ?
a. 3 b. 1 c. 2 d. 0
26. Which of the following operator takes only integer operands?
a. + b. * c. / d. %
27. In C programming language, which of the following type of operators have the
highest precedence
a. Relational b. Equality operators
c. Logical operators d. Arithmetic operators
28. Which operator has the lowest priority?
a. ++ b. % c. + d. ||
29. Which operator from the following has the lowest priority?
a. Assignment operator b. Division operator
c. Comma operator d. Conditional operator
30. Which command is used to skip the rest of a loop and carry on from the top of the
loop again?
a. break b. resume c. continue d. skip
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
b b c a d b a b c c b c c b c
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
a a a d d d c b b d d d d c c
Model Test Answer