C Programming Language Solution's Related to Grade 10 and 12
Introduction of c language
C is the programming language developed by Demis M. Ritchie at AT and I's Bell laboratories in 1970 S
Features of C language:
- 1) Portability
- 2) Redability
- 3) Maintainability
- 4) Extendibility
- 5) faster and efficent
- 6) Flexible
Advantages of C
- 1) It is easy to learn and use
- 2) Adding a new feature is easier and faster.
- 3) It is faster and efficent to use.
- 4) It is a flexible language
Disadvantages
- a)It Takes more time to design and implement the slw
- b)It doesnot contain run time checking
- c)AS the program extends difficult to fix the errors .
- d)It doesnot support opp (object oriented programming)
C is also called as :
- a)High level language
- b)Mother language
- c)Midddle level language
- d)Structure programming language
- e)Flexible language
-
C Language basic Structure
#include<stdio.h> int main(void) { printf("Hello World"); return 0; } /* Output: Hello World */
- stdio.h (Standard Input/Output): This header file provides definitions for the standard input and output functions, such as printf, scanf, getchar, and putchar.
- string.h (String handling): This header file provides definitions for various string-related functions, such as strcpy, strcat, strlen, strcmp, etc.
- math.h (Mathematical functions): This header file provides definitions for various mathematical functions, such as sqrt, pow, exp, log, etc.
- stdlib.h (Standard Library): This header file provides definitions for various standard library functions, such as malloc, free, atoi, itoa, etc.
- time.h (Time and date functions): This header file provides definitions for various time and date related functions, such as time, gmtime, mktime, etc.
- ctype.h (Character handling): This header file provides definitions for various character-related functions, such as islower, isupper, tolower, toupper, etc.
The character set used in C programming
The character set used in C programming is the ASCII character set, which consists of 128 characters and includes the 26 uppercase and lowercase letters of the English alphabet, the digits 0-9, various punctuation marks, and a number of special charactersExample of Character
- '.' (period)
- ',' (comma)
- ':' (colon)
- ';' (semicolon)
- '!' (exclamation mark)
- '?' (question mark)
- '$' (dollar sign)
- '@' (at sign)
- '&' (ampersand)
- '*' (asterisk)
- '+' (plus sign)
1):Uppercase characters
'A ','B 'to 'Z'
2) Lowercase characters:
'a''b''c' TO 'z'
3) Punctuation marks:
4) Special characters:
Explanation of upper code:
a) The first line #includeb) The main function is the entry point of the program. It is defined with the int return type, which indicates that the function returns an integer value. The void in the parentheses indicates that the function takes no arguments.
Within the body of the main function, we use the printf function to print the string "Hello, World!" to the console.
Finally, the return 0; statement at the end of the main function returns the value 0 to indicate that the program has executed successfully.
Header file IN C
In C programming, header files are used to store declarations and prototypes of functions, variables, and other constructs that can be used across multiple source files. Here are some commonly used header files in C with a brief description:
Comment in C
Comments are non-executable statements that are ignored by the compiler. They are used to add annotations, explanations, or notes to the source code to make it easier to understand for humans. There are two types of comments in C:
1) Single-line comments:
Single-line comments start with // and continue until the end of the line. For example:
// This is a single-line comment
2) Multi-line comments:
Multi-line comments start with /* and end with */. Everything in between is considered a comment and can span multiple lines. For example:
/* This is a multi-line comment */
Introduction to C
In C programming language, tokens are the basic building blocks of a program. Tokens are defined as the smallest individual units in a program. There are several types of tokens in C:
Keywords: Keywords are reserved words in C that have a special meaning and cannot be used as identifiers. Examples of keywords in C are int, char, if, while, etc.
Identifiers: Identifiers are names given to variables, functions, arrays, etc. in a C program. They can be any combination of letters, digits, and underscores, as long as they do not start with a digit.
Strings: Strings are sequences of characters surrounded by double quotes. They are used to represent text in C programs.
Operators: Operators are symbols that perform operations on one or more operands. In C, there are several types of operators, such as arithmetic operators, comparison operators, logical operators, etc.
Data Type in C
In C programming language, data types are used to define the type of values that a variable can hold.There are two main types of data types:
Primitive data types: Primitive data types are the basic data types that are built into the language. Examples of primitive data types in C are int, char, float, double, void, etc. These data types can store values of different sizes and ranges, and they cannot be further divided into simpler data types.
Example of a program in C that uses several primitive data types:
#include<stdio.h>
int main()
{
int age = 30;
char grade = 'A';
float price = 12.99;
double weight = 68.5;
printf("Age: %d\n", age);
printf("Grade: %c\n", grade);
printf("Price: %f\n", price);
printf("Weight: %lf\n", weight);
return 0;
}
/*
Age: 30
Grade: A
Price: 12.990000
Weight: 68.500000
*/
Derived data types:
Derived data types are data types that are derived from primitive data types or other derived data types. Examples of derived data types in C are arrays, structures, unions, pointers, and function types. These data types are used to store collections of data or to represent complex data structures.
Example of a program in C that uses an array of int values as a derived data type:
#include<stdio.h>
int main()
{
int numbers[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++)
{
printf("Number %d: %d\n", i + 1, numbers[i]);
}
return 0;
}
}
/*
Number 1: 1
Number 2: 2
Number 3: 3
Number 4: 4
Number 5: 5
*/
Constants
A constant is a value in a C program that cannot be changed once it has been set. Constants are used to represent values that remain the same throughout the execution of a program.
Constants are declared using the const keyword, followed by the data type of the constant and its name.
For example :
#include<stdio.h>
int main()
{
const double PI = 3.14159265;
printf("The value of PI is: %lf\n", PI);
return 0;
}
/*Output:
The value of PI is: 3.141593
*/
Types of Constant
C has four basic types of constants . They are :
1)Integer constant: It is an integer-valued number. It can be positive or negative. It can also be decimal, octal, or hexadecimal integer constant.
2) Floating-point constant: It is a fractional number. It can be written either in decimal form or an exponent form or both. It can also be decimal, octal, or hexadecimal floating-point constant.
3)
Character constant: It is a single character, enclosed in apostrophes (single quotation marks).
4) String constant: It consists of any number of consecutive characters enclosed in double quotation marks.
Variable
Variable is the name of a memory location which stores some data.Variables are the most fundamental part of any programming language .
#include<stdio.h>
int main()
{
int age = 30;
printf("Age: %d\n", age);
age = 35;
printf("New Age: %d\n", age);
return 0;
}
/*
Output:
Age: 30
New Age: 35
*/
In C programming, there are a few rules that must be followed when naming variables:
1) Variable names must begin with a letter or an underscore (_).
2)Variable names can only contain letters, digits, and underscores.
3)Variable names cannot contain spaces.
4)Variable names cannot be a keyword or a reserved word in the C language.
5)Variable names should be descriptive and meaningful.
6)Variable names are case sensitive in C, so age and Age are considered to be two different variables.
Escape Sequences
Escape sequences are a set of special characters in C programming language that represent non-printable characters such as a newline, tab, backspace, and others. The escape sequences are represented by a backslash (\) followed by a character.
Some of the most commonly used escape sequences in C are:
Escape Sequence | Description |
---|---|
\n | Represents a newline character |
\t | Represents a tab character |
\b | Represents a backspace character |
\\ | Represents a backslash character |
\' | Represents a single quote character |
\" | Represents a double quote character |
\r | Represents a carriage return character |
Operators
C programming language provides several types of operators to perform operations on variables and values.
Some of the most commonly used operators in C are:
Operator Type | Operators | Example |
---|---|---|
Arithmetic Operators | + (Addition), - (Subtraction), * (Multiplication), / (Division), % (Modulus), ** (Exponentiation) | 10 + 5 = 15, 20 - 10 = 10, 5 * 5 = 25, 20 / 5 = 4, 20 % 3 = 2, 2 ** 3 = 8 |
Logical Operators | && (And), || (Or), ! (Not) | true && false = false, true || false = true, !true = false |
Equality Operators | == (Equal to), != (Not equal to), === (Strict equal to), !== (Strict not equal to) | 10 == "10" = true, 10 != "10" = false, 10 === "10" = false, 10 !== "10" = true |
Relational Operators | < (Less than), > (Greater than), <= (Less than or equal to), >= (Greater than or equal to) | 10 < 20 = true, 10 > 20 = false, 10 <= 20 = true, 10 >= 20 = false |
Assignment Operators | = (Assign), += (Add and assign), -= (Subtract and assign), *= (Multiply and assign), /= (Divide and assign), %= (Modulus and assign), **= (Exponentiation and assign) | x = 10, x += 5 (x = x + 5), x -= 5 (x = x - 5), x *= 5 (x = x * 5), x /= 5 (x = x / 5), |
#include<stdio.h>
int main()
{
float area, r, PI=3.14;
printf("Enter Radius Of Circle: \n");
scanf("%f", &r);
area=PI*r*r;
printf("The Area Of Circle Is: %f ", area);
return 0;
}
/*
Output:
Enter Radius Of Circle:
7
The Area Of Circle Is: 153.860016
*/
#include<stdio.h>
int main()
{
int l, b, area;
printf("Enter The Length Of Rectangle:\n");
scanf("%d", &l);
printf("Enter The Breadth Of Rectangle: \n");
scanf("%d", &b);
area=l*b;
printf("The area of Rectangle is: %d", area);
return 0;
}
/*
Output:
Enter The Length Of Rectangle:
4
Enter The Breadth Of Rectangle:
5
The area of Rectangle is: 20
*/
#include<stdio.h>
int main()
{
int x = 10, y = 15, temp;
printf("Value Before Swapping x=%d and y=%d\n", x, y);
temp = x;
x = y;
y = temp;
printf("Value After Swapping x = %d and y = %d", x, y);
return 0;
}
/*
Output:
Value Before Swapping x=10 and y=15
Value After Swapping x = 15 and y = 10
*/
#include<stdio.h>
int main()
{
int a;
printf("Enter A Number:\n");
scanf("%d", &a);
if(a%2==0){
printf("The Input Number Is Even.");
}
else{
printf("The Input Number Is Odd.");
}
return 0;
}
/*
Output:
Enter A Number:
5
The Input Number Is Odd.
*/
#include<stdio.h>
int main()
{
int a;
printf("Enter A Number:\n");
scanf("%d", &a);
if(a%5==0){
printf("The Number %d Is Divisible By 5", a);
}
else{
printf("The Number %d Is Not Divisible By 5", a);
}
return 0;
}
/*
Output:
Enter A Number:
25
The Number 25 Is Divisible By 5
*/
#include<stdio.h>
int main()
{
int a, b, c;
printf("Enter 3 Number's:\n");
scanf("%d %d %d", &a, &b, &c);
if(a>b && a>c){
printf("The %d is Greatest Number.", a);
}
else if(b>a && b>c){
printf("The %d is Greatest Number.", b);
}
else{
printf("The %d is Greatest Number.", c);
}
return 0;
}
/*
Output:
Enter 3 Number's:
7
1
2
The 7 is Greatest Number.
*/
#include <stdio.h>
int main()
{
int a,b,c;
printf("Enter three numbers\n");
scanf("%d %d %d",&a,&b,&c);
if(b>a && a>c || c>a && a>b){
printf("\n%d is a middle number",a);
}
if(a>b && b>c || c>b && b>a){
printf("\n%d is a middle number",b);
}
if(a>c && c>b || b>c && c>a){
printf("\n%d is a middle number",c);
}
return 0;
}
/*
Output:
Enter three numbers
5
6
7
6 is a middle number
*/
#include<stdio.h>
int main()
{
int a, b, c;
printf("Enter 3 Number's:\n");
scanf("%d %d %d", &a, &b, &c);
if(a<b && a<c){
printf("The %d is Smallest Number.", a);
}
else if(b<a && b<c){
printf("The %d is Smallest Number.", b);
}
else{
printf("The %d is Smallest Number.", c);
}
return 0;
}
#include<stdio.h>
int main()
{
char ch;
printf("Input a Character : ");
scanf("%c", &ch);
switch(ch)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
printf("\n\n%c is a vowel.\n\n", ch);
break;
default:
printf("%c is not a vowel.\n\n", ch);
}
return 0;
}
/*
Output:
Input a Character : A
A is a vowel.
*/
#include<stdio.h>
int main()
{
int a;
while (a<10)
{
printf("%d\t", a);
a++;
}
return 0;
}
/*
Output:
0 1 2 3 4 5 6 7 8 9
*/
#include<stdio.h>
int main()
{
int c=10;
do
{
printf("%d\t", c);
c=c-1;
} while (c>0);
return 0;
}
/*
Output:
10 9 8 7 6 5 4 3 2 1
*/
#include<stdio.h>
int main()
{
int fact, i, n;
fact = 1;
printf("Enter the number:\n");
scanf("%d" , &n);
for(i = 1; i <= n; i++)
{
fact = fact*i;
}
printf("Factorial of %d is %d", n , fact);
return 0;
}
/*
Output:
Enter the number:
5
Factorial of 5 is 120
*/
#include<stdio.h>
int main()
{
int i, j;
for(i=0;i<=5;i++){
for(j=0;j<=i;j++){
printf("*");
}
printf("\n");
}
return 0;
}
/*
Output:
*
**
***
****
*****
******
*/
#include<stdio.h>
int main()
{
int a, b, c, d, e;
float avg;
printf("Enter 5 Number's :\n");
scanf("%d %d %d %d %d", &a, &b, &c, &d, &e);
avg=(a+b+c+d+e)/5;
printf("The Average Of 5 Number's is: %f", avg);
return 0;
}/*
Output:
Enter 5 Number's :
1
2
3
5
4
The Average Of 5 Number's is: 3.000000
*/
#include <stdio.h>
int main() {
int n, i;
printf("Enter an integer: ");
scanf("%d", &n);
for (i = 1; i <= 10; ++i) {
printf("%d * %d = %d \n", n, i, n * i);
}
return 0;
}
/*
Output:
Enter an integer: 6
6 * 1 = 6
6 * 2 = 12
6 * 3 = 18
6 * 4 = 24
6 * 5 = 30
6 * 6 = 36
6 * 7 = 42
6 * 8 = 48
6 * 9 = 54
6 * 10 = 60
*/
#include <stdio.h>
int main() {
int n, i, sum = 0;
printf("Enter A integer: ");
scanf("%d", &n);
for (i = 1; i <= n; ++i) {
sum += i;
}
printf("Sum of 0 from %d Is: %d",n, sum);
return 0;
}
/*
Output:
Enter A integer: 5
Sum of 0 from 5 Is: 15
*/
#include <stdio.h>
int main() {
int n, reversed = 0, remainder, original;
printf("Enter an integer: ");
scanf("%d", &n);
original = n;
while (n != 0) {
remainder = n % 10;
reversed = reversed * 10 + remainder;
n /= 10;
}
if (original == reversed)
printf("%d is a palindrome.", original);
else
printf("%d is not a palindrome.", original);
return 0;
}
/*
Output:
Enter an integer: 101
101 is a palindrome.
*/
#include <stdio.h>
int main()
{
int marks[10], i, n, sum = 0, average;
printf("Enter number of elements: ");
scanf("%d", &n);
for(i=0; i<n; ++i)
{
printf("Enter number%d: ",i+1);
scanf("%d", &marks[i]);
sum += marks[i];
}
average = sum/n;
printf("Average = %d", average);
return 0;
}
/*
Output:
Enter number of elements: 5
Enter number1: 1
Enter number2: 2
Enter number3: 3
Enter number4: 4
Enter number5: 5
Average = 3
*/
#include <stdio.h>
int main()
{
int Variable = 15;
printf("Variable: %d\n", Variable);
printf("address of Variable: %p", &Variable);
return 0;
}
/*
Output:
Variable: 15
address of Variable: 000000000062FE1C
*/
#include <stdio.h>
int main()
{
char name[20];
printf("Enter name: ");
scanf("%s", name);
printf("Your name is %s.", name);
return 0;
}
/*
Output:
Enter name: Tech
Your name is Tech.
*/
#include <stdio.h>
#include <string.h>
int main()
{
char a[100];
int length;
printf("Enter a string: \n");
gets(a);
length = strlen(a);
printf("Length of the string = %d\n", length);
return 0;
}
#include <stdio.h>
#include <string.h>
int main() {
char str1[100] = "C Programming ", str2[] = "Language";
strcat(str1, str2);
puts(str1);
return 0;
}
/*
Output:
C Programming Language
*/
#include<stdio.h>
#include<string.h>
int main()
{
char a[100], b[100];
printf("Enter the first string\n");
gets(a);
printf("Enter the second string\n");
gets(b);
if( strcmp(a,b) == 0 )
printf("Entered strings are equal.\n");
else
printf("Entered strings are not equal.\n");
return 0;
}
/*
Output:
Enter the first string
Programming
Enter the second string
Programming
Entered strings are equal.
*/
#include<stdio.h>
#include<string.h>
int main()
{
char s[100];
printf("Enter a string in Uppercase: ");
scanf("%[^\n]",s);
printf("In Lower Case:\n");
puts(strlwr(s));
return 0;
}
/*
Output:
Enter a string in Uppercase: PROGRAMMING
In Lower Case:
programming
*/
#include<stdio.h>
#include<string.h>
int main()
{
char s[100];
printf("Enter a string in Lowercase: ");
scanf("%[^\n]",s);
printf("In Uppercase Case:\n");
puts(strupr(s));
return 0;
}
/*
Output:
Enter a string in lowercase: programming
In Uppercase Case:
PROGRAMMING
*/
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100
int main()
{
char str[MAX_SIZE];
printf("Enter any string: ");
gets(str);
printf("Original string = %s\n", str);
strrev(str);
printf("Reverse string = %s", str);
return 0;
}
/*
Output:
Enter any string: Hello
Original string = Hello
Reverse string = olleH
*/
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100
int main()
{
char str[MAX_SIZE], reverse[MAX_SIZE];
int flag;
printf("Enter any string: ");
gets(str);
strcpy(reverse, str);
strrev(reverse);
flag = strcmp(str, reverse);
if(flag == 0)
{
printf("String is Palindrome.");
}
else
{
printf("String is Not Palindrome.");
}
return 0;
}
/*
Output:
Enter any string: mam
String is Palindrome.
*/
#include<stdio.h>
int main()
{
FILE *fptr;
char name[20];
int age;
float salary;
fptr = fopen("details.txt", "w");
if (fptr == NULL)
{
printf("File does not exist.\n");
return;
}
printf("Enter the name:\n");
scanf("%s", name);
fprintf(fptr, "Name = %s\n", name);
printf("Enter the age:\n");
scanf("%d", &age);
fprintf(fptr, "Age = %d\n", age);
printf("Enter the salary:\n");
scanf("%f", &salary);
fprintf(fptr, "Salary = %.2f\n", salary);
fclose(fptr);
return 0;
}
/*Output:
details.txt Will be created and ask for some details
Enter the name:
John
Enter the age:
27
Enter the salary:
26000
this details will be saved on details.txt file in that format
Name = John
Age = 27
Salary = 26000.00
*/
#include <stdio.h>
int main()
{
float cm, meter, km;
printf("Enter length in centimeter: ");
scanf("%f", &cm);
meter = cm / 100.0;
km = cm / 100000.0;
printf("Length in Meter = %f m \n", meter);
printf("Length in Kilometer = %f km", km);
return 0;
}
/*
Output:
Enter length in centimeter: 1000
Length in Meter = 10.000000 m
Length in Kilometer = 0.010000 km
*/
#include <stdio.h>
#include <math.h>
int main()
{
int base, expo, power;
printf("Enter base: ");
scanf("%d", &base);
printf("Enter exponent: ");
scanf("%d", &expo);
power = pow(base, expo);
printf("%d ^ %d = %d", base, expo, power);
return 0;
}
/*
Output:
Enter base: 2
Enter exponent: 2
2 ^ 2 = 4
*/
#include <stdio.h>
#include <math.h>
int main()
{
float num, root;
printf("Enter any number to find square root: ");
scanf("%f", &num);
root = sqrt(num);
printf("Square root of %.2f = %.2f", num, root);//%.2f is used for 2 numbers after decimal
return 0;
}
/*
Output:
Enter any number to find square root: 100
Square root of 100.00 = 10.00
*/
#include <stdio.h>
int main()
{
float principle, time, rate, SI;
printf("Enter principle (amount): ");
scanf("%f", &principle);
printf("Enter time: ");
scanf("%f", &time);
printf("Enter rate: ");
scanf("%f", &rate);
SI = (principle * time * rate) / 100;
printf("Simple Interest = %.2f", SI);
return 0;
}
/*
Output:
Enter principle (amount): 100000
Enter time: 1
Enter rate: 20
Simple Interest = 20000.00
*/
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
if (year % 400 == 0) {
printf("%d is a leap year.", year);
}
else if (year % 100 == 0) {
printf("%d is not a leap year.", year);
}
else if (year % 4 == 0) {
printf("%d is a leap year.", year);
}
else {
printf("%d is not a leap year.", year);
}
return 0;
}
/*
Output:
Enter a year: 2016
2016 is a leap year.
*/
#include<stdio.h>
int main()
{
char ch;
printf("Enter A Character:\n");
scanf("%c", &ch);
if((ch>='a' && ch<='z') ||(ch>='A' && ch<='Z')){
printf("This is an alphabet");
}
else{
printf("This is Not Alphabet");
}
return 0;
}
/*
Output:
Enter A Character:
a
This is an alphabet
*/
#include <stdio.h>
int main()
{
int week;
printf("Enter week number(1-7): ");
scanf("%d", &week);
switch(week)
{
case 1:
printf("Sunday");
break;
case 2:
printf("Monday");
break;
case 3:
printf("Tuesday");
break;
case 4:
printf("Wednesday");
break;
case 5:
printf("Thursday");
break;
case 6:
printf("Friday");
break;
case 7:
printf("Saturday");
break;
default:
printf("Invalid input! Please enter week number between 1-7.");
}
return 0;
}
/*
Output:
Enter week number(1-7): 4
Wednesday
*/
#include<stdio.h>
int main()
{
char ch;
printf("Alphabets from a - z are: \n");
for(ch='a'; ch<='z'; ch++)
{
printf("%c\t", ch);
}
return 0;
}
/*
Output:
Alphabets from a - z are:
a b c d e f g h i j
k l m n o p q r s t
u v w x y z
*/
#include<stdio.h>
int main()
{
int i, n;
printf("Print all even numbers till: ");
scanf("%d", &n);
printf("Even numbers from 1 to %d are: \n", n);
for(i=1; i<=n; i++)
{
if(i%2 == 0)
{
printf("%d\n", i);
}
}
return 0;
}
/*
Output:
Print all even numbers till: 10
Even numbers from 1 to 10 are:
2
4
6
8
10
*/
#include<stdio.h>
int main()
{
int i, n;
printf("Print all Odd numbers till: ");
scanf("%d", &n);
printf("Odd numbers from 1 to %d are: \n", n);
for(i=1; i<=n; i++)
{
if(i%2!= 0)
{
printf("%d\n", i);
}
}
return 0;
}
/*
Output:
Print all Odd numbers till: 10
Odd numbers from 1 to 10 are:
1
3
5
7
9
*/
#include <stdio.h>
int main()
{
int i, num;
printf("Enter number to print table: ");
scanf("%d", &num);
for(i=1; i<=10; i++)
{
printf("%d * %d = %d\n", num, i, (num*i));
}
return 0;
}
/*
Output:
Enter number to print table: 6
6 * 1 = 6
6 * 2 = 12
6 * 3 = 18
6 * 4 = 24
6 * 5 = 30
6 * 6 = 36
6 * 7 = 42
6 * 8 = 48
6 * 9 = 54
6 * 10 = 60
*/
#include<stdio.h>
int main()
{
int n;
printf("Enter A Number:\n");
scanf("%d", &n);
if(n<0){
printf("%d Is Negative", n);
}
else if(n==0){
printf("%d is Zero(0)", n);
}
else{
printf("%d Is Positive", n);
}
return 0;
}
/*
Output:
Enter A Number:
55
55 Is Positive
*/
#include<stdio.h>
#include<string.h>
struct Employee
{
char name[30];
int roll_no;
char favChar;
char address[40];
};
int main()
{
struct Employee s1;
printf("\nEnter Employee Name:\n");
scanf("%s", s1.name);
printf("Enter Employee Salary.:\n");
scanf("%d", &s1.roll_no);
printf("Enter Employee Address:\n");
scanf("%s", &s1.address);
printf("\n\n\n\tThe Employee Name Is:%s", s1.name);
printf("\n\tThe Employee Roll No. Is:%d", s1.roll_no);
printf("\n\tThe Employee Address Is:%s", s1.address);
return 0;
}
/*
Output:
Enter Employee Name:
John
Enter Employee Salary.:
15000
Enter Employee Address:
Canada
The Employee Name Is:John
The Employee Roll No. Is:15000
The Employee Address Is:Canada
*/
#include <stdio.h>
struct student
{
char name[30];
int roll_no;
char favChar;
char address[40];
};
int main()
{
struct student s1;
printf("\nEnter Student Name:\n");
scanf("%s", s1.name);
printf("Enter Student Roll No.:\n");
scanf("%d", &s1.roll_no);
printf("Enter Student Favourate Chracter:\n");
scanf(" %c", &s1.favChar);
printf("Enter Student Address:\n");
scanf("%s", &s1.address);
printf("\nStudent Details");
printf("\n\nThe Student Name Is:%s", s1.name);
printf("\nThe Student Roll No. Is:%d", s1.roll_no);
printf("\nThe Student Favourate Chracter Is:%c", s1.favChar);
printf("\nThe Student Address Is:%s", s1.address);
return 0;
}
/*
Output:
Enter Student Name:
John
Enter Student Roll No.:
12
Enter Student Favourate Chracter:
W
Enter Student Address:
France
Student Details
The Student Name Is:John
The Student Roll No. Is:12
The Student Favourate Chracter Is:W
The Student Address Is:France
*/
#include <stdio.h>
int main()
{
float celsius, fahrenheit;
printf("Enter temperature in Celsius: ");
scanf("%f", &celsius);
fahrenheit = (celsius * 9 / 5) + 32;
printf("%.2f Celsius = %.2f Fahrenheit", celsius, fahrenheit);
return 0;
}
/*
Output:
Enter temperature in Celsius: 0
0.00 Celsius = 32.00 Fahrenheit
*/
#include <stdio.h>
int main() {
int n, rev = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &n);
while (n != 0) {
remainder = n % 10;
rev = rev * 10 + remainder;
n /= 10;
}
printf("Reversed number = %d", rev);
return 0;
}
/*
Output:
Enter an integer: 5542
Reversed number = 2455
*/
#include<stdio.h>
long int multiplyNumbers(int n);
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
return 0;
}
long int multiplyNumbers(int n) {
if (n>=1)
return n*multiplyNumbers(n-1);
else
return 1;
}
/*
Output:
Enter a positive integer: 5
Factorial of 5 = 120
*/
Star Patter's Programs
#include <stdio.h>
int main()
{
int n, s, i, j;
printf("Enter number of rows: ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
for (s = i; s < n; s++)
{
printf(" ");
}
for (j = 1; j <= (2 * i - 1); j++)
{
printf("*");
}
printf("\n");
}
return 0;
}
/*
Output:
Enter number of rows: 5
*
***
*****
*******
*********
*/
#include <stdio.h>
int main()
{
int i, j, n;
printf("Enter number of rows: ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
for (j = 1; j <= i; j++)
{
printf("* ");
}
printf("\n");
}
return 0;
}
/*
OUtput:
Enter number of rows: 5
*
* *
* * *
* * * *
* * * * *
*/
#include <stdio.h>
int main()
{
int i, j, n;
printf("Enter number of rows: ");
scanf("%d", &n);
for (i = n; i >= 1; i--)
{
for (j = 1; j <= i; j++)
{
printf("* ");
}
printf("\n");
}
return 0;
}
/*
Output:
Enter number of rows: 5
* * * * *
* * * *
* * *
* *
*
*/
#include <stdio.h>
int main()
{
int r, i, j, s;
printf("Enter number of rows: ");
scanf("%d", &r);
for (i = 1; i <= r; i++)
{
for (s = i; s < r; s++)
printf(" ");
for (j = 1; j <= (2 * r - 1); j++)
{
if (i == r || j == 1 || j == 2 * i - 1)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}
/*
Output:
Enter number of rows: 5
*
* *
* *
* *
*********
*/
#include <stdio.h>
int main()
{
int r, i, j, s;
printf("Enter number of rows: ");
scanf("%d", &r);
for (i = r; i >= 1; i--)
{
for (s = i; s < r; s++)
printf(" ");
for (j = 1; j <= 2 * i - 1; j++)
{
if (i == r || j == 1 || j == 2 * i - 1)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}
/*
Output:
Enter number of rows: 5
*********
* *
* *
* *
*
*/
#include <stdio.h>
int main()
{
int n, i, j;
printf("Enter the number of rows:\n");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
printf("*");
}
printf("\n");
}
return 0;
}
/*
Output:
Enter the number of rows:
5
*****
*****
*****
*****
*****
*/
#include <stdio.h>
int main()
{
int n, i, j;
printf("Enter The Number To Print Plus Pattern(Odd Only):\n");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
if (i == ((n / 2) + 1))
{
for (j = 1; j <= n; j++)
{
printf("+");
}
}
else
{
for (j = 1; j <= n / 2; j++)
{
printf(" ");
}
printf("+");
}
printf("\n");
}
return 0;
}
/*
Output:
Enter The Number To Print Plus Pattern(Odd Only):
5
+
+
+++++
+
+
*/
#include <stdio.h>
int main()
{
int n, m, i, j;
printf("Enter the number:\n");
scanf("%d", &n);
m = 2 * n - 1;
for (i = 1; i <= m; i++)
{
for (j = 1; j <= m; j++)
{
if (i == j || j == (m - i + 1))
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
return 0;
}
/*
Output:
Enter the number5
* *
* *
* *
* *
*
* *
* *
* *
* *
*/
C Programming Project
#include <stdio.h>
int main()
{
int a, b;
char op;
float result;
printf("Enter First Number:");
scanf("%d", &a);
printf("Enter Second Number:");
scanf("%d", &b);
printf("Enter OPerater:");
scanf(" %c", &op);
switch (op)
{
case '+':
result = a + b;
printf("The Sum Is %f", result);
break;
case '-':
result = a - b;
printf("The Subtracr Is %f", result);
break;
case '*':
result = a * b;
printf("The Multiply Is %f", result);
break;
case '/':
result = a / b;
printf("The Division Is %f", result);
break;
default:
printf("Please Envalid Operater");
break;
}
return 0;
}
/*
Output:
Enter First Number:4
Enter Second Number:5
Enter OPerater:+
The Sum Is 9.000000
*/
wow sir
ReplyDelete