0% found this document useful (0 votes)
13 views24 pages

C Programs for Basic Operations

The document contains C code snippets demonstrating various programming concepts like conditional statements, loops, functions, etc. Specifically, it includes: 1) Programs to check divisibility, even/odd, two digit numbers using if-else statements. 2) A program to calculate average of numbers input by the user. 3) Programs to check if a user is adult/child using if-else and ternary operators. 4) Programs using loops like for, do-while to print patterns, numbers, tables and continue/break statements. 5) Functions to calculate sum and print tables by passing arguments. 6) Programs to check grades, factorials and nationality

Uploaded by

imrananjum900696
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views24 pages

C Programs for Basic Operations

The document contains C code snippets demonstrating various programming concepts like conditional statements, loops, functions, etc. Specifically, it includes: 1) Programs to check divisibility, even/odd, two digit numbers using if-else statements. 2) A program to calculate average of numbers input by the user. 3) Programs to check if a user is adult/child using if-else and ternary operators. 4) Programs using loops like for, do-while to print patterns, numbers, tables and continue/break statements. 5) Functions to calculate sum and print tables by passing arguments. 6) Programs to check grades, factorials and nationality

Uploaded by

imrananjum900696
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include<stdio.

h>
#include<math.h>

//[Link] divisibility method


/*int main(){
int x;
printf("enter a number : ");
scanf("%d", &x);

printf("%d", x % 2 == 0);
//(if divisible ,then output=1(means true) & if not divisible ,then
output=0(means false).)

return 0;
}*/

//[Link] a program to check even OR odd


/*int main(){
//even - 1
//odd - 0

int x;
printf("enter a number : ");
scanf("%d", &x);
printf("%d", x % 2 == 0);
return 0;
}*/

//[Link] a program to check for two digit number.


/*int main(){
int x;
printf("enter number : ");
scanf("%d", &x);
printf("%d", x>9 && x<100);
//(if output is 1-it's true & if output is 0-it's false)

return 0;
}*/

//[Link] a program to print the average of numbers.


/*int main(){
int x,y,z;

printf("enter 1st number : ");


scanf("%d", &x);

printf("enter 2nd number : ");


scanf("%d", &y);
printf("enter 3rd number : ");
scanf("%d", &z);

printf("Average is : %d", (x+y+z)/3);


return 0;
}*/

//[Link] a program for checking adolescent & the work they can do.
/*int main(){
int age;
printf("Enter your age : ");
scanf("%d", &age);

if(age > 18){


printf("Yes,you are adult \n");
printf("You can vote \n");
printf("You can drive \n");
}
//(if you have one statement in if or else,then you can also use witout
{},but you always use{}.)

else{
printf("No,you are not adult \n");
}

printf("Thank you");
return 0;
}*/

//5.(2nd method by ternary oprator.) - *very important*


/*int main(){
int age;
printf("enter your age : ");
scanf("%d", &age);

age >= 18 ? printf("You are adult \n") : printf("You are not adult \n");
return 0;
}*/

//[Link] for child,teenager,adult.


/*int main(){
int age;
printf("enter your age : ");
scanf("%d", &age);

if(age >= 18){


printf("You are a adult \n");
}

else if(age > 13 && age <18){


printf("You are a teenager \n");
}
//1st statement mai if & last statement mai else fix h.,but bich mai koi
v statement aaye us mai (else if) use hoga.

else{
printf("You are a child");
}
return 0;
}*/

//[Link] for days


/*int main(){
int day; //(1-monday; 2-tuesday; 3-wednesday; 4-thursday; 5-friday; 6-
saturday; 7-sunday)
printf("Enter day(1-7) of week : ");
scanf("%d", &day);

switch (day){
case 1 : printf("It's Monday \n");
break;
case 2 : printf("It's Tuesday \n");
break;
case 3 : printf("It's Wednesday \n");
break;
case 4 : printf("It's Thursday \n");
break;
case 5 : printf("It's Friday \n");
break;
case 6 : printf("It's Saturday \n");
break;
case 7 : printf("It's Sunday \n");
break;

//(you can switch any case in any place,but default is


last)

default : printf("It's Not valid");


break;//(after default break is not nessesary)
}
return 0;
}*/

//[Link] k under if then,else & else k under if then,else wala system....


/*int main(){
int number;
printf("enter a number : ");
scanf("%d", &number);

if(number >= 0 ){
printf("It's +ve \n");
if(number % 2 == 0 ){
printf("It's even \n");
} else {
printf("It's odd \n");
}

} else {
printf("It's -ve \n");
if(number % 2 == 0 ){
printf("It's -ve even \n");
} else {
printf("It's -ve odd \n");
}
}

return 0;
}*/

//[Link] a program to check if a student passed or failed.


/*int main(){
int marks;
printf("Enter your marks : ");
scanf("%d", &marks);

if(marks > 30 ){
printf("You are pass. \n");
printf("Congratulations \n");
} else {
printf("You are fail. \n");
printf("Focus on your study \n");
}
return 0;
}*/
//(you can also write in short by ternary oprator.) *very important*
/*int main(){
int marks;
printf("Enter your marks : ");
scanf("%d", &marks);

marks > 30 ? printf("You are pass. \n"),printf("Congratulations. \n") :


printf("You are fail. \n"),printf("Still focus on your study. \n");
return 0;
}*/

//[Link] a program for giving grade to the student.*very important*


/*int main(){
int marks;
printf("Enter your marks(0-100) : ");
scanf("%d", &marks);

if(marks == 100 ){
printf("You are pass with 1st [Link] with distinction \n");
printf("Your grade is A+ \n");
printf("Outstanding \n");
}

else if(marks > 90 ){


printf("You are pass with 1st [Link] with distinction \n");
printf("Your grade is A+ \n");
printf("Excellent \n");
}
else if(marks <=90 && marks > 80){
printf("You are pass with 1st [Link] with distinction \n");
printf("Your grade is A \n");
printf("Very good \n");
}
else if(marks <= 80 && marks >= 75){
printf("You are pass with 1st [Link] with distinction \n");
printf("Your grade is B+ \n");
printf("Good \n");
}
else if(marks < 75 && marks > 70){
printf("You are pass with 1st division \n");
printf("Your grade is B+ \n");
printf("Good \n");
}
else if(marks <=70 && marks > 60){
printf("You are pass with 1st division \n");
printf("Your grade is B \n");
printf("Above average \n");
}
else if(marks == 60){
printf("You are pass with 1st division \n");
printf("Your grade is C+ \n");
printf("Average \n");
}
else if(marks < 60 && marks > 50){
printf("You are pass with 2nd division \n");
printf("Your grade is C+ \n");
printf("Average \n");
}
else if(marks == 50){
printf("You are pass with 2nd division \n");
printf("Your grade is C \n");
printf("Below average \n");
}
else if(marks < 50 && marks > 40){
printf("You are pass with 3rd division \n");
printf("Your grade is C \n");
printf("Below average \n");
}
else if(marks <=40 && marks > 30){
printf("You are pass with 3rd division \n");
printf("Your grade is D+ \n");
printf("Pass \n");
}
else if(marks == 30){
printf("You are pass with 3rd division \n");
printf("You are just pass \n");
printf("Focus on your study \n");
}
else if(marks < 30) {
printf("You are fail \n");
printf("Focus on your study & work hard \n");
printf("Away from mobile \n");
}

return 0;
}*/

//[Link] system
//OR print number from 1 to 10
/*int main(){
for(int i=1; i<=10; i=i+1){
//(**Very Important**)
//(if i<=10 part is missing, then infinite loop system is
[Link] mack you memorey ful,untill it crush..)
//(i=i+1 OR i += 1 both are same.)*
//( + means each time value is increases by 1,then always use <= till
how many times.)

printf("%d \n", i);


}

return 0;
}*/
//[Link] the numbers from 0 to n, if n is given by user
/*int main(){
int n;
printf("enter a numer : ");
scanf("%d", &n);

for(int i=0; i<=n; i=i+1){


printf("%d \n", i);
}

return 0;
}*/

//[Link] table for any number(**Very important**)


/*int main(){
int n;
printf("enter a number for creating table of the number : ");
scanf("%d", &n);

for(int i=1; i<=10; i++){


printf("%d \n", n*i);
}

return 0;
}*/

//(another method)
/*int sum(int a, int b);
void printTable(int n);

int main(){
int n;
printf("Enter a number for creating table : ");
scanf("%d", &n);

printTable(n); //argument/actual parameter

return 0;
}

int sum(int x, int y){


return x + y;
}

void printTable(int n){//parameter/formal parameter


for(int i=1; i<=10; i++){
printf("%d \n", i*n);
}
}*/

//[Link] taking numbers as input from user until user an odd number.
/*int main(){
int n;
do{
printf("enter a number : ");
scanf("%d", &n);
printf("%d \n", n);

if(n % 2 !=0){
break;
}
}while(1);
printf("You enter a odd number, that's why it's stop. \n");
printf("Thank You.");

return 0;
}*/

//[Link] skipping any number from counting no.


/*int main(){
for(int i=1; i<=7; i++){
if(i == 3){
//skip 3 from counting no.
continue;
}
printf("%d \n", i);
}

return 0;
}*/

//[Link] to print all no. before entered number,which is given by user


/*int main(){
int n;
printf("Enter any number to get all no. before entered no. : ");
scanf("%d", &n);

for(int i=1; i<=n; i++){


if(i == n){
//skip n from counting no.
continue;
}
printf("%d \n", i);
}

return 0;
}*/

//[Link] all the odd numbers from 5 to 50.


/*int main(){
for(int i=5; i<=50; i++){
if(i % 2 != 0){
printf("%d \n", i);
}
}
return 0;
}*/

//[Link] of a number n.
/*int main(){
int n;
printf("Enter a number to get factorial of the number : ");
scanf("%d", &n);

int fact = 1;
for(int i=1; i<=n; i++){
fact = fact * i;
}
printf("Final factorial is %d", fact);

return 0;
}*/

//[Link] a function that prints Namaste if user is Indian & Bonjour if the
user is french.
//(declaration/prototype)
/*void Namaste();
void Bonjour();

int main(){
printf("Enter f for French & i for Indian : "); //(function call)
char ch;
scanf("%c", &ch);

if(ch=='i'){
Namaste();
} else{
Bonjour();
}

return 0;
}

void Namaste(){
printf("Namaste \n");
}

void Bonjour(){
printf("Bonjour \n");
}

//(function definition)
void printHello(){
printf("Hello! \n");
printf("Goodbye \n");
}*/

//[Link] calculating final price(include GST).


/*void calculatePrice(float value);

int main(){
float value = 100.0;
calculatePrice(value);
printf("Original value is : %f\n", value);
return 0;
}

void calculatePrice(float value){


value = value + (0.18 * value);
printf("Final price(including GST) is : %f\n", value);
}*/

//[Link] calculating final price(include GST), if price is given by user.


//(**Very Very important**)
/*void calculatePrice(int n);

int main(){
int n;
printf("Enter orginal price to get GST included price : ");
scanf("%d", &n);

calculatePrice(n);
printf("Original value is : %d\n", n);
return 0;
}

void calculatePrice(int n){


n = n + (0.18 * n);
printf("Final price(including GST) is : %d\n", n);
}*/
//[Link] square/cube(power of any number),in which number is given by
user.
/*int main(){
int n;
printf("Enter a number to geeting square of the number : ");
scanf("%d", &n);

printf("%f", pow(n, 3));


}*/

//[Link] factorial of number.


/*int fact(int n);

int main(){
printf("Factorial is : %d", fact(5));
return 0;
}
int fact(int n){

//base case 'V' is very imp to stop the program.


if (n==0){
return 1;
}
int factNm1 = fact(n-1);
int factN = factNm1 * n;
return factN;
}*/

//[Link] celsius to fahrenheit


/*float convertTemp(float celsius);

int main(){
float far = convertTemp(37);
printf("farhrenheight is : %f", far);
return 0;
}

float convertTemp(float celsius){


float far = celsius * (9.0/5.0) + 32;
return far;
}*/

//[Link] percentage of marks obtained.


/*int calcPercentage(int science, int math, int sanskrit);

int main(){
int science = 98;
int math = 95;
int sanskrit = 99;

printf("percentage is : %d", calcPercentage(science, math, sanskrit));


return 0;
}

int calcPercentage(int science, int math, int sankrit){


return((science + math + sankrit) / 3);
}*/

//25.(Write a function to calculate the sum, product & average of 2


[Link] that average in the main function.)
/*void doWork(int a, int b, int *sum, int *product, int *avg);

int main(){
int a = 3, b = 5;
int sum, product, avg;
doWork(a, b, &sum, &product, &avg);

printf(" sum = %d\n product = %d\n avg = %d\n", sum, product, avg);
return 0;
}

void doWork(int a, int b, int *sum, int *product, int *avg){


*sum = a+b;
*product = a*b;
*avg = (a+b)/2;
}*/

//[Link] a program to enter price of 3 items & print their final cost with
GST.
/*int main(){
float price[3];
printf("Enter all 3 prices : ");
scanf("%f", &price[0]);
scanf("%f", &price[1]);
scanf("%f", &price[2]);

printf("Total price 1st : %f\n", price[0]+(0.18*price[0]));


printf("Total price 2nd : %f\n", price[1]+(0.18*price[1]));
printf("Total price 3rd : %f\n", price[2]+(0.18*price[2]));

return 0;
}*/

//[Link] a given arrey (int arr[] = {1,2,3,4,5}) ,what will the following
gives?
//a. *(arr+2) b. *(arr+4)
/*int countOdd(int arr[], int n);

int main(){
int arr[] = {1, 2, 3, 4, 5};
printf("%d \n", *(arr+2));
printf("%d \n", *(arr+4));

return 0;
}

int countOdd(int arr[], int n){


int count = 0;
for(int i=0; i<n; i++){
if(arr[i] % 2 != 0){ //odd
count++;
}
}

return count;
}*/

//[Link] marks of two student of three student


/*int main(){
//2 x 3
int marks[2][3];
marks[0][0] = 98;
marks[0][1] = 89;
marks[0][2] = 78;

marks[1][0] = 90;
marks[1][1] = 89;
marks[1][2] = 78;

printf("%d", marks[0][0]);//[0][0] is 1st student & 1st subject marks


}*/

//[Link] a function to reverse an array


/*void reverse(int arr[], int n);
void printArr(int arr[], int n);

int main(){
int arr[] = {1, 2, 3, 4, 5};
reverse(arr, 5);
//printArr(arr, 5);
return 0;
}

void printArr(int arr[], int n){


for(int i=0; i<n; i++){
printf("%d\t", arr[i]);
}
printf("\n");
}

void reverse(int arr[], int n){


for(int i=0; i<n/2; i++){
int firstVal = arr[i];
int secondVal = arr[n-i-1];
arr[i] = secondVal;
arr[n-i-1] = firstVal;
}
}*/
/*int main(){
char star = '*';
printf("Enter any letter of english alphabate");
scanf("%c", &star);

if((ch >= 'a') & (ch <= 'z')){


if(ch == a,e,i,o,u)
{
printf("letter is vowel");
}
if{
printf("letter is constant");
}
}
else{
printf("not a english letter");
}
}*/

//create a arry, storing the tables of 2 & 3.


/*void storeTables(int arr[][10], int n, int m, int number);

int main(){
int tables[2][10];
storeTables(tables, 0, 10, 2);
storeTables(tables, 1, 10, 3);

for(int i=0; i<10; i++){ //loop for 0 to 10


printf("%d\t", tables[0][i]);
}

printf("\n");

for(int i=0; i<10; i++){ //loop for 0 to 10


printf("%d\t", tables[1][i]);
}
return 0;
}

void storeTables(int arr[][10], int n, int m, int number)


{
for(int i=0; i<m; i++){ //loop for 0 to 10
arr[n][i] = number*(i+1); //2, 4, 6, 8, 10...
//arr[0][0] arr[0][1] arr[0][2]...
}
}*/

//create a string firstname & lastName to store detials of user & print all
the characters using a loop.

/*void printString(char arr[]);

int main(){
char firstName[] = "Imran";
char lastName[] ="Ali";

printstring(firstName);
printString(lastName);

return 0;
}

void printString(char arr[]){


for(int i=0; arr[i] != '\0'; i++){
printf("%c", arr[i]);
}
printf("\n");
}*/

/*void printString(char arr[]);

int main(){
char str[100];
fgets(str, 100, stdin);
puts(str);

return 0;
}

void printstring(char arr[]){


for(int i=0; arr[i] !='\0'; i++){
printf("%c", arr[i]);
}
printf("\n");
}*/

/*void printString(char arr[]);

int main(){
char *canChange = "Hello Imran";
puts(canChange);
canChange = "Hello";
puts(canChange);
return 0;
}

void printString(char arr[]){


for(int i=0; arr[i] !='\0'; i++){
printf("%c", arr[i]);
}
printf("\n");
}*/

//print lenght of word, given by user


/*void printString(char arr[]);
int countLength(char arr[]);

int main(){
char name[100];
fgets(name, 100, stdin);
printf("length is : %d", countLength(name));
return 0;
}

int countLength(char arr[]){


int count = 0;
for(int i=0; arr[i] !='\0'; i++){
count++;
}
return count-1;//-1 bcz of '\0' is also count by compiler
}

void printString(char arr[]){


for(int i=0; arr[i] !='\0'; i++){
printf("%c", arr[i]);
}
printf("\n");
}*/

//standard library functions


#include<string.h>

/*void printString(char arr[]);


int countLength(char arr[]);

int main(){
char name[] = "Imran";
int length = strlen(name);
printf("length is : %d", length);
return 0;
}

int countLength(char arr[]){


int count = 0;
for(int i=0; arr[i] !='\0'; i++){
count++;
}
return count-1;//-1 bcz of '\0' is also count by compiler
}

void printString(char arr[]){


for(int i=0; arr[i] !='\0'; i++){
printf("%c", arr[i]);
}
printf("\n");
}*/

/*void printSteing(char arr[]);


int countlength(char arr[]);

int main(){
char oldStr[] = "oldStr";
char newStr[] = "newString";
strcpy(newStr, oldStr);
puts(newStr);
}*/

/*void printString(char arr[]);


int countLength(char arr[]);

int main(){
char firstStr[100] = "Hello";
char secString[] = "IMran";
strcat(firstStr, secString);
puts(firstStr);
}*/

//check wether a year is leap or not.


/*int main(){
int year;
printf("enter a leap year : ");
scanf("%d",&year);
if(year % 100 != 0 && year % 4 == 0){
printf("leap year");
}
else{
printf("Not a leap year");
}
return 0;
}*/

//check weather a triangle is valid or not by ussing sum of two sides is


greatrer than 3rd sides.
/*int main(){
int side1, side2, side3, largeside, sum;
printf("enter three sides of the triangle : ");
scanf("%d%d%d",&side1,&side2,&side3);
if(side1>side2){
if(side1>side3){
sum=side2+side3; largeside = side1;
}
else{
sum=side2+side1; largeside = side3;
}
}

else{
if(side2>side3){
sum=side1+side3; largeside = side2;
}
else{
sum=side1+side2; largeside = side3;
}
}

if(sum>largeside){
printf("the triangle is a valid triangle\n");
}
else{
printf("the triangle is an invalid triangle\n");
}

return 0;
}*/
//find the salted form of a password entered by user if the salt is "123" &
added at the end.
/*void salting(char password[]);

int main(){
char password[100];
scanf("%s", password);
salting(password);
}

void salting(char password[]){


char salt[] = "123";
char newPassword[200];

strcpy(newPassword, password);//Password = "Test"


strcat(newPassword,salt);//newPassword = "test" + "123"
puts(newPassword);
}*/

//write a function named slice, which takes a string & returns a slice string
from index n to m.
/*void slice(char str[], int n, int m);

int main(){
char str[] = "HelloWorld";
slice(str, 3, 6);
}

void slice(char str[], int n, int m){ // n & m are valid value
char newStr[100];
int j = 0;
for(int i=n; i<=m; i++, j++){
newStr[j] = str[i];
}
newStr[j] = '\0';
puts(newStr);
}*/

//write a function to count the occurrence of vawels in a string.


/*int countVowels(char str[]);

int main(){
char str[] = "HelloImran";
printf("Vowels are : %d", countVowels(str));
}

int countVowels(char str[]){


int count = 0;
for(int i=0; str[i] != '\0'; i++){
if(str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' ||
str[i] == 'u'){
count++;
}
}
return count;
}*/

//collecting student data.


/*struct student{
int roll;
float cgpa;
char name[100];
};

int main(){
struct student s1;
[Link] = 1664;
[Link] = 9.2;
strcpy([Link], "Imran");

printf("Student name = %s\n", [Link]);


printf("Student roll no. = %d\n", [Link]);
printf("Student cgpa = %f\n", [Link]);

printf("\n");

struct student s2;


[Link] = 1660;
[Link] = 8.7;
strcpy([Link], "Anjum");

printf("Student name = %s\n", [Link]);


printf("Student roll n0. = %d\n", [Link]);
printf("Student cgpa = %f\n", [Link]);

return 0;
}*/

//another method
/*struct student{
char name[100];
int roll;
float cgpa;
};
int main(){
struct student s1 = {"Imran", 1662, 9.2};
printf("student name = %s\nstudent roll = %d\nstudent cgpa = %f\n",
[Link], [Link], [Link]);

printf("\n");

struct student s2 = {"Anjum", 1660, 8.7};


printf("student name = %s\nstudent roll = %d\nstudent cgpa = %f\n",
[Link], [Link], [Link]);

return 0;
}*/

//Enter address(house no., block, city, state) of 2 people.


/*struct address{
int houseNo;
int block;
char city[100];
char state[100];
};

void printAdd(struct address add);

int main(){
struct address adds[2];
//for input
printf("Enter info for person 1 : ");
scanf("%d", &adds[0].houseNo);
scanf("%d", &adds[0].block);
scanf("%s", adds[0].city);
scanf("%s", adds[0].state);

printf("Enter info for person 2 : ");


scanf("%d", &adds[1].houseNo);
scanf("%d", &adds[1].block);
scanf("%s", adds[1].city);
scanf("%s", adds[1].state);

printAdd(adds[0]);
printAdd(adds[1]);

return 0;
}

void printAdd(struct address add){


printf("address is %d, %d, %s, %s\n",
[Link],[Link],[Link],[Link]);
}*/

//create a structure to store vectors. then make a function to return sum of


2 vectors.
/*struct vector{
int x;
int y;
};

void calcSum(struct vector v1,struct vector v2,struct vector sum);

int main(){
struct vector v1 = {5, 10};
struct vector v2 = {3, 7};
struct vector sum = {0};

calcSum(v1, v2, sum);


return 0;
}

void calcSum(struct vector v1,struct vector v2,struct vector sum)


{
sum.x = v1.x + v2.x;
sum.y = v1.y + v2.y;

printf("sum of x component is : %d\n", sum.x);


printf("sum of y component is : %d\n", sum.y);
}*/

//make a structure tomstore Bank account information of a customer of ABC


bank. also make an alias for it.
/*typedef struct bankAccount {
int accountNo;
char name[100];
} acc;

int main(){
acc acc1 = {123, "Imran"};
acc acc2 = {124, "Anjum"};
acc acc3 = {125, "Gulnar"};

printf("Acc no. = %d\nAcc holder name = %s\n", [Link],


[Link]);

printf("Acc no. = %d\nAcc holder name = %s\n", [Link],


[Link]);
printf("Acc no. = %d\nAcc holder name = %s\n", [Link],
[Link]);

return 0;
}*/

//make a program to read 5 integers from a file.


/*int main(){
FILE *fptr;
fptr = fopen("[Link]", "r");

int n;
fscanf(fptr, "%d", &n);
printf("Number = %d\n", n);

fscanf(fptr, "%d", &n);


printf("Number = %d\n", n);

fscanf(fptr, "%d", &n);


printf("Number = %d\n", n);

fscanf(fptr, "%d", &n);


printf("Number = %d\n", n);

fscanf(fptr, "%d", &n);


printf("Number = %d\n", n);

fclose(fptr);
return 0;
}*/

//make a program to input student information from a user & enter it to a


file.
/*int main(){
FILE *fptr;
fptr = fopen("[Link]", "w");

char name[100];
int roll;
float cgpa;

printf("Enter name : ");


scanf("%s", name);

printf("Enter roll no. : ");


scanf("%d", &roll);

printf("Enter cgpa : ");


scanf("%f", &cgpa);

fprintf(fptr, "Name = %s\n", name);


fprintf(fptr, "Roll No. = %d\n", roll);
fprintf(fptr, "CGPA = %f", cgpa);

fclose(fptr);
return 0;
}*/

