Frequently asked programmes/ assignment question in C programming
Write a c programme to display,"this is my first c programme.
#include <stdio.h>
int main(){
printf("This is my first c programme");
return 0;
}
Write a C program to perform addition subtraction division and multiplication of two numbers..
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main()
{
int num1,num2;
printf("Enter the first number: ");
scanf("%d",&num1);
printf("Enter the second number: ");
scanf("%d",&num2);
printf("\nAddition of %d + %d=%d",num1,num2,num1+num2);
printf("\nSubstraction of %d - %d=%d",num1,num2,num1-num2);
printf("\nMultiplication of %d * %d=%d",num1,num2,num1*num2);
printf("\nDivision of %d / %d=%d",num1,num2,num1/num2);
getch();
return 0;
}
Write a C program to calculate simple interest. .
#include<stdio.h>
int main()
{
float P , R , T , SI ;
P =34567;
R =30;
T = 5;
SI = (P*R*T)/100;
printf("\n\n Simple Interest is : %f", SI);
return (0);
}
Write a C program to calculate compound interest. ..
#include <stdio.h>
#include <math.h>
int main()
{
float principle, rate, time, CI;
/* Input principle, time and rate */
printf("Enter principle amount: \n");
scanf("%f", &principle);
printf("Enter time: \n");
scanf("%f", &time);
printf("Enter rate: \n");
scanf("%f", &rate);
/* Calculate compound interest */
CI = principle* (pow((1 + rate / 100), time));
/* Print the resultant CI */
printf("Compound Interest = %f", CI);
return 0;
}
Write a C program to swap two numbers using third variable. ..
#include <stdio.h>
int main()
{
int n1, n2, temp;
printf("Enter two integers:\n");
scanf("%d%d", &n1, &n2);
printf("Before Swapping First variable = %d and Second variable = %d\n", n1, n2);
temp = n1;
n1 = n2;
n2 = temp;
printf("After Swapping First variable = %d Second variable = %d", n1, n2);
return 0;
}
Write a C program to Swap two numbers without using third variable....
#include<stdio.h>
int main()
{
int a, b;
Printf("enter the value of a and b:\n") ;
Scanf("%d %d", &a, &b) ;
printf("Before swap a=%d b=%d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("\nAfter swap a=%d b=%d",a,b);
return 0;
}
Write a C program to illustrate the use of unary prefix and postfix increment and decrement operator. ...
Pre-increment
#include <stdio.h>
#include <conio.h>
int main ()
{
// declare integer variables
int x, y, z;
printf (" Input the value of X: ");
scanf (" %d", &x);
printf (" Input the value of Y: ");
scanf (" %d", &y);
printf (" Input the value of Z: ");
scanf (" %d", &z);
// use pre increment operator to update the value by 1
++x;
++y;
++z;
printf (" \n The updated value of the X: %d ", x);
printf (" \n The updated value of the Y: %d ", y);
printf (" \n The updated value of the Z: %d ", z);
return 0;
}
Post increment
#include <stdio.h>
#include <conio.h>
int main ()
{
// declare integer variables
int x, y, z, a, b, c;
printf (" Input the value of X: ");
scanf (" %d", &x);
printf (" Input the value of Y: ");
scanf (" %d", &y);
printf (" Input the value of Z: ");
scanf (" %d", &z);
// use post-increment operator to update the value by 1
a = x++;
b = y++;
c = z++;
printf (" \n The original value of a: %d", a);
printf (" \n The original value of b: %d", b);
printf (" \n The original value of c: %d", c);
printf (" \n\n The updated value of the X: %d ", x);
printf (" \n The updated value of the Y: %d ", y);
printf (" \n The updated value of the Z: %d ", z);
return 0;
}
Pre decrement
#include <stdio.h>
#include <conio.h>
int main ()
{
// declare integer variables
int x, y, z;
printf (" Input the value of X: ");
scanf (" %d", &x);
printf (" \n Input the value of Y: ");
scanf (" %d", &y);
printf ("n Input the value of Z: ");
scanf (" %d", &z);
// use pre decrement operator to update the value by 1
--x;
--y;
--z;
printf (" \n The updated value of the X: %d ", x);
printf (" \n The updated value of the Y: %d ", y);
printf (" \n The updated value of the Z: %d ", z);
return 0;
}
Post decrement
#include <stdio.h>
#include <conio.h>
int main ()
{
// declare integer variables
int x, y, z, a, b, c;
printf (" Input the value of X: ");
scanf (" %d", &x);
printf (" Input the value of Y: ");
scanf (" %d", &y);
printf (" Input the value of Z: ");
scanf (" %d", &z);
// use post-decrement operator to update the value by 1
a = x--;
b = y--;
c = z--;
printf (" \n The original value of a: %d", a);
printf (" \n The original value of b: %d", b);
printf (" \n The original value of c: %d", c);
printf (" \n\n The updated value of the X: %d ", x);
printf (" \n The updated value of the Y: %d ", y);
printf (" \n The updated value of the Z: %d ", z);
return 0;
}
Write a C program to find the root of Quadratic equation .....
# include<stdio.h>
# include<conio.h>
# include<math.h>
main (){
float a,b,c,r1,r2,d;
printf (“enter the values of a b c”);
scanf (“ %f %f %f”, &a, &b, &c);
d= b*b – 4*a*c;
if (d>0){
r1 = -b+sqrt (d) / (2*a);
r2 = -b-sqrt (d) / (2*a);
printf (“The real roots = %f %f”, r1, r2);
}
else if (d= =0){
r1 = -b/(2*a);
r2 = -b/(2*a);
printf (“roots are equal =%f %f”, r1, r2);
}
else
printf(“Roots are imaginary”);
getch ();
}
Write a C program to find the largest of three numbers using ternary operator. ..
#include<stdio.h>
int main(){
int a,b,c;
int big;
printf("Enter any there numbers: ");
scanf("%d%d%d",&a,&b,&c);
if(a>b && a>c)
big = a;
else if(b>c)
big = b;
else
big = c;
printf("Largest number is: %d",big);
return 0;
}
Write a C program to check whether a number is prime or not. . . .
#include <stdio.h>
int main() {
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
// 0 and 1 are not prime numbers
// change flag to 1 for non-prime number
if (n == 0 || n == 1)
flag = 1;
for (i = 2; i <= n / 2; ++i) {
// if n is divisible by i, then n is not prime
// change flag to 1 for non-prime number
if (n % i == 0) {
flag = 1;
break;
}
}
// flag is 0 for prime numbers
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
return 0;
}
Write a C program to check whether the entered year is leap year not. ...
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
// leap year if perfectly divisible by 400
if (year % 400 == 0) {
printf("%d is a leap year.", year);
}
// not a leap year if divisible by 100
// but not divisible by 400
else if (year % 100 == 0) {
printf("%d is not a leap year.", year);
}
// leap year if not divisible by 100
// but divisible by 4
else if (year % 4 == 0) {
printf("%d is a leap year.", year);
}
// all other years are not leap years
else {
printf("%d is not a leap year.", year);
}
return 0;
}
Write a C program to find factorial of a number using loop
#include <stdio.h>
int main() {
int n, i;
unsigned long long inter fact = 1;
printf("Enter an integer: ");
scanf("%d", &n);
// shows error if the user enters a negative integer
if (n < 0)
printf("Error! Factorial of a negative number doesn't exist.");
else if(n==0) {
printf(" Factorial of 0 is 1.");
else {
for (i = 1; i <= n; ++i) {
fact *= i;
}
printf("Factorial of %d = %llu", n, fact);
}
return 0;
}
Write a C program to find factor of a number using recursion. .
#include<stdio.h>
int fact(int);
int main()
{
int x,n;
printf(" Enter the Number to Find Factorial :");
scanf("%d",&n);
x=fact(n);
printf(" Factorial of %d is %d",n,x);
return 0;
}
int fact(int n)
{
if(n==0)
return(1);
return(n*fact(n-1));
}
Write a C program to check whether a given number is armstrong or not. ..
#include <math.h>
#include <stdio.h>
int main() {
int num, originalNum, remainder, n = 0;
float result = 0.0;
printf("Enter an integer: ");
scanf("%d", &num);
originalNum = num;
// store the number of digits of num in n
for (originalNum = num; originalNum != 0; ++n) {
originalNum /= 10;
}
for (originalNum = num; originalNum != 0; originalNum /= 10) {
remainder = originalNum % 10;
// store the sum of the power of individual digits in result
result += pow(remainder, n);
}
// if num is equal to result, the number is an Armstrong number
if ((int)result == num)
printf("%d is an Armstrong number.", num);
else
printf("%d is not an Armstrong number.", num);
return 0;
}
Write a program to count the number of digit in a given integer. ...
#include <stdio.h>
int main()
{
int n; // variable declaration
int count=0; // variable declaration
printf("Enter a number");
scanf("%d",&n);
while(n!=0)
{
n=n/10;
count++;
}
printf("\nThe number of digits in an integer is : %d",count);
return 0;
}
Write a C program to reverse a string. ...
#include <stdio.h>
#include <string.h>
int main()
{
char str[40];
printf (" \n Enter a string to be reversed: ");
scanf ("%s", str);
printf (" \n After the reverse of a string: %s ", strrev(str));
return 0; }
Write a C program to print the sum of digit of a number using loop..
#include <stdio.h>
int main()
{
int Num, Reminder, Sum;
printf("\nPlease Enter any value\n");
scanf("%d", &Num);
for (Sum=0; Num > 0;Num=Num/10)
{
Reminder = Num % 10;
Sum=Sum+ Reminder;
}
printf("\n Sum of the digits = %d", Sum);
return 0;
}
Write a C program to check whether number is palindrome or not. . ...
#include <stdio.h>
int main() {
int n, reversed = 0, remainder, original;
printf("Enter an integer: ");
scanf("%d", &n);
original = n;
// reversed integer is stored in reversed variable
while (n != 0) {
remainder = n % 10;
reversed = reversed * 10 + remainder;
n /= 10;
}
if (original == reversed)
printf("%d is a palindrome.", original);
else
printf("%d is not a palindrome.", original);
return 0;
}
Write a C program to generate Fibonacci series....
#include <stdio.h>
int main() {
int i, n;
// initialize first and second terms
int t1 = 0, t2 = 1;
// initialize the next term (3rd term)
int nextTerm = t1 + t2;
// get no. of terms from user
printf("Enter the number of terms: ");
scanf("%d", &n);
// print the first two terms t1 and t2
printf("Fibonacci Series: %d, %d, ", t1, t2);
// print 3rd to nth terms
for (i = 3; i <= n; ++i) {
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
return 0;
}
Write a C program to find the gcd of two numbers. . .
include <stdio.h>
int main()
{
int n1, n2, i, gcd;
printf("Enter two integers: ");
scanf("%d %d", &n1, &n2);
for(i=1; i <= n1 && i <= n2; ++i)
{
// Checks if i is factor of both integers
if(n1%i==0 && n2%i==0)
gcd = i;
}
printf("G.C.D of %d and %d is %d", n1, n2, gcd);
return 0;
}
Write a C program to find LCM of two numbers. . . .
#include <stdio.h>
#include <conio.h>
void main()
{
int num1, num2, max_div, flag = 1;
printf( " Enter any two positive numbers to get the LCM \n ");
scanf(" %d %d", &num1, &num2);
number between num1 and num2.
max_div = (num1 > num2) ? num1 : num2;
while (flag) // (flag = 1)
{
if (max_div % num1 == 0 && max_div % num2 == 0)
{
printf( " The LCM of %d and %d is %d. ", num1, num2, max_div);
break;
}
++max_div;
}
}
Write a C program to find largest and smallest element in an array.. ..
#include<stdio.h>
int main()
{
int a[50],i,n,large,small;
printf("How many elements:");
scanf("%d",&n);
printf("Enter the Array:");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
large=small=a[0];
for(i=1;i<n;++i)
{
if(a[i]>large)
large=a[i];
if(a[i]<small)
small=a[i];
}
printf("The largest element is %d",large);
printf("\nThe smallest element is %d",small);
return 0;
}
Write a C programme for deletion of an array element from specified position....
#include<stdio.h>
void main()
{
int a[100],i,n,pos;
printf("\nEnter no of elements\n");
scanf("%d",&n);
printf("Enter the elements\n");
for (i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Elements of array are\n");
for(i=0;i<n;i++)
{
printf("a[%d] = %d\n",i,a[i]);
}
printf("Enter the position from which the number has to be deleted\n");
scanf("%d",&pos);
for(i=pos;i<n-1;i++)
{
a[i]=a[i+1];
}
n=n-1;
printf("\nOn Deletion, new array we get is\n");
for(i=0;i<n;i++)
{
printf("a[%d] = %d\n",i,a[i]);
}
}
Write a C program to reverse an array element. . .
#include <stdio.h>
#include <stdlib.h>
#define n 6
int main(){
int arr[n] = {9, 8, 7, 2, 4, 3};
int temp;
for(int i = 0; i<n/2; i++){
temp = arr[i];
arr[i] = arr[n-i-1];
arr[n-i-1] = temp;
}
for(int i = 0; i < n; i++){
printf("%d,", arr[i]);
}
}
Write a C program to access and element from 2D.....
#include<stdio.h>
int main(){
/* 2D array declaration*/
int disp[2][3];
/*Counter variables for the loop*/
int i, j;
for(i=0; i<2; i++) {
for(j=0;j<3;j++) {
printf("Enter value for disp[%d][%d]:", i, j);
scanf("%d", &disp[i][j]);
}
}
//Displaying array elements
printf("Two Dimensional array elements:\n");
for(i=0; i<2; i++) {
for(j=0;j<3;j++) {
printf("%d ", disp[i][j]);
if(j==2){
printf("\n");
}
}
}
return 0;
}
Write a C program for addition of two matrices of any order in C
#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 matrix\n");
scanf("%d%d", & m, & n);
printf("Enter the elements of first matrix\n");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", & first[c][d]);
printf("Enter the elements of second matrix\n");
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] = first[c][d] + second[c][d];
printf("%d\t", sum[c][d]);
}
printf("\n");
}
return 0;
}
Write a program to access an element of 2D array.. .
#include<stdio.h>
int main(){
/* 2D array declaration*/
int disp[2][3];
/*Counter variables for the loop*/
int i, j;
for(i=0; i<2; i++) {
for(j=0;j<3;j++) {
printf("Enter value for disp[%d][%d]:", i, j);
scanf("%d", &disp[i][j]);
}
}
//Displaying array elements
printf("Two Dimensional array elements:\n");
for(i=0; i<2; i++) {
for(j=0;j<3;j++) {
printf("%d ", disp[i][j]);
if(j==2){
printf("\n");
}
}
}
return 0;
}
Write a C program to add subtract multiply and divide two integers using user defined type function with return type.. .
#include<stdio.h> // functions declaration int add(int n1, int n2); int subtract(int n1, int n2); int multiply(int n1, int n2); int divide(int n1, int n2); // main function int main() { int num1, num2; printf("Enter two numbers: "); scanf("%d %d", &num1, &num2); printf("%d + %d = %d\n", num1, num2, add(num1, num2)); printf("%d - %d = %d\n", num1, num2, subtract(num1, num2)); printf("%d * %d = %d\n", num1, num2, multiply(num1, num2)); printf("%d / %d = %d\n", num1, num2, divide(num1, num2)); return 0; } // function to add two integer numbers int add(int n1, int n2) { int result; result = n1 + n2; return result; } // function to subtract two integer numbers int subtract(int n1, int n2) { int result; result = n1 - n2; return result; } // function to multiply two integer numbers int multiply(int n1, int n2) { int result; result = n1 * n2; return result; } // function to divide two integer numbers int divide(int n1, int n2) { int result; result = n1 / n2; return result; }
Write a C program to copy one string to another using pointer,.. .
#include<stdio.h>
#include<conio.h>
void main()
{
char *str1, *str2;
int i;
clrscr();
printf("Enter the string : ");
scanf("%s", str2);
for(i = 0; *str2 != '\0'; i++, str1++, str2++)
*str1 = *str2;
*str1 = '\0';
str1 = str1 - i;
printf("\nThe copied string is : %s", str1);
getch();
}
Write a C program to find length of string using pointer...
#include<stdio.h>
#include<conio.h>
int string_ln(char*);
void main() {
char str[20];
int length;
clrscr();
printf("\nEnter any string : ");
gets(str);
length = string_ln(str);
printf("The length of the given string %s is : %d", str, length);
getch();
}
int string_ln(char*p) /* p=&str[0] */
{
int count = 0;
while (*p != '\0') {
count++;
p++;
}
return count;
}
Write a program to read and print employees detail using structure.. .....
#include <stdio.h>
#include <stdlib.h>
typedef struct{
char name[30];
int id;
double salary;
} Employee;
int main()
{
//number of employees
int n=2;
//array to store structure values of all employees
Employee employees[n];
//Taking each employee detail as input
printf("Enter %d Employee Details \n \n",n);
for(int i=0; i<n; i++){
printf("Employee %d:- \n",i+1);
//Name
printf("Name: ");
scanf("%[^\n]s",employees[i].name);
//ID
printf("Id: ");
scanf("%d",&employees[i].id);
//Salary
printf("Salary: ");
scanf("%lf",&employees[i].salary);
//to consume extra '\n' input
char ch = getchar();
printf("\n");
}
//Displaying Employee details
printf("-------------- All Employees Details ---------------\n");
for(int i=0; i<n; i++){
printf("Name \t: ");
printf("%s \n",employees[i].name);
printf("Id \t: ");
printf("%d \n",employees[i].id);
printf("Salary \t: ");
printf("%.2lf \n",employees[i].salary);
printf("\n");
}
return 0;
}
Write a program to swap values of two numbers using pointer. .. .
#include<stdio.h>
#include<conio.h>
void main()
{
//Declaration of variables and pointer variables
int x,y,*ptr_x,*ptr_y,temp;
clrscr();
//Insert value of x and y
printf("Enter the value of x and y\n");
scanf("%d%d", &x, &y);
//Printing value before swapping
printf("Before Swapping\nx = %d\ny = %d\n", x, y);
//Assigning address of variables to pointers
ptr_x = &x;
ptr_y = &y;
//Swapping pointer address with the help of temp variable
temp = *ptr_y;
*ptr_y = *ptr_x;
*ptr_x = temp;
//printing values of x and y
printf("After Swapping\nx = %d\ny = %d\n", x, y);
getch();
}
Write a program to find some of all element of an ad using pointer. ....
#include <stdio.h>
#include <malloc.h>
void main()
{
int i, n, sum = 0;
int *a;
printf("Enter the size of array A \n");
scanf("%d", &n);
a = (int *) malloc(n * sizeof(int));
printf("Enter Elements of the List \n");
for (i = 0; i < n; i++)
{
scanf("%d", a + i);
}
/* Compute the sum of all elements in the given array */
for (i = 0; i < n; i++)
{
sum = sum + *(a + i);
/* this *(a+i) is used to access the value stored at the address*/
}
printf("Sum of all elements in array = %d\n", sum);
return 0;}
Write a program to find biggest among three numbers using pointer.. ...
#include <stdio.h>
int main()
{
int num1, num2, num3;
int *p1, *p2, *p3;
//taking input from user
printf("Enter First Number: ");
scanf("%d",&num1);
printf("Enter Second Number: ");
scanf("%d",&num2);
printf("Enter Third Number: ");
scanf("%d",&num3);
//assigning the address of input numbers to pointers
p1 = &num1;
p2 = &num2;
p3 = &num3;
if(*p1 > *p2)
{
if(*p1 > *p3)
{
printf("%d is the largest number", *p1);
}
else
{
printf("%d is the largest number", *p3);
}
}
else
{
if(*p2 > *p3)
{
printf("%d is the largest number", *p2);
}
else
{
printf("%d is the largest number", *p3);
}
}
return 0;
}
Priyanshu verma