Q: Write a program to calculate area of a square.
#include <stdio.h>
int main() {
float side;
printf("Enter side of a square: ");
scanf("%f", &side);
printf("Area of a square is %f", side*side);
return 0;
}
Q: WAP to calculate area of a circle.
#include <stdio.h>
int main() {
float radius;
printf("Enter radius of a circle: ");
scanf("%f", &radius);
printf("Area of a Circle is %f", 3.14 * radius * radius);
return 0;
}
Q: WAP to calculate perimeter of rectangle.
#include <stdio.h>
int main() {
float len,wid;
printf("Enter length and width of a rectangle\n");
scanf("%f %f", &len, &wid);
printf("Perimeter of rectangle is %f", 2 * (len + wid));
return 0;
}
Q: Take a number n from user and output its cube.
#include <stdio.h>
int main() {
int n;
printf("Enter number: ");
scanf("%d", &n);
printf("Cube is %d", n * n * n);
return 0;
}
Q: WAP to check if a number is divsible by 2 or not.
#include <stdio.h>
int main() {
int n;
printf("Enter number: ");
scanf("%d", &n);
// if(n % 2 == 0) {
// printf("Divide");
// }
// else {
// printf("Not Divide");
// }
// OR
printf("%d", n % 2 == 0); // 0 - Divide, 1 - Not divide
return 0;
}
Q: Are the following valid or not?
#include <stdio.h>
int main() {
int a = 8^8; // Valid
int x; int y = x; // Valid
int _x, _y = _x; // Not Valid
char stars = '**'; // Not Valid
return 0;
}
Q: If a number is greater than 9 and less than 100, print true.
#include <stdio.h>
int main() {
// 1 - True , 0 - False
int n;
printf("Enter number: ");
scanf("%d", &n);
printf("%d", (n > 9) && (n < 100));
return 0;
}
Q: WAP a program to print the average of 3 numbers.
#include <stdio.h>
int main() {
printf("Exit press 0\n");
float n, sum = 0.0;
int numCount = 0;
while (1){
scanf("%f", &n);
if (n == 0) {
break;
}
else {
sum += n;
numCount++;
}
}
// OR
// for (int i=0; ; i++){
// scanf("%f", &n);
// if (n == 0) {
// break;
// }
// else {
// sum += n;
// numCount++;
// }
// }
printf("Average is: %f", sum/numCount);
return 0;
}
Q: Check given character is a digit or not?
#include stdio.h>
#include ctype.h>
int main() {
char ch;
printf("Enter a character or number ");
scanf("%c", &ch);
printf("%d", isdigit(ch));
return 0;
}
Q: Write a program to calculate simple interest for a set of values representing principal, number of years and rate of interest?
#include stdio.h>
int main() {
int p = 2000;
float r = 2.5, t = 1;
printf("Simple interest is : %f", (p*r*t)/100);
return 0;
}
Q: What will be the output of this program.
#include <stdio.h>
int main () {
int a = 10;
if(a = 11) {
printf("I am 11");
}
else {
printf("I am not 11");
}
return 0;
}
Q: Find the smallest number?
#include <stdio.h>
// Smallest among two numbers
int main() {
int a, b, small;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
if(a > b){
small = b;
}
else{
small = a;
}
printf("Smallest number : %d", small);
return 0;
}
#include <stdio.h>
// Smallest number in an array
int main() {
int n, smallest;
printf("How many numbers want to add ?\n");
scanf("%d", &n);
int arr[n];
printf("Enter %d numbers\n", n);
for(int i=0; i<n; i++){
scanf("%d", &arr[i]);
}
smallest = arr[0];
for(int i=0; i<n; i++){
if(arr[i] < smallest){
smallest = arr[i];
}
}
printf("Smallest number : %d", smallest);
return 0;
}
Write a program to check if a student passed or failed?
#include <stdio.h>
int main() {
int marks;
printf("Enter your marks: ");
scanf("%d", &marks);
if(marks >= 0 && marks <= 30){
printf("Fail");
}
else if(marks > 30 && marks <= 100){
printf("Pass");
}
else{
printf("Invalid marks");
}
return 0;
}
Q: Addon to previous question to give Grade the students.
#include <stdio.h>
int main() {
int marks;
printf("Enter your marks: ");
scanf("%d", &marks);
if (marks < 30){
printf("Grade C");
}
else if(marks >= 30 && marks < 70){
printf("Grade B");
}
else if(marks >= 70 && marks < 90){
printf("Grade A");
}
else if(marks >= 90 && marks <= 100){
printf("Grade A+");
}
else{
printf("Invalid marks");
}
return 0;
}
Q: What will be the output?
#include <stdio.h>
int main() {
int x = 2;
if (x = 0){ //if x = 1 excute if statement else x = 0 excute else statement.
printf("x = 1");
}
else{
printf("x != 1");
}
return 0;
}
Q: Calculate income tax paid by an employee to the govt. as per the slabs mentioned below.
Income Slab Tax
2.5 - 5L 5%
5 - 10L 20%
Above 10L 30%
Note: There is no tax below 2.5L. Take income amount as an input from the user.
#include<stdio.h>
int main () {
printf("Enter your income in Lakhs: ");
float income;
scanf("%f", &income);
if(income < 2.5) {
printf("Zero Tax");
}
else if(income >= 2.5 && income < 5.0) {
printf("Pay 5%% tax on your income i.e %f", (income * 100000 * 5) / 100);
}
else if(income >= 5.0 && income < 10.0) {
printf("Pay 20%% tax on your income i.e %f", (income * 100000 * 20) / 100);
}
else if(income >= 10.0) {
printf("Pay 30%% tax on your income i.e %f", (income * 100000 * 30) / 100);
}
else {
printf("Enter valid income.");
}
return 0;
}
Q: WAP to find whether a year entered by the user in a leap year or not.
#include<stdio.h>
int main () {
printf("Enter year: ");
int year;
scanf("%d", &year);
if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
printf("This is a leap year");
}
else {
printf("This is not a leap year");
}
return 0;
}
Q: WAP to find greatest of four numbers entered by the user.
#include<stdio.h>
int main () {
printf("Enter the four numbers: ");
float num1, num2, num3, num4;
scanf("%f %f %f %f", &num1, &num2, &num3, &num4);
if(num1 > num2 && num1 > num3 && num1 > num4) {
printf("The greatest number is %f", num1);
}
else if(num2 > num1 && num2 > num3 && num2 > num4) {
printf("The greatest number is %f", num2);
}
else if(num3 > num1 && num3 > num2 && num3 > num4) {
printf("The greatest number is %f", num3);
}
else if(num4 > num1 && num4 > num2 && num4 > num3) {
printf("The greatest number is %f", num4);
}
else {
printf("Enter valid number.");
}
return 0;
}
Q: Write a program to find if a character entered by user is upper case or not?
#include <stdio.h>
#include <ctype.h>
int main() {
char ch;
printf("Enter character: ");
scanf("%c", &ch);
//Option 1
// printf("%d", isupper(ch));
//Option 2
if (ch >= 'A' && ch <= 'Z'){
printf("Upper case");
}
else if (ch >= 'a' && ch <= 'z'){
printf("Lower case");
}
else{
printf("Invalid character");
}
return 0;
}
Q: Write a program to check if a given number is Armstrong number or not?
#include <stdio.h>
#include <Math.h>
// Armstrong number: Input 371 -> (3^3) + (7^3) + (1^3)
int main() {
int num, copyNum1, copyNum2, separateNum, numLength = 0, armstrongNum = 0;
printf("Enter number: ");
scanf("%d", &num);
copyNum1 = num;
copyNum2 = num;
// Get Length
while(copyNum1){
numLength++;
copyNum1 /= 10;
}
// Calculate Armstrong Number
while(copyNum2){
separateNum = copyNum2 % 10;
armstrongNum += pow(separateNum, numLength);
copyNum2 /= 10;
}
if(armstrongNum == num){
printf("Armstrong number");
}else{
printf("Not a Armstrong number");
}
return 0;
}
Q: WAP to print natural numbers from 10 to 20 when initial loop counter is initialized to 0.
#include <stdio.h>
int main () {
int i = 0;
while (i <= 20){
if (i >= 10){
printf("The value of i is %d\n", i);
}
i++;
}
return 0;
}
Q: Write a program to check if the given number is a natural number?
#include <stdio.h>
int main() {
int num;
printf("Enter number: ");
scanf("%d", &num);
if (num > 0){
printf("Natural number");
}
else{
printf("Not a Natural number");
}
return 0;
}
Q: Print English alphabets.
#include <stdio.h>
int main() {
char ch = 'a';
while(ch <='z'){
printf("%c\n", ch);
ch++;
}
return 0;
}
Q: Print number 1 to 5 using 'Do while' loop.
#include <stdio.h>
int main() {
int i = 1;
do{
printf("%d\n", i);
i++;
}while(i <= 5);
return 0;
}
Q: Calculate sum of natural numbers.
#include <stdio.h>
int main() {
int i = 1, sum = 0, j = 5;
do{
sum += i;
i++;
}while(i <= 5);
printf("%d\n", sum);
do{
printf("%d\n", j);
j--;
}while(j >= 1);
return 0;
}
OR
#include <stdio.h>
/*
def
sum(n) = 1 + 2 + 3 + 4 + 5 + ... + n-1 + n;
sum(n) = sum(n-1) + n;
*/
int sum_natural(int);
int main() {
printf("The sun of first 5 natural number is %d", sum_natural(5));
return 0;
}
int sum_natural(int n) {
if(n == 1) {
return 1;
}
return sum_natural(n-1) + n;
}
Q: Print table.
#include <stdio.h>
int main() {
int num;
printf("Enter number : ");
scanf("%d", &num);
printf("Table of %d:\n", num);
for(int i=1; i<=10; i++){
printf("%d * %d = %d\n", num,i,num*i);
}
return 0;
}
Q: Print the factorial of a number n?
#include <stdio.h>
int main() {
int num, facto = 1;
printf("Enter number : ");
scanf("%d", &num);
if(num == 0 || num == 1){
facto = 1;
}
for(int i=num; i>=1; i--){
facto *= i;
}
printf("Factorial of %d is %d: \n", num, facto);
return 0;
}
Q: Print reverse of the table for a number n.
#include <stdio.h>
int main() {
int num;
printf("Enter number : ");
scanf("%d", &num);
printf("Table of %d \n", num);
for(int i=10; i>=1; i--){
printf("%d * %d = %d\n", num, i, num*i);
}
return 0;
}
Q: Print sum from 5 to 50.
#include <stdio.h>
int main() {
int sum = 0;
for(int i=5; i<=50; i++){
sum += i;
}
printf("Sum from 5 to 50 is : %d \n", sum);
return 0;
}
Q: Write a program to check if a number is prime or not.
#include <stdio.h>
int main() {
int num, index = 0, i = 1;
printf("Enter number: ");
scanf("%d", &num);
while(i <= num){
if(num % i == 0){
index++;
}
i++;
}
if(index == 2){
printf("%d is prime", num);
}
else{
printf("%d is not prime", num);
}
return 0;
}
Q: Write a program to print prime numbers in a range?
#include <stdio.h>
int main() {
int num, index = 0, i = 1, j = 1;
printf("Enter number: ");
scanf("%d", &num);
while(i <= num){
while(j <= i){
if(i % j == 0){
index++;
}
j++;
}
if(index == 2){
printf("%d, ", i);
}
index = 0;
j = 1;
i++;
}
return 0;
}
Q: WAP to check whether a given number is prime or not without using loops.
#include <stdio.h>
// Recursive function to check if a number is prime
int is_prime(int n, int divisor) {
// Base cases
if (n <= 1) {
return 0; // Numbers <= 1 are not prime
}
if (divisor == 1) {
return 1; // If no divisors found, it's prime
}
if (n % divisor == 0) {
return 0; // If divisible, it's not prime
}
// Recursive case: Check next smaller divisor
return is_prime(n, divisor - 1);
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
// Call the recursive function starting with divisor = num / 2
if (is_prime(num, num / 2)) {
printf("%d is a prime number.\n", num);
} else {
printf("%d is not a prime number.\n", num);
}
return 0;
}
Q: WAP using function to print the right angle triangle pattern.
#include <stdio.h>
void right_triangle_pattern(int);
int main() {
right_triangle_pattern(5);
return 0;
}
void right_triangle_pattern(int n) {
for(int i=0; i<n; i++) {
for(int j=0; j<=i; j++) {
printf("*");
}
printf("\n");
}
}
Q: WAP using function to print the triangle pattern.
#include <stdio.h>
void right_triangle_pattern(int);
int main() {
right_triangle_pattern(3);
return 0;
}
void right_triangle_pattern(int n) {
for(int i=0; i<n; i++) {
for(int j=0; j<2*i+1; j++) {
printf("*");
}
printf("\n");
}
}
Q: Write a functions to solve basic calculations?
#include <stdio.h>
#include <math.h>
void calcAdd(int a, int b);
int calcSub(int a, int b);
double calcPower(int a, int b);
void calcAreaCircle(int a);
void calcAreaSquare(int a);
void calcAreaRectangle(int a, int b);
int main() {
int a = 5, b = 2;
// Add
calcAdd(a, b);
// Sub
int res = calcSub(a, b);
printf("%d \n", res);
// Power calculation
printf("%f \n", calcPower(a, b));
// Area of Square
calcAreaSquare(a);
// Area of Circle
calcAreaCircle(a);
// Area of Rectangle
calcAreaRectangle(a, b);
return 0;
}
void calcAdd(int a,int b){
printf("%d\n", a + b);
}
int calcSub(int x, int y){
return x - y;
}
double calcPower(int a, int b){
return pow(a, b);
}
void calcAreaSquare(int a){
printf("%d \n", a * a);
}
void calcAreaCircle(int a){
printf("%f \n", M_PI * pow(a, 2));
}
void calcAreaRectangle(int a, int b){
printf("%d \n", a * b);
}
Q: Guess the output.
#include <stdio.h>
int main() {
int n = 4, _n = 4;
printf("%d %d %d\n", n, ++n, n++); // 6 6 4
printf("%d %d %d\n", _n, _n++, ++_n); // 6 5 6
// Compiler automatically evalute the execuation of the code from right to left
return 0;
}
Q: Print sum of n natural numbers using Recursion.
#include <stdio.h>
// Using Recursion
int sum(int value);
int main(){
printf("%d", sum(5));
return 0;
}
int sum(int value){
if(value == 1){
return 1;
}
int res = sum(value-1) + value;
return res;
}
Q: Print factorial of n numbers.
#include <stdio.h>
double factorial(int n);
// Using Recursion
int main(){
int n;
printf("Enter number: ");
scanf("%d", &n);
printf("Factorial of %d is %f", n,factorial(n));
return 0;
}
double factorial(int n){
if(n == 0){
return 1;
}
double facto1 = factorial(n-1);
double facto = facto1 * n;
return facto;
}
Q: Write an function to convert celsius to Fahrenheit.
#include <stdio.h>
float calcTemp(float celsius);
// Using Recursion
int main(){
float celsius;
printf("Enter temperature in Celsius : ");
scanf("%f", &celsius);
printf("Temperature in Faherenheit is %f deg.", calcTemp(celsius));
return 0;
}
float calcTemp(float celsius){
int fahrenheit = (9.0/5.0 * celsius) + 32;
return fahrenheit;
}
Q: Write a program to print n terms of the fibonacci sequence?
#include <stdio.h>
int fibonacciSeq(int n);
// Using Recursion
int main(){
printf("%d", fibonacciSeq(6));
return 0;
}
int fibonacciSeq(int n){
if(n == 0){
return 0;
}
if(n == 1){
return 1;
}
int fibo = fibonacciSeq(n-1) + fibonacciSeq(n-2);
return fibo;
}
Q: Write a program to print the series of the fibonacci sequence?
#include <stdio.h>
void fibonacciSeq(int n);
// Using Recursion
int main(){
fibonacciSeq(6);
return 0;
}
void fibonacciSeq(int n){
int t1 = 0, t2 = 1;
printf("Fibonacci sequence %d, %d, ", t1, t2);
while(n != 0){
int nextTerm = t1 + t2;
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
n--;
}
}
Q: Write a function to find sum of digits of a number?
#include <stdio.h>
int calcSumOfDigit(int n);
int main(){
int n;
printf("Enter the number: ");
scanf("%d", &n);
printf("%d", calcSumOfDigit(n));
return 0;
}
int calcSumOfDigit(int n){
int sum = 0;
while(n){
sum += n % 10;
n /= 10;
}
return sum;
}
Q: Write a function to find square root of a number.
#include <stdio.h>
#include <math.h>
double calcSquareRoot(int n);
int main(){
int n;
printf("Enter the number: ");
scanf("%d", &n);
printf("%f", calcSquareRoot(n));
return 0;
}
double calcSquareRoot(int n){
return sqrt(n);
}
Q: Write a program to create your own pow function?
#include <stdio.h>
double userDefinedPower(int digit, int power);
int main(){
int digit, power;
printf("Enter the digit : ");
scanf("%d", &digit);
printf("Enter the power of a digit : ");
scanf("%d", &power);
printf("%f", userDefinedPower(digit, power));
return 0;
}
double userDefinedPower(int digit, int power){
int res = 1;
for(int i=1; i<=power; i++){
res *= digit;
}
return res;
}
0 Comments