//write a program to write all the odd numbers from 1 to n in a file.


/*int main(){
FILE *fptr;
fptr = fopen("[Link]", "w");

int n;
printf("Enter n to get all odd number until n : ");
scanf("%d", &n);

for(int i=1; i<=n; i++){


if(i % 2 != 0){
fprintf(fptr, "%d\t", i);
}
}

fclose(fptr);
return 0;
}*/

//2 number a & b are written in a file. write a program to replace them with
their sum.
/*int main(){
FILE *fptr;
fptr = fopen("[Link]", "r");

int a;
fscanf(fptr, "%d", &a);
int b;
fscanf(fptr, "%d", &b);

fclose(fptr);

fptr = fopen("[Link]", "w");


fprintf(fptr, "%d", a+b);
fclose(fptr);
return 0;
}*/

Common questions

Powered by AI

The program calculates the factorial of a number 'n' using both iterative and recursive methods. In the iterative approach, a for loop multiplies a variable initialized to 1 by 'i', iterating from 1 to 'n', producing 'n!'. The recursive method defines a base case where if 'n' is 0, it returns 1; otherwise, it recursively calls itself with 'n-1' and multiplies the result by 'n', thereby building the factorial through the recursion stack .

The given program determines if a year is a leap year by checking two conditions: if the year is divisible by 100 but not by 400, it is not a leap year; otherwise, if the year is divisible by 4, it is a leap year. The logic uses a combination of logical operators and modulus operation to decide, implemented as: if a year is not divisible by 100 and divisible by 4, it returns 'leap year', otherwise 'Not a leap year' .

The program converts Celsius temperature to Fahrenheit by defining a function that multiplies the Celsius value by (9.0/5.0), and then adds 32, following the formula 'F = C * 9/5 + 32'. This conversion formula is effective and mathematically accurate for converting Celsius measurements into Fahrenheit, and the implementation by encapsulating the conversion in a function enhances modularity and reuse .

The program calculates the percentage of marks by summing marks of three subjects and dividing by three. However, it treats this as an integer division, which could be a limitation if the precision of floating-point arithmetic is needed. It assumes equal weightage for subjects without adjustment for different maximum marks, which could limit accuracy if subjects differ in importance or available marks .

To reverse an array, the program uses a for loop that iterates from the start of the array to the midpoint. It swaps the elements at the 'i-th' position from the start with the 'n-i-1-th' position from the end, where 'n' is the length of the array. Temporary variables are used to facilitate swapping, ensuring in-place reversal of elements .

The program uses a C struct type to define a 'student', containing fields for roll number, CGPA, and name. It initializes instances of this struct with student data and utilizes the 'strcpy' function to copy names into the struct's name field. Then it displays stored data using 'printf', which accesses struct fields via dot notation. This method leverages structs to efficiently bundle related data together, facilitating clearer and more organized data manipulation and output .

In the program, a 'struct' is defined to handle bank account data, containing account number and name fields. The 'typedef' creates an alias 'acc' for 'struct bankAccount', simplifying subsequent declarations. Instances of 'acc' are initialized with account details and printed, demonstrating effective data encapsulation and simpler syntax with typedef usage for consistency and readability in code .

The program demonstrates file reading by opening a file in 'r' mode and using 'fscanf' for reading integers sequentially. Each integer read is printed immediately. This strategy is straightforward for reading known quantities of integers, though it may be inefficient or unsuitable for large or variable-length datasets without additional checks for EOF or errors during reading .

The program models vector addition using a 'struct' for vectors containing 'x' and 'y' components. Two vectors are instantiated and passed to a function computing their sum into another struct. Results are output with 'printf'. While clear and direct, it could be critiqued for potential inefficiency by not using pointers to avoid struct copying, and lack of error handling or dynamic allocation that might be required for more extensive applications .

The program uses a 'scanf' function to take integer input from the user for which a multiplication table needs to be generated. Following the input, the program uses a for loop to iterate through numbers 1 to 10, multiplying each by the user's input and printing the result. This approach effectively captures user input and generates output, although it may not handle invalid inputs robustly without additional error-checking .

You might also like