Category: Programming Examples in C

Program to Demonstrate Typecasting in C 4

Program to Demonstrate Typecasting in C

Here is a program to demonstrate typecasting in C programming language. #include #include void main(){ float result; int num1, num2; clrscr(); num1 = 10; num2 = 3; result = num1/num2; printf(“\nBefore Typecasting: %f”,result); /* As num1 and num2 are declared as int, it will return int value and it will get stored in the variable “result” as float i.e. 3.000000 To solve this problem we can use typecasting. */ result = (float)num1/num2; printf(“\nAfter Typecasting: %f”,result); getch(); } Output: Before Typecasting: 3.000000 After Typecasting: 3.333333

Program to check if a number is Armstrong or not 4

Program to check if a number is Armstrong or not

Definition (Armstrong Number): An n-digit number equal to the sum of the nth powers of its digits. Example: 153 = 13 + 53 + 33 1634 = 14 + 64 + 34 + 44 #include<stdio.h> #include<math.h> int main(){ long int num =153, count = 0, i, temp1, temp2, temp3 = 0; clrscr(); printf(“Enter the number: “); scanf(“%ld”,&num); if(num <= 0){ printf(“\nEnter the number greater than 0”); return 0; } temp1 = temp2 = num; while(temp1!=0){ temp1=temp1/10; count++; } for(i=0;i < count; i++) { temp3 = temp3 + pow((temp2%10),count); temp2 = temp2/10; } if(num == temp3) { printf(“\n %d is an...

Program to Calculate Length of a String (Using strlen in-built function) 5

Program to Calculate Length of a String (Using strlen in-built function)

#include #include #include void main(){ int strlength; char *str; clrscr(); printf(“\nEnter the string: “); gets(str); strlength=strlen(str); printf(“\nThe length of the string is %d.”,strlength); getch(); } Output: Enter the string: Learn C Online The length of the string is 14.

Program to Calculate Length of a String (without using strlen in-built function). 30

Program to Calculate Length of a String (without using strlen in-built function).

#include #include void main(){ char *str; int count = 0,i; clrscr(); printf(“\nEnter the String: “); gets(str); for(i=0;str[i]!=’\0′;i++){ count++; } printf(“\nThe length of the string is %d.”,count); getch(); } Output: Enter the String: Learn C Online The length of the string is 14.

Program in C to reverse a given Number 9

Program in C to reverse a given Number

Below is the C program to reverse a given number. This C program will accept a number (in a variable named “num”) and performs an operation to reverse the number. #include <stdio.h> #include <stdlib.h> int main(void) { int num=1234, newNum=0; while(num!=0){ newNum=newNum*10 + num%10; num=num/10; } printf(“\n”); printf(“%d”, newNum); return 1; } Output: 4321 Explanation: The statement while(num!=0) will be executed till value of num !=0 num%10 (read as num mod 10) will return the last digit. So in 1st iteration, it will return 4. So, newNum will become–> 0*10 + 4 –>4 Next line i.e. num/10 will return 123...

FREE C Cheatsheet - Speed Up Your C Programming.

FREE C Cheatsheet - Speed Up Your C Programming.

Download a 7-page free cheat sheet for easy and quick access to C Concepts, Snippets, and Syntax.

Thank you! Check you inbox and access your cheat-sheet