C Programming | Function Comment, if answer to the questions highlighted in red are correct, or not. Or if any suggestion. #include <stdio.h> int main() { cout<<“Hello World!”; return 0; } /** Function is the code which execute other codes *Here main is a function *Here main is a predefined function, library function *Main is also known as […]
Category: C Coding
C program to use printf() without semicolon ” ; “
C program to use printf( ) funtion without using semicon. /* C program to swap values*/ #include<stdio.h> int main() { /*In if, condition will execute * whether its true of FALSE*/ if(printf(“In If Condition”)) { } /*In switch, condition will execute * whether its true or false*/ switch(printf(“nIn Swich Condition”)) { } return 0; } Output: In If ConditionIn Swich Condition
C program to swap two numbers using 2 variables
C program to swap 2 numbers using 2 variables. /* C program to swap values*/ #include<stdio.h> int main() { int firstNo = 2, secondNo = 4; firstNo = firstNo + secondNo; secondNo = firstNo – secondNo; firstNo = firstNo – secondNo; //Value of firstNo and secondNo swapped printf(“fisrtNo=%d”,firstNo); printf(“nsecondNo=%d”,secondNo); return 0; } Output: fisrtNo=4secondNo=2
C program to find nth term using Arithmetic progrssion
C program to find nth term in given terms. Use Arithmetic progrssion in the program. An arithmetic progression is a sequence of numbers. Arithmetic progression, an = a1+(n-1)*d an = the nᵗʰ term in the sequence a1 = the first term in the sequence d = the common difference between terms /* C program to find nth term using *Arithmetic progrssion */ #include <stdio.h> […]
C program to find sum of first n even positive numbers
C program to find sum of first n even positive numbers ? Sol. /** * C program to print the sum of first n even positive numbers */ #include <stdio.h> int main() { /* Even numbers are integers */ int count, n, sumOfEvenNum=0; /* First positive even numbers starts from 0 and end at n. */ printf(“Enter positive value of n : n”); scanf(“%d”, &n); /* Find the sum of first n even positive numbers */ /*Positive even numbers are 0, 2, 4, 6…. * So increment count by 2 each time */ for(count=0; count<=n; count=count+2) { sumOfEvenNum = sumOfEvenNum + count; } printf(“Sum of first n even numbers = %d”, sumOfEvenNum); return 0; } Output :
C program to calculate sum of first n even numbers
C program to calculate sum of first n even numbers between 0 and n.? Take n as input from user? Sol. Input : n = 6 Output : 42 Even numbers : 2, 4, 6, 8, 10, 12, 14, 16… Sum of first 6 even numbers : = (2+4+6+8+10+12) = 42 /* C program to find *sum of even numbers betwe 0 to n*/ #include <stdio.h> int main() […]
C program to find nth odd number
C program to find nth odd number. Take a number as input. Sol. Input = 5 Output = 9 //nth odd positive number = 2*n-1 First three odd numbers are 1, 3, 5, 7, 9. /* C program to find nth odd number */ #include<stdio.h> int main() { int nOddNumber,oddNum,loop, nthOddNum=0; printf(“Enter a number: n”); scanf(“%d”,&nOddNumber); printf(“%dth odd numbers is:n”,nOddNumber); for(loop=1,oddNum=1;loop<=nOddNumber;loop=loop+1,oddNum=oddNum+2) { nthOddNum = oddNum; } printf(“%d”,nthOddNum); return 0; } Output :
C program to find sum of first n odd positive numbers
C program to find sum of first n odd positive numbers. Take a number n as input. Sol. /** * C program to print the sum of first n odd positive numbers */ #include <stdio.h> int main() { /* odd numbers are integers */ int count, n, sumOfOddNum=0; /* First positive odd numbers starts from 1 and end at n. */ printf(“Enter value of n : n”); scanf(“%d”, &n); /* Find the sum of first n odd positive numbers */ /*Positive odd numbers are 1,3,5,7…. * So increment count = 1 by 2 each time */ for(count=1; count<=n; count=count+2) { sumOfOddNum = sumOfOddNum + count; } printf(“Sum of first n odd numbers = %d”, sumOfOddNum); return 0; } Output :
C program to calculate perimeter and area of a rectangle
C program to calculate perimeter and area of a rectangle.Take length and breadth of ractangle as input. Sol. //C program to calculate perimeter and area of rectangle #include <stdio.h> float widthOfRectangle, heightOfRectangle, areaOfRectangle, perimeterOfRectangle; int main() { printf(“Enter width of rectangle : n”); scanf(“%f”, &widthOfRectangle); printf(“Enter height of rectangle :n”); scanf(“%f”, &heightOfRectangle); //Perimeter of ractangle = 2*(width + height) perimeterOfRectangle = 2*(widthOfRectangle + heightOfRectangle); printf(“Perimeter of the rectangle = %fn”, perimeterOfRectangle); //Area of rectangle = width * height areaOfRectangle = widthOfRectangle * heightOfRectangle; printf(“Area of the rectangle = %fn”, areaOfRectangle); return(0); } Output:
C program to calculate perimeter and area of a square
C program to calculate perimeter and area of a square. Take sides of square as input. Sol. // C program to calculate perimeter and area of a square #include <stdio.h> int main(){ float sideOfSquare, areaOfSquare, perimeterOfSquare; printf(“Enter length of side of squaren”); scanf(“%f”, &sideOfSquare); // Perimeter of Square = 4*Side perimeterOfSquare = 4*sideOfSquare; printf(“Perimeter of square : n%f”, perimeterOfSquare); // Area of Square = Side*Side areaOfSquare = sideOfSquare * sideOfSquare; printf(“nArea of square : n%f”, areaOfSquare); return 0; } Output:
C program to calculate Perimeter and Area of Circle
C Program to calculate perimeter and area of a circle, take radius as input. Sol. //C program to find area and perimeter of a circle #include<stdio.h> int main() { //In circle pi = 3.14 float radius, perimeter, area, pi=3.14; printf(“nEnter the radius of Circle : “); scanf(“%f”, &radius); // Perimeter of circle = 2πr perimeter = 2 * pi * radius; printf(“nPerimeter of Circle : %f”, perimeter); //Area of circle = π*r*r area = pi * radius * radius; printf(“nArea of Circle : %f”, area); return (0); } Output:
C prgoram to convert inch to feet
C program to convert a number in Inch to Feet. Sol. #include<stdio.h> int main() { float inch, feet; printf(“Enter any number in Inch: n”); scanf(“%f”, & inch); //1 Feet = 0.0833333 Inch feet = 0.0833333 * inch; printf (“Value in Inch is:n%f”, feet); return 0; } Output:
C program to convert KM to CM
C program to convert a Kilometer to Centimeter. Take kilometer as input? Sol. Cheat code: 1 Kilometer = 100000 Centimeters 1 Kilometer = 1000 Meters 1 Meter = 100 Centimeters #include<stdio.h> int main() { float kilometer, centimeter; printf(“Enter any number in Kilometer n”); scanf(“%f”, & kilometer); // 1 Kilometer = 100000 Centimeters centimeter = 100000* kilometer; printf (“Value in Centimeter is: %f n”, centimeter); return 0; } Output:
C program to convert meter to centimeter
C Program convert meters in to centimeters. Take meter as input ? Sol. Cheat code: 1 meter = 100 centimeter. #include<stdio.h> int main() { float meter, centimeter; printf(“Enter any number in meter n”); scanf(“%f”, & meter); centimeter = 100 * meter; printf (“Value in centimeter is: %f n”, centimeter); return 0; } Output:
C program to calculate remainder, difference, division, product
C Program to calculate sum, difference, product, division and remainder of two numbers ? Sol. #include <stdio.h> int main() { int num1, num2, sum, difference, product, remainder ; float division; printf( “Please enter two numbers: “); scanf(“%d %d”, & num1, & num2); sum = num1 + num2; difference = num1 – num2; product = num1 * num2; division = num1 / (float)num2; //typecasting remainder = num1 % num2; printf(“Sum = %d n”, sum); printf(“Difference = %d n”, difference); printf(“Product = %d n”, product); printf(“Division = %f n”, division); printf(“Remainder = %d n”, remainder); return 0; } Output: