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

C Language Codes

This document contains 12 questions and their solutions in C programming language. Each question demonstrates a different concept such as input/output, arithmetic operations, conditional statements, loops etc. For each question, the code and output are provided. The questions cover basic topics like printing data types, user input, calculations for area and perimeter, checking even/odd numbers etc. Overall, the document serves as a reference for learning basic C programming concepts through examples.

Uploaded by

Samarth
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

C Language Codes

This document contains 12 questions and their solutions in C programming language. Each question demonstrates a different concept such as input/output, arithmetic operations, conditional statements, loops etc. For each question, the code and output are provided. The questions cover basic topics like printing data types, user input, calculations for area and perimeter, checking even/odd numbers etc. Overall, the document serves as a reference for learning basic C programming concepts through examples.

Uploaded by

Samarth
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

C Language Codes

By - Samarth Guha

Question 1 C program to print your name.

Code: //C language program to print Hello World


#include <stdio.h>
int main(){
printf("Hello World \n");
printf("Hello World \n");
printf("Hello World");
printf("Hello World");
return 0;

Output: Hello World


Hello World
Hello WorldHello World
Question 2 C program to Data types in C programming language using printf function
to print them.

Code: #include <stdio.h>

int main(){
int age=22 ;
printf("Age is: %d \n",age);
float pi=3.14;
printf("The value of pi is: %f \n",pi);
char star='*';
printf("Star is: %c",star);
return 0;
}

Output: Age is: 22


The value of pi is: 3.140000
Star is: *

Question 3 C program to input a number from the user using the scanf function and
print it.

Code: //C program to input a number from the user


using the scanf function and print it

#include <stdio.h>

int main(){
int age;
printf("Enter age: ");
scanf("%d", &age);
printf("Age is: %d",age);
return 0;

Output: Enter age: 23


Age is: 23
Question 4 C language program to print the area of square with side provided.
Code: //C language program to print the area of square with side provided

#include <stdio.h>

int main(){
int side;//float side for floating point values
printf("Enter side: ");
scanf("%d",&side);
printf("The area of square with side given by you is: %d",side*side);
return 0;

Output: Enter side: 4


The area of square with side given by you is: 16

Question 5 C language program to calculate area of circle with radius given by the user.

Code: //C language program to calculate area of


circle with radius given by the user

#include <stdio.h>

int main(){
float radius;
printf("Enter the radius: ");
scanf("%f",&radius);
printf("The area of circle with value
entered by you is: %f",3.14*radius*radius);
return 0;

Output: Enter the radius: 4.5


The area of circle with value entered by you is: 63.585000

Question 6 C language code to calculate the perimeter of rectangle with value of sides
given by the user.
Code: //C language code to calculate the perimeter
of rectangle with value of sides given by the
user

#include <stdio.h>

int main(){
int l,b;
printf("Enter the Length: ");
scanf("%d",&l);
printf("Enter the breadth: ");
scanf("%d",&b);
printf("The perimeter of rectangle with
values given by you is: %d",2*(l+b));
return 0;

Output: Enter the Length: 7


Enter the breadth: 4
The perimeter of rectangle with values given by you is: 22

Question 7 C Language program to input 2 numbers from the user and print their sum.
Code: //C Language program to input 2 numbers
from the user and print their sum

#include <stdio.h>

int main(){
int a; //can also be written commonly as int
a,b;
printf("Enter the first number: ");
scanf("%d",&a);

int b;
printf("Enter the second number: ");
scanf("%d",&b);

int sum = a+b;//instead of creating a new


variable we con also write a+b in the printf
statement
printf("The sum of the two numbers is:
%d",sum);//a+b can also be written
return 0;

Output: Enter the first number: 34


Enter the second number: 23
The sum of the two numbers is: 57

Question 8 C language program to check if a number entered by the user is divisible by


2 or not.
Code: //C language program to calculate area of
circle with radius given by the user

#include <stdio.h>

int main(){
float radius;
printf("Enter the radius: ");
scanf("%f",&radius);
printf("The area of circle with value
entered by you is: %f",3.14*radius*radius);
return 0;

Output: Enter a number: 34


The answer is: 1
//1 means True and 0 means False

Question 9 C Language practice question Apni Kaksha.

Code: // C Language practice question Apni Kaksha

#include <stdio.h>

int main(){
int isSunday = 1;
int isSnowing = 1;
printf("The answer is: %d",isSunday &&
isSnowing);

int isMonday = 1;
int isRaining = 0;
printf("The answer is: %d",isMonday ||
isRaining);
return 0;

Output: The answer is: 1The answer is: 1


Question C language program to check whether the number entered by the user is in
10 the range 9 – 100 // C language program to check whether the number
entered by the user is a two digit number of not.

Code: //C language program to check whether the number entered by the user is in the
range 9 - 100
//C language program to check whether the number entered by the user is a two
digit number of not

#include <stdio.h>

int main(){
int x;
printf("Enter a number: ");
scanf("%d",& x);
printf("The number entered by you is in between 9 to 100: %d",x>9 &&
x<=100);//1 for true and 0 for false
return 0;

Output: Enter a number: 34


The number entered by you is in between 9 to 100: 1
//1 means True and 0 means False
Question C language program to find out the average of 3 numbers.
11

Code: //C language program to find out the average


of 3 numbers

#include <stdio.h>

int main(){
int a;
printf("Enter the first number: ");
scanf("%d",& a);

int b;
printf("Enter the second number: ");
scanf("%d",& b);

int c;
printf("Enter the third number: ");
scanf("%d",& c);
int d = a+b+c/3;
printf("The average of three numbers is:
%d",d);
return 0;

Output: Enter the first number: 3


Enter the second number: 4
Enter the third number: 7
The average of three numbers is: 9
Question C Language program to check if a number is even or odd.
12

Code: // C Language program to check if a number


is even or odd

#include <stdio.h>

int main(){
//even -> 1
//odd -> 0
int n;
printf("Enter a number: ");
scanf("%d",& n);
printf("The number entered by you is:
%d",n%2 == 0);
return 0;

Output: Enter a number: 4


The number entered by you is: 1
Question 2 C program to Data types in C programming language using printf function
to print them.

Code: #include <stdio.h>

int main(){
int age=22 ;
printf("Age is: %d \n",age);
float pi=3.14;
printf("The value of pi is: %f \n",pi);
char star='*';
printf("Star is: %c",star);
return 0;
}

Output: Age is: 22


The value of pi is: 3.140000
Star is: *
Question 2 C program to Data types in C programming language using printf function
to print them.

Code: #include <stdio.h>

int main(){
int age=22 ;
printf("Age is: %d \n",age);
float pi=3.14;
printf("The value of pi is: %f \n",pi);
char star='*';
printf("Star is: %c",star);
return 0;
}

Output: Age is: 22


The value of pi is: 3.140000
Star is: *
Question 2 C program to Data types in C programming language using printf function
to print them.

Code: #include <stdio.h>

int main(){
int age=22 ;
printf("Age is: %d \n",age);
float pi=3.14;
printf("The value of pi is: %f \n",pi);
char star='*';
printf("Star is: %c",star);
return 0;
}

Output: Age is: 22


The value of pi is: 3.140000
Star is: *

You might also like