KEMBAR78
C Programming Example | PDF
Prathamesh Deshpande
G-(04)
Q.1 Write a program to print the following pattern
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
C programming code:
#include<stdio.h>
int main()
{
int i,j;
char input,temp='1';
printf("Enter number you want in triangle at last row: ");
scanf("%c",&input);
for(i=1;i<=(input-'1'+1);++i)
{
for(j=1;j<=i;++j)
printf("%c",temp);
++temp;
printf("n");
}
return 0;
}
Output:
Q.2 Write a program to find bigger of three integers.
C programming code:
#include <stdio.h>
int main()
{
int array[100], maximum, size, c, location = 1;
printf("Enter the number of elements in arrayn");
scanf("%d", &size);
printf("Enter %d integersn", size);
for (c = 0; c < size; c++)
scanf("%d", &array[c]);
maximum = array[0];
for (c = 1; c < size; c++)
{
if (array[c] > maximum)
{
maximum = array[c];
location = c+1;
}
}
printf("Maximum element is present at location %d and it's value is
%d.n", location, maximum);
return 0;
}
Output:
Q.3 Write a program to calculate GCD between two numbers.
C programming code:
#include <stdio.h>
int gcd(int n1, int n2);
int main()
{
int n1, n2;
printf("Enter two positive integers: ");
scanf("%d%d", &n1, &n2);
printf("GCD of %d and %d = %d", n1, n2, gcd(n1,n2));
return 0;
}
int gcd(int n1, int n2)
{
if (n2!=0)
return gcd(n2, n1%n2);
else
return n1;
}
Output:
Q.4 Write a program to find transpose of matrix.
C programming code:
#include <stdio.h>
int main()
{
int m, n, c, d, matrix[10][10], transpose[10][10];
printf("Enter the number of rows and columns of matrixn");
scanf("%d%d", &m, &n);
printf("Enter the elements of matrixn");
for (c = 0; c < m; c++)
for(d = 0; d < n; d++)
scanf("%d",&matrix[c][d]);
for (c = 0; c < m; c++)
for( d = 0 ; d < n ; d++ )
transpose[d][c] = matrix[c][d];
printf("Transpose of entered matrix :-n");
for (c = 0; c < n; c++) {
for (d = 0; d < m; d++)
printf("%dt",transpose[c][d]);
printf("n");
}
return 0;
}
Output:
Q.5 Write a program which deletes an element from an array & display all other elements.
C programming code:
#include <stdio.h>
int main()
{
int array[100], position, c, n;
printf("Enter number of elements in arrayn");
scanf("%d", &n);
printf("Enter %d elementsn", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d", &array[c]);
printf("Enter the location where you wish to delete elementn");
scanf("%d", &position);
if ( position >= n+1 )
printf("Deletion not possible.n");
else
{ for ( c = position - 1 ; c < n - 1 ; c++ )
array[c] = array[c+1];
printf("Resultant array isn");
for( c = 0 ; c < n - 1 ; c++ )
printf("%dn", array[c]);
}
return 0;
}
Output:
Q.6 Write a program to calculate XA+YB where A & B are matrix & X=2, Y=3.
C programming code:
#include <stdio.h>
int main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];
printf("Enter the number of rows and columns of matrixn");
scanf("%d%d", &m, &n);
printf("Enter the elements of first matrixn");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);
printf("Enter the elements of second matrixn");
for (c = 0; c < m; c++)
for (d = 0 ; d < n; d++)
scanf("%d", &second[c][d]);
printf("Sum of entered matrices:-n");
for (c = 0; c < m; c++)
{ for (d = 0 ; d < n; d++)
{ sum[c][d] = (2*(first[c][d]))+(3*(second[c][d]));
printf("%dt", sum[c][d]);
}
printf("n");
} return 0;
}
Output:
Q.7 Write a program to calculate the total amount of money in the piggybank, given that coins of
Rs.10,
Rs.5, Rs.2, RS.1.
C programming code:
#include<stdio.h>
int main()
{
int a, b, c, d, e;
printf("Enter no. coins of Rs10 Rs5 Rs2 Rs1 n");
scanf("%d%d%d%d",&a,&b,&c,&d);
e = (10*a)+(5*b)+(2*c)+d;
printf("Sum of money in piggybank = %dn",e);
return 0;
}
Output:
Q.8 Write a program to calculate sum of squares of first n even no.
C programming code:
#include<stdio.h>
int main()
{
int i, j, sum = 0, n;
printf("Enter no of terms");
scanf("%d",&n);
printf("nEven numbers and their square:n");
for(i =1; i <=n; i++)
{ if(i % 2 == 0)
{ j = i * i;
printf("%dtt%d",i,j);
printf("n");
sum = sum + j;
}
}
printf("nnSum of even numbers square is: %d",sum);
return 0;
}
Output:
Q.9 Write a program to calculate exp(X,Y) using recursive functions.
C programming code:
#include <stdio.h>
long power (int, int);
int main()
{
int Y, X;
long result;
printf("Enter a number X : ");
scanf("%d", &X);
printf("Enter it's power Y : ");
scanf("%d", &Y);
result = power(X, Y);
printf("%d^%d is %ld", X, Y, result);
return 0;
}
long power (int X, int Y)
{
if (Y)
{
return (X * power(X, Y - 1));
}
return 1;
}
Output:
Q.10 Write a program to enter a number from 1 to 7 & display it’s corresponding day of week using
Switch - case statement.
C programming code:
#include <stdio.h>
int main()
{
int week;
printf("Enter day of week to get it's corresponding day (1-7): ");
scanf("%d", &week);
switch(week)
{
case 1: printf("MONDAY");
break;
case 2: printf("TUESDAY");
break;
case 3: printf("WEDNESDAY");
break;
case 4: printf("THURSDAY");
break;
case 5: printf("FRIDAY");
break;
case 6: printf("SATURDAY");
break;
case 7: printf("SUNDAY");
break;
default: printf("Invalid input! Please enter week number
between 1-7");
}
return 0;
}
Output:

C Programming Example

  • 1.
    Prathamesh Deshpande G-(04) Q.1 Writea program to print the following pattern 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 C programming code: #include<stdio.h> int main() { int i,j; char input,temp='1'; printf("Enter number you want in triangle at last row: "); scanf("%c",&input); for(i=1;i<=(input-'1'+1);++i) { for(j=1;j<=i;++j) printf("%c",temp); ++temp; printf("n"); } return 0; } Output: Q.2 Write a program to find bigger of three integers. C programming code: #include <stdio.h> int main() { int array[100], maximum, size, c, location = 1; printf("Enter the number of elements in arrayn"); scanf("%d", &size); printf("Enter %d integersn", size); for (c = 0; c < size; c++) scanf("%d", &array[c]); maximum = array[0]; for (c = 1; c < size; c++) { if (array[c] > maximum) { maximum = array[c]; location = c+1; } }
  • 2.
    printf("Maximum element ispresent at location %d and it's value is %d.n", location, maximum); return 0; } Output: Q.3 Write a program to calculate GCD between two numbers. C programming code: #include <stdio.h> int gcd(int n1, int n2); int main() { int n1, n2; printf("Enter two positive integers: "); scanf("%d%d", &n1, &n2); printf("GCD of %d and %d = %d", n1, n2, gcd(n1,n2)); return 0; } int gcd(int n1, int n2) { if (n2!=0) return gcd(n2, n1%n2); else return n1; } Output: Q.4 Write a program to find transpose of matrix. C programming code: #include <stdio.h> int main() { int m, n, c, d, matrix[10][10], transpose[10][10]; printf("Enter the number of rows and columns of matrixn"); scanf("%d%d", &m, &n); printf("Enter the elements of matrixn"); for (c = 0; c < m; c++) for(d = 0; d < n; d++) scanf("%d",&matrix[c][d]); for (c = 0; c < m; c++) for( d = 0 ; d < n ; d++ ) transpose[d][c] = matrix[c][d]; printf("Transpose of entered matrix :-n"); for (c = 0; c < n; c++) { for (d = 0; d < m; d++)
  • 3.
    printf("%dt",transpose[c][d]); printf("n"); } return 0; } Output: Q.5 Writea program which deletes an element from an array & display all other elements. C programming code: #include <stdio.h> int main() { int array[100], position, c, n; printf("Enter number of elements in arrayn"); scanf("%d", &n); printf("Enter %d elementsn", n); for ( c = 0 ; c < n ; c++ ) scanf("%d", &array[c]); printf("Enter the location where you wish to delete elementn"); scanf("%d", &position); if ( position >= n+1 ) printf("Deletion not possible.n"); else { for ( c = position - 1 ; c < n - 1 ; c++ ) array[c] = array[c+1]; printf("Resultant array isn"); for( c = 0 ; c < n - 1 ; c++ ) printf("%dn", array[c]); } return 0; } Output:
  • 4.
    Q.6 Write aprogram to calculate XA+YB where A & B are matrix & X=2, Y=3. C programming code: #include <stdio.h> int main() { int m, n, c, d, first[10][10], second[10][10], sum[10][10]; printf("Enter the number of rows and columns of matrixn"); scanf("%d%d", &m, &n); printf("Enter the elements of first matrixn"); for (c = 0; c < m; c++) for (d = 0; d < n; d++) scanf("%d", &first[c][d]); printf("Enter the elements of second matrixn"); for (c = 0; c < m; c++) for (d = 0 ; d < n; d++) scanf("%d", &second[c][d]); printf("Sum of entered matrices:-n"); for (c = 0; c < m; c++) { for (d = 0 ; d < n; d++) { sum[c][d] = (2*(first[c][d]))+(3*(second[c][d])); printf("%dt", sum[c][d]); } printf("n"); } return 0; } Output: Q.7 Write a program to calculate the total amount of money in the piggybank, given that coins of Rs.10, Rs.5, Rs.2, RS.1. C programming code: #include<stdio.h> int main() { int a, b, c, d, e; printf("Enter no. coins of Rs10 Rs5 Rs2 Rs1 n"); scanf("%d%d%d%d",&a,&b,&c,&d); e = (10*a)+(5*b)+(2*c)+d; printf("Sum of money in piggybank = %dn",e); return 0; }
  • 5.
    Output: Q.8 Write aprogram to calculate sum of squares of first n even no. C programming code: #include<stdio.h> int main() { int i, j, sum = 0, n; printf("Enter no of terms"); scanf("%d",&n); printf("nEven numbers and their square:n"); for(i =1; i <=n; i++) { if(i % 2 == 0) { j = i * i; printf("%dtt%d",i,j); printf("n"); sum = sum + j; } } printf("nnSum of even numbers square is: %d",sum); return 0; } Output: Q.9 Write a program to calculate exp(X,Y) using recursive functions. C programming code: #include <stdio.h> long power (int, int); int main() { int Y, X; long result; printf("Enter a number X : "); scanf("%d", &X); printf("Enter it's power Y : "); scanf("%d", &Y);
  • 6.
    result = power(X,Y); printf("%d^%d is %ld", X, Y, result); return 0; } long power (int X, int Y) { if (Y) { return (X * power(X, Y - 1)); } return 1; } Output: Q.10 Write a program to enter a number from 1 to 7 & display it’s corresponding day of week using Switch - case statement. C programming code: #include <stdio.h> int main() { int week; printf("Enter day of week to get it's corresponding day (1-7): "); scanf("%d", &week); switch(week) { case 1: printf("MONDAY"); break; case 2: printf("TUESDAY"); break; case 3: printf("WEDNESDAY"); break; case 4: printf("THURSDAY"); break; case 5: printf("FRIDAY"); break; case 6: printf("SATURDAY"); break; case 7: printf("SUNDAY"); break; default: printf("Invalid input! Please enter week number between 1-7"); } return 0; } Output: