C Programming

Program to print Non Zero Elements from the matrix

#include<stdio.h>

void main(){

int arr[3][3];

int counter = 0;

printf("Enter Nine Numbers \n");

for(int x = 0; x < 3; x++){

for(int y = 0; y < 3; y++){

scanf("%d", &arr[x][y]);

}

}

for(int x = 0; x < 3; x++){

for(int y = 0; y < 3; y++){

if (arr[x][y] != 0){

counter++;

}

}

}

printf("\n");

printf("Number of Non-Zero Elements = ");

printf("%d", counter);

}

Menu Driven Program to Print Even, Odd and Perfect Numbers


#include<stdio.h>

#include<stdlib.h>

int main()

{

char ch;

int n;

int sum;

do{

printf("Enter The Valoue of N Greater Than 10 ");

scanf("%d",&n);

if(n>10)

{

printf("\nEnter E or e for Even ( Divisible by 8) \n");

printf("Enter O or o for Odd Number (Divisible by 5 and 9\n");

printf("Enter P or p for Perfect Number \n");

printf("Enter X or x for Exit\n");

getchar(); // some times character not accepting then getchar will help in that case

scanf("%c",&ch);


switch(ch)

{

case 'e':

case 'E':

printf("\n Even Number Divisible by 8 \n") ;

for(int i=1;i<=n;i++)

{

if(i%2==0 && i%8==0)

{

printf("%d\n",i);

}

}

break;

case 'o':

case 'O':

printf("\n Odd Number Divisible by 5 and 9\n") ;

for(int i=1;i<=n;i++)

{

if(i%2!=0 && (i%5==0 && i%9==0))

{

printf("%d\n",i);

}

}

break;

case 'p':

case 'P':

printf(" \n Perfect Number \n");

for(int i=1;i<=n;i++)

{

sum=0;

for(int j=1;j<i;j++)

{

if (i%j==0)

{

sum +=j;

}

}

if(sum==i)

{

printf("%d\n",i);

}

}

break;

case 'x':

case 'X':

printf("Exit") ;

exit(1);

deafult:

printf("\n wrong choise\n ");

break;

}

}

else

{

printf("\n Number is less than 10\n");

}


}while(ch!='x' || ch!='X');


return 0;

}


Program to take employee, his/her name, age, and salary using structure.


#include <stdio.h>

struct employee{ // structure definition

char name[20];

int age;

int salary;

}

int main()

{

struct employee person; // declaration of structure variable person

printf("Enter name,age and salary \n");

scanf("%s %d %d", person.name, person.age, person.salary);

printf("%s %d %d \n", person.name, person.age, person.salary);

return 0;

}



Write C program to read and print the student details using structure and Static Memory Allocation.( Name, roll, Perc)

#include <stdio.h>

struct student

{

char name[50];

int roll;

float marks;

} s;

int main()

{

printf("Enter information:\n");

printf("Enter name: ");

scanf("%s", s.name);

printf("Enter roll number: ");

scanf("%d", &s.roll);

printf("Enter marks: ");

scanf("%f", &s.marks);

printf("Displaying Information:\n");

printf("Name: ");

puts(s.name);

printf("Roll number: %d\n",s.roll);

printf("Marks: %.1f\n", s.marks);

return 0;

}


Write C program to read and print the 3 student details using array of structures (Name, roll, Perc)

#include <stdio.h>

#include <string.h>

struct student

{

int id;

char name[30];

float percentage;

};

int main()

{

int i;

struct student record[2];

// 1st student's record

record[0].id=1;

strcpy(record[0].name, "AAA");

record[0].percentage = 86.5;

// 2nd student's record

record[1].id=2;

strcpy(record[1].name, "BBB");

record[1].percentage = 90.5;

// 3rd student's record

record[2].id=3;

strcpy(record[2].name, "CCC");

record[2].percentage = 81.5;

for(i=0; i<3; i++)

{

printf(" Records of STUDENT : %d \n", i+1);

printf(" Id is: %d \n", record[i].id);

printf(" Name is: %s \n", record[i].name);

printf(" Percentage is: %f\n\n",record[i].percentage);

}

return 0;

}



Write C program to read and print the student details using structure and Dynamic Memory Allocation.( Name, roll, Perc)


/*C program to read and print the student details

using structure and Dynamic Memory Allocation.*/

#include <stdio.h>

#include <stdlib.h>

/*structure declaration*/

struct student

{

char name[30];

int roll;

float perc;

};

int main()

{

struct student *pstd;

/*Allocate memory dynamically*/

pstd=(struct student*)malloc(1*sizeof(struct student));

if(pstd==NULL)

{

printf("Insufficient Memory, Exiting... \n");

return 0;

}

/*read and print details*/

printf("Enter name: ");

gets(pstd->name);

printf("Enter roll number: ");

scanf("%d",&pstd->roll);

printf("Enter percentage: ");

scanf("%f",&pstd->perc);

printf("\nEntered details are:\n");

printf("Name: %s, Roll Number: %d, Percentage: %.2f\n",pstd->name,pstd->roll,pstd->perc);

return 0;

}



Write C program to read and print the N student details using structure and Dynamic Memory Allocation. ( Name, roll, Perc)

/*C program to read and print the N student

details using structure and Dynamic Memory Allocation.*/

#include <stdio.h>

#include <stdlib.h>

/*structure declaration*/

struct student

{

char name[30];

int roll;

float perc;

};

int main()

{

struct student *pstd;

int n,i;

printf("Enter total number of elements: ");

scanf("%d",&n);

/*Allocate memory dynamically for n objetcs*/

pstd=(struct student*)malloc(n*sizeof(struct student));

if(pstd==NULL)

{

printf("Insufficient Memory, Exiting... \n");

return 0;

}

/*read and print details*/

for(i=0; i<n; i++)

{

printf("\nEnter detail of student [%3d]:\n",i+1);

printf("Enter name: ");

scanf(" "); /*clear input buffer*/

gets((pstd+i)->name);

printf("Enter roll number: ");

scanf("%d",&(pstd+i)->roll);

printf("Enter percentage: ");

scanf("%f",&(pstd+i)->perc);

}

printf("\nEntered details are:\n");

for(i=0; i<n; i++)

{

printf("%30s \t %5d \t %.2f\n",(pstd+i)->name,(pstd+i)->roll,(pstd+i)->perc);

}

return 0;

}


Write a C program to accept a integer value from the user and print it’s equivalent ASCII value. The ASCII values vary from 0 to 255.

#include <stdio.h>

int main()

{

int a;

printf("Enter a character: ");

// Reads character input from the user

scanf("%d", &a);

// %d displays the integer value of a character

// %c displays the actual character

printf("ASCII character of %d = %c", a, a);

return 0;

}


Write a C program to perform the following arithmetic operations. Accept all the variable values (a, b, c) from the user

(a+b)/c*a

Result = ------------------

(a+c)/2

#include<stdio.h>

int main()

{

float a,b,c, result;

printf("Enter a, b, c ");

scanf("%f %f %f", &a, &b, &c);

result=((a+b)/(c*a))/((a+c)/2);

printf("the result is = %f\n", result);

return 0;


Write a program using the if statement that will read 2 numbers and an operator (+, -, *, /) . Based on the operator, perform the corresponding operation and print the result.

          1. Enter operator either: *

          2. Enter two operands: 6.5 4.25

          3. The operations output is 6.5 * 4.25 = 27.625

#include<stdio.h>


int main()


{


int a, b, res;

char c;

printf ("Enter any one operator +, -, *, / \n");

scanf("%c", &c);

printf ("\n Enter two numbers \n");

scanf ("\n %d \n %d",&a, &b);


if (c=='+')

{

res=a+b;

printf("\n The sum is %d",res);

}


else if(c== '-')

{

res=a-b;

printf("\n The difference is %d",res);

}


else if(c== '*')

{

res=a*b;

printf("\n The product is %d",res);

}


else if(c==’/’)

{

res=a/b;

printf("\n The quotient is %d",res);

}


else

{

printf ("\n Invalid entry");

}


return 0;


}





Write a program to add two user entered number without using third variable.

#include<stdio.h>

int main()

{

int a, b;

printf("Enter two numbers to add\n");

scanf("%d%d", &a, &b);

a+=b;

printf("Sum of the numbers = %d\n", a);

return 0;

}



Write a program to swap the value of two variable

for example:-

value before swapping value after swapping

a=10 a=20

b=20 b=10


#include <stdio.h>

int main()

{

int x, y, t;

printf("Enter two integers\n");

scanf("%d%d", &x, &y);

printf("Before Swapping\nFirst integer = %d\nSecond integer = %d\n", x, y);

t = x;

x = y;

y = t;

printf("After Swapping\nFirst integer = %d\nSecond integer = %d\n", x, y);

return 0;

}


Find the area of a right triangle with a base of b centimeters and a height of h centimeters. (Take input of b and h from keyboard)

by using following formula:-

A= 1/2 *(b*h)


#include <stdio.h>

int main()

{

float base, height, area;


/* Input base and height of triangle */

printf("Enter base of the triangle: ");

scanf("%f", &base);

printf("Enter height of the triangle: ");

scanf("%f", &height);


/* Calculate area of triangle */

area = (base * height) / 2;


/* Print the resultant area */

printf("Area of the triangle = %.2f sq. units", area);


return 0;

}


Write a C program to calculate the division of a student in 5 subjects. Consider the maximum marks in a subject is 100.

The division will be decided as below


Percentage Division

>80 Distinction

>=60 First

>=45 Second

<45 Fail


/**

* C program to enter marks of five subjects and find percentage and grade

*/


#include <stdio.h>


int main()

{

int phy, chem, bio, math, comp;

float per;


/* Input marks of five subjects from user */

printf("Enter five subjects marks: ");

scanf("%d%d%d%d%d", &phy, &chem, &bio, &math, &comp);



/* Calculate percentage */

per = (phy + chem + bio + math + comp) / 5.0;


printf("Percentage = %.2f\n", per);


/* Find grade according to the percentage */

if(per > 80)

{

printf("Distinction");

}

else if(per >= 60)

{

printf("First");

}

else if(per >= 45)

{

printf("Second");

}

else

{

printf("Fail");

}


return 0;

}


Write a program to read a character in upper case and convert it to the lower case.

/* C Program - Convert Uppercase Character to Lowercase */

#include<stdio.h>


void main()

{

char ch;

printf("Enter a character in uppercase : ");

scanf("%c",&ch);

ch=ch+32;

printf("character in lowercase = %c",ch);

}


Write a C program to accept the marks obtained by a student in 5 courses and calculate average and percentage. (Consider maximum marks in a

#include <stdio.h>

void main()

{

float m1,m2,m3,m4,m5,avg,percent;

printf("\nEnter the marks of 5 subjects in the order=");

scanf("%f %f %f %f %f",&m1,&m2,&m3,&m4,&m5);

avg=(m1+m2+m3+m4+m5)/5;

percent=((m1+m2+m3+m4+m5)/500)*100;

printf("\nThe average is= %f", avg);

printf("\nThe percentage is= %f", percent);

}

Sample Input- 50,20,60,90,85

Output Shown-

Enter the marks of 5 subjects in the order=50 20 60 90 85

The average is= 61.0000

The percentage is= 61.0004

Write a C program to accept the radius of circle and find the area & perimeter of circle for example, constant for pi=3.14 Area=pi r*r; and perimeter =2*pi*r;


#include <stdio.h>

#define pi 3.142

void main()

{

float radius,area,perimeter;

printf("\nEnter the radius= ");

scanf("%f",&radius);

area=pi*radius*radius;

perimeter=2*pi*radius;

printf("\nThe area is= %f",area);

printf("\nThe perimeter is= %f",perimeter);

}

Sample input- 5

Output shown-

Enter the radius= 5

The area is= 78.55



The total salary of any employ is combination of basic, HRA and allowance. The HRA is 40% of Basic. The basic and allowance will be accepted from the user. Write a C program to calculate the total salary and display.

#include<stdio.h>

void main()

{

int sal,bas,al,HRA;

printf(“\nEnter the basic= );

scanf(“%d”,&bas);

printf(“\nEnter the allowance= “);

scanf(“%d”,al);

HRA=(bas*40)/100;

sal=bas+al+HRA;

printf(“\nThe salary is= %d”,sal);

}

Sample input- 5000 3000

Output shown-

Enter the basic= 5000

Enter the allowance= 3000

The salary is= 10000



Write a C program to accept 4 digit number from the input device and perform the sum of its digits.

example:

Input: 1234

output: 4 + 3+ 2 + 1 = 10


#include <stdio.h>

void main()

{

int r1,r2,r3,r4,sum,x;

printf("\nEnter a 4 digit number= ");

scanf("%d",&x);

r1=x%10;

x=x/10;

r2=x%10;

x=x/10;

r3=x%10;

x=x/10;

r4=x%10;

x=x/10;

sum=r1+r2+r3+r4;

printf("\nSum of the digits is= %d",sum);

}

Sample Input- 1234

Output shown-

Enter a 4 digit number= 1234

Sum of the digits is= 10



Write a program which takes a user input character value and prints if its vowel or consonant or any invalid alphabet using switch statement.

#include <stdio.h>

int main()

{char ch;

printf("enter an alphabet");

scanf("%c \n", &ch);

switch(ch)

{case'a':

case'A':

case'e':

case'E':

case'i':

case'I':

case'o':

case'O':

case'u':

case'U':

printf("alphabet is a vowel");break;

default:

printf("alphabet is a consonant or invalid alphabet");break;

}

}

INPUT:

Enter an alphabet

Y

OUTPUT:

Alphabet is a consonant or invalid alphabet.



Write a program using the switch statement that will Read 2 numbers and an operator (+,-,*,/) . Based on the operator, perform the corresponding operation and print the result.

#include <stdio.h>

int main()

{int a, b, c;

char ch;

printf("Enter your operator\n");

scanf("%c", &ch);

printf("Enter the values of a and b\n");

scanf("%d %d", &a, &b);

switch(ch)

{case '+': c = a + b;

printf("result is %d", c); break;

case '-': c = a - b;

printf("result is %d", c);break;

case '*': c = a * b;

printf("result is %d", c);break;

case '/': c = a / b;

printf("result is %d", c);break;

default: printf("error");break;

}

}

INPUT:

Enter an operator

-

Enter two numbers

75

15




OUTPUT:

Result is 60


Write a program using the switch statement to read a single digit no (0..9) and print the corresponding word (Zero, One .. Nine etc,).

#include <stdio.h>

int main()

{int a;

printf("enter a number between 0-9");

scanf("%d", &a);

switch(a)

{case 0:

printf("the corresponding word is ZERO");break;

case 1:

printf("the corresponding word is ONE");break;

case 2:

printf("the corresponding word is TWO");break;

case 3:

printf("the corresponding word is THREE");break;

case 4:

printf("the corresponding word is FOUR");break;

case 5:

printf("the corresponding word is FIVE");break;

case 6:

printf("the corresponding word is SIX");break;

case 7:

printf("the corresponding word is SEVEN");break;

case 8:

printf("the corresponding word is EIGHT");break;

case 9:

printf("the corresponding word is NINE");break;

default:

printf("error");break;

}

}

INPUT:

Enter a number between 0-9

8

OUTPUT:

The corresponding word is EIGHT


Write a program using the switch statement to print the number of days in a month using switch case.

#include <stdio.h>

int main()

{int a;

printf("input month number");

scanf("%d", &a);

switch(a)

{case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12:

printf("there are 31 days in the month");break;

case 2:

printf("there are 29 or 28 days in the month");break;

case 4:

case 6:

case 9:

case 11:

printf("there are 30 days in the month");break;

default:

printf("error");break;

}

}

INPUT:

Input month number

6

OUTPUT:

There are 30 days in the month.

Write a program using the if statement that will read 2 numbers and an operator (+, -, *, /) . Based on the operator, perform the corresponding operation and print the result. Enter operator either: * Enter two operands: 6.5 4.25 The operations output is 6.5 * 4.25 = 27.625

#include <stdio.h>

int main()

{float a,b,c;

char op;

printf("Enter your operator \n");

scanf("%c", &op);

printf("Enter any two numbers \n");

scanf("%f %f", &a, &b);

if (op=='+')

{c= a + b;

printf("the result of addition is %f", c);

}

else if (op=='-')

{c= a - b;

printf("the result of subtraction is %f", c);

}

else if (op=='*')

{c= a * b;

printf("the result of multiplication is %f", c);

}

else if (op=='/')

{c= a / b;

printf("the result of division if %f", c);

}

}

INPUT:

Enter your operator

*

Enter any two numbers

6.5

4.25

OUTPUT:

The result of multiplication is 27.625000



Write a C program using function that takes a character indicating a color as an input argument and displays the full color if it is present else prints the message that the color is not present. Here function doesn't return anything. Rainbow colors:

R O Y G B I V = Red Orange Yellow Green Blue Indigo Violet.


#include<stdio.h>


main()


{


char c;


printf ("Enter the colour");


scanf("\n %c", &c);


print(c);


return 0;

}

void print (c)


{


switch(c)

{

case 'r':

printf("\n The colous is red");

break;

case 'o':

printf("\n The color is orange");

break;

case 'g':

printf("The color is orange");

break;


case 'y':

printf("\n The color is yellow");

break;


default: printf ("\n Invalid colour");

}

return 0;

}


Write a program in C to convert decimal number to binary number using the function.

#include <stdio.h>

#include <math.h>


long decimalToBinary(int decimalnum)

{

long binarynum = 0;

int rem, temp = 1;


while (decimalnum!=0)

{

rem = decimalnum%2;

decimalnum = decimalnum / 2;

binarynum = binarynum + rem*temp;

temp = temp * 10;

}

return binarynum;

}


int main()

{

int decimalnum;

printf("Enter a Decimal Number: ");

scanf("%d", &decimalnum);

printf("Equivalent Binary Number is: %ld", decimalToBinary(decimalnum));

return 0;

}


Write a function to find if a given number is prime or not. Function takes number as input argument. It returns 1 if number is prime else return 0. Based on the return value, print the appropriate message in main function.


#include <stdio.h>

int checkPrime(int n);

int main() {

int n, i, flag = 0;

printf("Enter a positive integer: ");

scanf("%d", &n);


for (i = 2; i <= n / 2; ++i) {

// condition for i to be a prime number

if (checkPrime(i) == 1) {

// condition for n-i to be a prime number

if (checkPrime(n - i) == 1) {

printf("%d = %d + %d\n", n, i, n - i);

flag = 1;

}

}

}

if (flag == 0)

printf("%d cannot be expressed as the sum of two prime numbers.", n);


return 0;

}


// function to check prime number

int checkPrime(int n) {

int i, isPrime = 1;

for (i = 2; i <= n / 2; ++i) {

if (n % i == 0) {

isPrime = 0;

break;

}

}

return isPrime;

}


Write a program that takes the x-y coordinates of a point in the Cartesian plane. Write a function which takes as input the x-y coordinates and returns the quadrant in which it lies.

#include <stdio.h>

void main()

{

int co1,co2;


printf("Input the values for X and Y coordinate : ");

scanf("%d %d",&co1,&co2);

findcoordinate(co1,co2);

}

void findcoordinate(int co1,int co2)

{


if( co1 > 0 && co2 > 0)

printf("The coordinate point (%d,%d) lies in the First quandrant.\n",co1,co2);

else if( co1 < 0 && co2 > 0)

printf("The coordinate point (%d,%d) lies in the Second quandrant.\n",co1,co2);

else if( co1 < 0 && co2 < 0)

printf("The coordinate point (%d, %d) lies in the Third quandrant.\n",co1,co2);

else if( co1 > 0 && co2 < 0)

printf("The coordinate point (%d,%d) lies in the Fourth quandrant.\n",co1,co2);

else if( co1 == 0 && co2 == 0)

printf("The coordinate point (%d,%d) lies at the origin.\n",co1,co2);


}